Javascript then()函数的替代方法
我正在做一个需要PayPal支付网关的项目。我在网上找到了一个简单的教程,除了PayPal响应被发送到控制台日志之外,它工作得很好。联机查找除登录到控制台之外的其他任何实例时,then函数都是空的。现行守则:Javascript then()函数的替代方法,javascript,php,paypal-sandbox,Javascript,Php,Paypal Sandbox,我正在做一个需要PayPal支付网关的项目。我在网上找到了一个简单的教程,除了PayPal响应被发送到控制台日志之外,它工作得很好。联机查找除登录到控制台之外的其他任何实例时,then函数都是空的。现行守则: return actions.order.capture().then(function (details) { console.log(details); 为了将JSON对象发送到PHP处理页面,需要将其更改为除此之外的函数。作为一名后端开发人员,我不确定这将是
return actions.order.capture().then(function (details) {
console.log(details);
为了将JSON对象发送到PHP处理页面,需要将其更改为除此之外的函数。作为一名后端开发人员,我不确定这将是什么功能。谁能提个建议吗?整个代码如下:
HTML&JS脚本
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Paypal Payment</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main id="cart-main">
<div class="site-title text-center">
<h3 class="font-title">Shopping Cart</h3>
</div>
<div class="container">
<div class="grid">
<div class="col-1">
<div class="flex item justify-content-between">
<div class="flex">
<div class="img text-center">
<img src="./assets/pro1.png" alt="">
</div>
<div class="title">
<h3>Canon EOS 1500D</h3>
<span>Electronics</span>
<div class="buttons">
<button type="submit"><i class="fas fa-chevron-up"></i> </button>
<input type="text" class="font-title" value="1">
<button type="submit"><i class="fas fa-chevron-down"></i> </button>
</div>
<a href="#">Save for later</a> |
<a href="#">Delete From Cart</a>
</div>
</div>
<div class="price">
<h4 class="text-red">$349</h4>
</div>
</div>
</div>
<div class="col-2">
<div class="subtotal text-center">
<h3>Price Details</h3>
<ul>
<li class="flex justify-content-between">
<label for="price">Products ( 1 item ) : </label>
<span>$399</span>
</li>
<li class="flex justify-content-between">
<label for="price">Delivery Charges : </label>
<span>Free</span>
</li>
<hr>
<li class="flex justify-content-between">
<label for="price">Amout Payble : </label>
<span class="text-red font-title">$399</span>
</li>
</ul>
<div id="paypal-payment-button">
</div>
</div>
</div>
</div>
</div>
</main>
<script src="https://www.paypal.com/sdk/js?client-id=ASbdZ8CH5kN5y98rzOuKMLPYsHl4QHLYcDGJ6lgaRjxiRp97t53sPWr1yG5vyd9mlHbyqw3vGUZaJsok&disable-funding=credit,card"></script>
<script>
// Create a Global var - the HTML charge is dummy stuff
window.charge = 0.27;
paypal.Buttons({
style : {
color: 'blue',
shape: 'pill'
},
createOrder: function (data, actions) {
return actions.order.create({
purchase_units : [{
amount: {
value: window.charge
}
}]
});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function (details) {
console.log(details);
window.location.replace("https://localhost/PayPal_Simple/payPalResponse.php?q=good");
window.alert('This was successful.');
})
},
onCancel: function (data) {
window.location.replace("https://localhost/PayPal_Simple/payPalResponse.php?q=bad");
window.alert('Something went wrong!');
}
}).render('#paypal-payment-button');</script>
</body>
</html>
PHP
<?php
// Get the Response from PayPal
$status = $_GET['q'];
// Once there is an object that can be tested, that will be used instead of ?q=
if($status = "good") {
echo "The payment was a success.<br />";
} elseif($status = "bad") {
echo "The charge was cancelled.";
} else {
echo "Something else went wrong.";
}
// Of course, this doesn't display anything
echo '<pre>';
print_r($_POST);
echo '<pre>';
// Insert data into the database
// Redirect the client to another page
?>
非常感谢您的帮助!
干杯
Rick不要在客户端捕获数据,然后将数据发送到后端。相反,改为适当的服务器端集成后端应该与PayPal本身通信,并根据请求向客户端发送数据
在服务器上创建两条路由,一条用于“创建订单”,另一条用于“捕获订单”。这些路由应该只返回JSON数据,不返回HTML或文本。后者应在成功返回之前将付款详细信息存储在您的数据库中,尤其是购买单位[0]。payments.captures[0]。id,PayPal交易id
将这两条路线与以下审批流配对:作为,尝试使用
批准
按钮按钮功能
要将内容发送到您希望使用但仍在使用的PHP后端,您放入其中的代码可以做的不仅仅是控制台日志。哇!这是有史以来最快的评论,基思。谢谢,我要开始看fetch了!谢谢你,普雷斯顿。从昨晚起我就一直在和费奇闹着玩。PayPal教程和文章显示这是在客户端完成的,这似乎很奇怪,其他支付提供商捕获授权并将数据发送到后端以进行实际收费。我会检查你链接的文档。当然,简单的客户端集成会在客户端捕获。但如果您的用例是在之后写入服务器数据库,请不要这样做。谢谢StarshipladDev,我也会检查一下。我感谢所有的帮助!
const onApprove= new Promise((resolve, reject) => {
{resolve((data,actions) => {actions.order.capture(data,actions)}});
});
async function doYourButtonPush() {
try {
await onApprove;
console.log("The Promise is resolved!", value);
} catch (e) {
console.error("The Promise is rejected!", error);
} finally {
function (details) {
console.log(details);
window.location.replace("https://localhost/PayPal_Simple/payPalResponse.php?q=good");
window.alert('This was successful.');
}
}
}