我正在尝试通过在SmartButtons中创建订单来使用server side将PayPal付款集成到我的网站上。
按照文档中的步骤进行操作后,例如初始化按钮,然后设置服务器端代码以创建orderID(当我尝试进行付款时),在客户端出现以下错误:
Error: INVALID_RESOURCE_ID↵ at https://www.sandbox.paypal.com/smart/buttons?style.label=paypal&style.layout=horizontal&style.color=gold&style.shape=rect&style.tagline=false&style.height=52&components.0=buttons&locale.country...
在客户端接收到的订单ID是正确的,因为它是在贝宝(Paypal)仪表板上创建的订单ID。.在阅读了其他一些类似的问题后,有人说要捕获付款onApprove
,但是即使设置了用于捕获付款的API只是没有进入用于捕获付款的api。
我的按钮代码如下:
var PAYPAL_SCRIPT = 'https://www.paypal.com/sdk/js?client-id=XXXXXX¤cy=EUR';
var script = document.createElement('script');
script.setAttribute('src', PAYPAL_SCRIPT);
script.onload = function(){
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'horizontal',
label: 'paypal',
tagline: false,
height: 52
},
createOrder: function() {
return fetch('https://localhost:44350/payment/paypal/order/create/', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.headers[2].Value[0]; // Use the same key name for order ID on the client and server
});
},
onApprove: function(data, actions) {
return fetch('https://localhost:44350/payment/paypal/' + data.headers[2].Value[0] + '/capture/', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
}
document.head.appendChild(script);
然后我的api控制器如下:
[HttpPost("paypal/order/create/")]
public Task<HttpResponse> PayPal()
{
return PaypalHelper.CreateOrder("XXXXX", "XXXXXX", true);
}
[HttpPost("paypal/{orderID}/capture/")]
public Task<HttpResponse> PayPal(string orderID)
{
return PaypalHelper.CaptureOrder(orderID, "XXXXX", "XXXXX", true);
}
创建订单的方法如下:
public async static Task<HttpResponse> CreateOrder(string publickey, string privatekey, bool debug = false)
{
var request = new OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(BuildRequestBody());
//3. Call PayPal to set up a transaction
var response = await PayPalClient.client(publickey, privatekey).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()
{
var order = new OrderRequest()
{
CheckoutPaymentIntent = "CAPTURE",
PurchaseUnits = new List<PurchaseUnitRequest>()
{
new PurchaseUnitRequest()
{
AmountWithBreakdown = new AmountWithBreakdown()
{
CurrencyCode = "EUR",
Value = "100.00"
}
}
},
ApplicationContext = new ApplicationContext()
{
ReturnUrl = "https://www.example.it/vmenu?paypal=success",
CancelUrl = "https://www.example.com?paypal=error"
}
};
return order;
}
createOrder提取中的数据如下:
{headers: Array(3), statusCode: 201}headers: Array(3)0: {Key: "Cache-Control", Value: Array(1)}1: {Key: "Date", Value: Array(1)}2: Key: "Paypal-Debug-Id"Value: ["97fcfafaae8ef"]__proto__: Objectlength: 3__proto__: Array(0)statusCode: 201__proto__: Object
menu_test.js:33
尽管获得data.headers[2].Value[0]
的结果是97fcfafaae8ef
答案 0 :(得分:1)
您传递的ID错误。 PayPal-Debug-Id不是订单ID。
此外,orderID不在标头中返回,而是在正文中以“ id”形式返回。
您可以在此处看到示例响应:https://developer.paypal.com/docs/api/orders/v2/#orders_create