【发布时间】:2022-01-21 13:29:18
【问题描述】:
我已经使用 PayPal JS SDK 和相应的 PHP SDK 实现了一个沙盒环境。
在 JavaScript 中我使用:
paypal.Buttons({
createOrder: function(data, actions) {
return fetch('../checkout-sdk/samples/CaptureIntentExamples/CreateOrder.php', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(resp) {
respjs = resp.json();
return respjs;
}).then(function(orderData) {
order_id = orderData.id;
return order_id;
});
},
onApprove: function(data, actions) {
return fetch('../checkout-sdk/samples/CaptureIntentExamples/CaptureOrder.php?order_id='+data.orderID, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
}).then(function(appr_res) {
apprjs = appr_res.json();
return apprjs;
}).then(function(orderData) {
var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
if (errorDetail) {
if (errorDetail.issue === 'INSTRUMENT_DECLINED') {
return actions.restart(); // Recoverable state, per:
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
} else {
var msg = 'Sorry, your transaction could not be processed.';
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
return alert(msg); // Show a failure message
}
}
// Successful capture! For demo purposes:
console.log('Capture result', JSON.stringify(orderData, null, 2));
})
.catch(function(error) {
console.error("There was a problem with the approval fetch: ", error);
});
},
onError: function(err) {
alert(err);
}
}).render('#paypal-button-container');
当函数 createOrder 失败时,PHP 脚本 CreateOrder.php 应该向 javascript SDK 返回一条消息,以便通知客户。
失败时,onApprove 不需要“运行”。 如何更改代码以有条件地“运行” onApprove 函数?
我在 JSLint 上尝试过类似下面的操作:
paypal.Buttons({
createOrder: function(data, actions) {
return fetch(url, {
method: "post"
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.id;
});
},
if (orderData !== false) { // error: Expected ')' and instead saw '!=='.
onApprove: function(data, actions) {
return fetch(url2 + data.orderID, {
method: "post"
}).then(function(res) {
return res.json();
}).then(function(orderData) {
//
});
},
}
}).render("#paypal-button-container");
但 JSLint 报告:应为 ')' 而看到的是 '!=='。
如何有条件地实现 if-then 条件?
【问题讨论】:
标签: paypal