我目前正在尝试将PayPal智能按钮集成到我的asp.net核心Web应用程序中。我参考了以下API文档,以通过服务器上的PayPal API进行交易:https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/#on-the-server。但是,文档在PayPal订单请求正文中对行项目进行了硬编码。现在,我有一个名为 lineItems 的变量,其中包含要发送到PayPal的产品列表。如何在BuildRequestBody()方法中动态创建lineItems?下面是我当前的代码:
更新,我仍在尝试解决此问题。非常感谢您的帮助!
Update2 仍在尝试解决此问题,非常需要帮助。
Update3 是否有人和我有相同的问题?文档非常模糊,我需要帮助。
Update4 ,没有任何进展,仍然需要帮助!
public async Task<HttpResponse> createPaypalTransaction()
{
List<Product> lineItems = new List<Product>();
decimal totalPrice = 4.00;
lineItems.Add(new Product
{
ProductId = 1,
ProductName = "T-Shirt",
Quantity = 2,
Price = 2.00M
});
lineItems.Add(new SanitizeProduct
{
ProductId = 2,
ProductName = "Shoe",
Quantity = 2,
Price = 2.00M
});
var request = new OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(BuildRequestBody());
//3. Call PayPal to set up a transaction
var response = await PayPalClient.client().Execute(request);
var result = response.Result<PayPalCheckoutSdk.Orders.Order>();
Console.WriteLine("Status: {0}", result.Status);
Console.WriteLine("Order Id: {0}", result.Id);
Console.WriteLine("Intent: {0}", result.Intent);
Console.WriteLine("Links:");
foreach (LinkDescription link in result.Links)
{
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}
AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
return response;
}
/*
Method to generate sample create order body with CAPTURE intent
@return OrderRequest with created order request
*/
private static OrderRequest BuildRequestBody(decimal totalPrice, List<SanitizeProduct> lineItems)
{
OrderRequest orderRequest = new OrderRequest()
{
Intent = "CAPTURE",
ApplicationContext = new ApplicationContext
{
BrandName = "12345",
UserAction = "CONTINUE",
},
PurchaseUnits = new List<PurchaseUnitRequest>
{
new PurchaseUnitRequest{
ReferenceId = "PUHF",
Description = "Customisable Goods",
CustomId = "CUST-HighFashions",
SoftDescriptor = "HighFashions",
Amount = new AmountWithBreakdown
{
CurrencyCode = "SGD",
Value = "230.00",
Breakdown = new AmountBreakdown
{
// The subtotal for all items (quantity * price)
ItemTotal = new Money
{
CurrencyCode = "SGD",
Value = "230.00"
},
}
},
// How do i dynamically generate the list of items instead of hardcoding it?
Items = new List<Item>
{
new Item
{
Name = "T-shirt",
Description = "Green XL",
Sku = "sku01",
UnitAmount = new Money
{
CurrencyCode = "SGD",
Value = "90.00"
},
Tax = new Money
{
CurrencyCode = "SGD",
Value = "10.00"
},
Quantity = "1",
Category = "PHYSICAL_GOODS"
},
new Item
{
Name = "Shoes",
Description = "Running, Size 10.5",
Sku = "sku02",
UnitAmount = new Money
{
CurrencyCode = "SGD",
Value = "45.00"
},
Tax = new Money
{
CurrencyCode = "SGD",
Value = "5.00"
},
Quantity = "2",
Category = "PHYSICAL_GOODS"
}
},
}
}
};
return orderRequest;
}