I'm try to implement the paypal express checkout.
(我正在尝试实施Paypal Express结帐。)
So I implement the button and write the result in my database.(因此,我实现了按钮并将结果写入我的数据库。)
Nothing exceptional, it's the script they gave.(没什么特别的,这是他们给的脚本。)
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Call your server to save the transaction
return fetch('recordDatabase.php', {
method: 'post',
mode: "same-origin",
credentials: "same-origin",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
orderID: data.orderID,
time: details.create_time,
status: details.status,
nom: details.payer.name.given_name,
prenom: details.payer.name.surname,
pays: details.payer.address.country_code,
valeur:details.purchase_units[0].amount.value
})
})
});
}
}).render('#paypal-button-container');
</script>
The php to record in the database:
(在数据库中记录的php:)
<?php
$link = connect();
$date = date('Y-m-d H:i:s');
//Receive the RAW post data.
$contentType = isset($_SERVER["CONTENT_TYPE"]) ?trim($_SERVER["CONTENT_TYPE"]) : '';
if ($contentType === "application/json") {
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if(! is_array($decoded)) {
//echo "error";
} else {
$name = $decoded['nom'];
$time = $decoded['time'];
$id = $decoded['orderID'];
$stat = $decoded['status'];
$pays = $decoded['pays'];
$val = $decoded['valeur'];
$secQuery = "INSERT INTO myDatabase(PSEUDO,PASSWORD,CONNECTION,INSCRIPTION,ANNIVERSAIRE,MAIL,IDPAYPAL,STATPAYPAL,NOMPAYER,PAYS,VALEUR) VALUES ('essai','123456',0,'$date','$time','email@mail','$id','$stat','$name','$pays','$val') ";
if (mysqli_query($link,$secQuery)) {
//echo "ok";
} else {
//echo "error";
}
}
} else {
//echo "error";
}
So, the record in my database works fine, but my question is:
(因此,数据库中的记录工作正常,但我的问题是:)
How can I retrieve the echo error or ok in the javascript to confirm the user that everything is fine, or if an error happen.
(如何在JavaScript中检索echo错误或ok,以确认用户一切正常,或者发生错误。)
I tried another solution, to redirect the user from the php and add to the php:
(我尝试了另一种解决方案,将用户从php重定向并添加到php:)
header("Location: confirmation web page");
or
(要么)
echo "<script>window.location = 'confirmation web page'</script>";
but both solution doesn't work.
(但是两种解决方案都行不通。)
No redirection happen(没有重定向发生)
ask by laurent emin translate from so