javascript - 返回 HttpResponse 并在 PayPal 系统中进行响应
问题描述
我是贝宝系统的新手,我在捕获从客户端服务器返回的 orderID 时遇到了很大的问题。
我在服务器端有这个代码:
[HttpPost]
[Route("/Order/create-paypal-order")]
public async Task<HttpResponse> CreatePayPalOrder(bool debug = false)
{
var request = new OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(BuildRequestBody());
//3. Call PayPal to set up a transaction
HttpResponse response = await PayPalClient.client().Execute(request);
if (debug)
{
var result = response.Result<Order>();
Console.WriteLine("Status: {0}", result.Status);
Console.WriteLine("Order Id: {0}", result.Id);
Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
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].AmountWithBreakdown;
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
}
return response;
}
private static OrderRequest BuildRequestBody()
{
OrderRequest orderRequest = new OrderRequest()
{
CheckoutPaymentIntent = "CAPTURE",
ApplicationContext = new ApplicationContext
{
BrandName = "LimitlessSoftTest",
LandingPage = "BILLING",
UserAction = "CONTINUE",
ShippingPreference = "SET_PROVIDED_ADDRESS"
},
PurchaseUnits = new List<PurchaseUnitRequest>
{
new PurchaseUnitRequest{
ReferenceId = "PUHF",
Description = "Sporting Goods",
CustomId = "CUST-HighFashions",
SoftDescriptor = "HighFashions",
AmountWithBreakdown = new AmountWithBreakdown
{
CurrencyCode = "USD",
Value = "230.00",
AmountBreakdown = new AmountBreakdown
{
ItemTotal = new Money
{
CurrencyCode = "USD",
Value = "180.00"
},
Shipping = new Money
{
CurrencyCode = "USD",
Value = "30.00"
},
Handling = new Money
{
CurrencyCode = "USD",
Value = "10.00"
},
TaxTotal = new Money
{
CurrencyCode = "USD",
Value = "20.00"
},
ShippingDiscount = new Money
{
CurrencyCode = "USD",
Value = "10.00"
}
}
},
Items = new List<Item>
{
new Item
{
Name = "T-shirt",
Description = "Green XL",
Sku = "sku01",
UnitAmount = new Money
{
CurrencyCode = "USD",
Value = "90.00"
},
Tax = new Money
{
CurrencyCode = "USD",
Value = "10.00"
},
Quantity = "1",
Category = "PHYSICAL_GOODS"
},
new Item
{
Name = "Shoes",
Description = "Running, Size 10.5",
Sku = "sku02",
UnitAmount = new Money
{
CurrencyCode = "USD",
Value = "45.00"
},
Tax = new Money
{
CurrencyCode = "USD",
Value = "5.00"
},
Quantity = "2",
Category = "PHYSICAL_GOODS"
}
},
ShippingDetail = new ShippingDetail
{
Name = new Name
{
FullName = "John Doe"
},
AddressPortable = new AddressPortable
{
AddressLine1 = "123 Townsend St",
AddressLine2 = "Floor 6",
AdminArea2 = "San Francisco",
AdminArea1 = "CA",
PostalCode = "94107",
CountryCode = "US"
}
}
}
}
};
return orderRequest;
}
在客户端:
<script src="https://www.paypal.com/sdk/js?client-id=AeGBdpKN0NdruKiwvJIiLsHFhqHYQw7oCyo_G1SsgChM_MgnA4ELwvUPxjJRY-GrqXRvTdhvXw-bWPHi¤cy=EUR">
</script>
<div id="paypal-button-container" style="text-align: center"></div>
<script>
paypal.Buttons({
createOrder: function () {
return fetch('/Order/create-paypal-order', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function (res) {
console.log(res);
var a = res.json();
alert("RES:" + a);
console.log(a);
return a;
}).then(function (data) {
console.log(data);
alert("DATA: " + data.id);
return data.id; // Use the key sent by your server's response, ex. 'id' or 'token'
});
},
onApprove: function (data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function (details) {
// This function shows a transaction success message to your buyer.
alert('Transaction completed by ' + details.payer.name.given_name);
});
}
}).render('#paypal-button-container');
</script>
这是我在调试时得到的:
如您所见,有数据和 ID 以及其他所有内容(不确定是否需要直接在“标题”中,但也尝试手动添加)
解决方案
所有返回数据都需要位于服务器 HTTP 响应的正文(而不是标头)中,格式为 JSON。您应该能够将完整的 URL/Order/create-paypal-order
放入任何 Web 浏览器并查看 JSON 数据,其中 id 键具有 orderID 值。如果您看不到这一点,则您的服务器正在返回错误的数据或格式错误。
您还应该在浏览器开发者工具的网络选项卡中调试服务器返回的内容,在获取请求的响应部分到/Order/create-paypal-order
. 如果您确实返回了与您的服务器对象对应的 JSON 数据,这将被格式化为原始字符串,result
就像您应该的那样。
推荐阅读
- keycloak - Keycloak 将注册用户 id 写入外部 SQL 数据库
- amazon-web-services - 如何使用 gremlin 查询 aws neptune 设置查询超时?
- time - 如何从我的代码中删除 TLE 错误(时间限制 = 1 秒)
- arrays - 我可以在 Google 表格中使用 Vlookup 和 CountA 来查询 B 列中有多少个与 A 列具有相同值的非空单元格吗?
- redis - 自定义redis缓存数据目录
- java - 如何使用具有相同 CronTrigger 对象的 Quartz 调度两次调度作业?
- c - 分段错误(c语言)
- python - 使用带有 Huggingface Accelerate 的“gloo”后端
- reactjs - 调度后useSelector不更新值
- javascript - QuaggaJS http问题