![](/img/trans.png)
[英]PayPal Checkout (javascript) with Smart Payment Buttons create order problem
[英]Problem when create order On Paypal Javascript
当脚本在页面HTML / javascript中调用创建订单时,我遇到了问题。
在控制台中,我有一个错误“预期将字符串订单ID传递给createOrder”。
我给我的代码HTMl / javascript
<html >
<body>
<div id="bouton-paypal"></div>
<script src="https://www.paypal.com/sdk/js?client-id=monid"></script>
<script>
paypal.Buttons({
// Set up the transaction
createOrder: function() {
var SETEC_URL = 'http://localhost/paypalphp/paypalcreatepayment.php';
return fetch(SETEC_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.token;
});
}
}).render('#bouton-paypal');
我假设您正在关注在服务器上设置事务的PayPal指南,并且您已经测试过您的服务器正在返回有效的事务。
您获得的错误表明该行return data.token;
没有返回有效的订单ID字符串(很可能返回undefined)。
我已将console.log(data)
添加到您的代码中。 请运行此命令,以便我们可以检查从服务器返回的JSON。
<html> <body> <div id="bouton-paypal"></div> <script src="https://www.paypal.com/sdk/js?client-id=monid"></script> <script> paypal.Buttons({ // Set up the transaction createOrder: function() { var SETEC_URL = 'http://localhost/paypalphp/paypalcreatepayment.php'; return fetch(SETEC_URL, { method: 'post', headers: { 'content-type': 'application/json' } }).then(function(res) { return res.json(); }).then(function(data) { console.log(data) return data.token; }); } }).render('#bouton-paypal'); </script>
谢谢你的回复。 我喜欢这个问题的解决方案
<script>
paypal.Buttons({
style: {
layout: 'vertical',
color: 'blue',
shape: 'rect',
label: 'paypal'
},
// Set up the transaction
createOrder: function() {
var SETEC_URL = 'http://localhost/paypalphp/paypalcreatepayment.php';
return fetch(SETEC_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(data) {
let token;
token = data.paypal_response.links[1].href.match(/EC-\w+/)[0];
console.log(token);
return token;
});
},
// Finalize the transaction
onApprove: function(data) { // On indique le chemin vers notre script PHP qui se chargera d'exécuter le paiement (appelé après approbation de l'utilisateur côté client).
var EXECUTE_URL = 'http://localhost/paypalphp/paypalexecutepayment2.php?payerID='+data.payerID+'&paymentID='+data.paymentID;
/*var data = {
paymentID: data.paymentID,
payerID: data.payerID
};*/
console.log(data);
return fetch(EXECUTE_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
token: data.orderID,
payerID: data.payerID,
paymentID: data.paymentID
})
});
},
onCancel: function(data, actions) {
alert("Paiement annulé : vous avez fermé la fenêtre de paiement.");
},
onError: function(err) {
alert("Paiement annulé : une erreur est survenue. Merci de bien vouloir réessayer ultérieurement.");
}
}).render('#bouton-paypal');
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.