我应该如何使用 paypal SDK 将 orderID 发布到服务器?示例代码既不适用于本地主机,也不适用于远程主机。在我提交 PayPal 按钮后帖子为空。
据称, https://developer.paypal.com/docs/checkout/integrate/#6-verify-the-transaction
PayBundle\Resources\actions\ppal\ppalAction_brt_stkovr.php
<?php
use PayBundle\actions\Order;
echo "<br><br> POST ="; print_r($_POST);
file_put_contents( PHP_EOL . PHP_EOL . __DIR__ .'/out.php', json_encode( $_POST ) .' from '.__FILE__, FILE_APPEND );
include_once ( PATH_ACTIONS_PAY . '/ppal/__init.php' );
include_once ( PATH_ACTIONS_PAY . '/ppal/__payAInit.php' );
require_once PATH_VENDOR . '/braintree/braintree_php/lib/Braintree.php';
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
Braintree_Configuration::environment( 'sandbox' );
Braintree_Configuration::merchantId( 'x' );
Braintree_Configuration::publicKey( 'y' );
Braintree_Configuration::privateKey( 'z' );
if( isset($_POST['payPp'] ) ) {
include_once ( PATH_ACTIONS_PAY . '/ppal/pay/'.$this->vdArr['lang'].'/__Bk.php' );
echo "<br> from ppalAction orderID = "; print_r($this->vdArr['orderID']);
if ( !count(debug_backtrace()) ) {
$ppalOrder = new GetOrder();
$ppalOrder->getOrder( $this->vdArr['orderID'], true);
} // if ( !count(debug_backtrace())
} // if( isset($_POST['paypalA'] ) ) {
else {
$this->vdArr['tokenPpal'] = Braintree_ClientToken::generate();
}
客户端代码:
<script type='text/javascript' >
window.onload = function() {
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) {
//alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
//console.log('Transaction completed' ); alert(details);
console.log( 'data.orderID = ' + data.orderID );
document.getElementById('orderID').value = data.orderID;
console.log('data'); console.log(data);
console.log('Transaction completed by ' + details.payer.name.given_name );
//return fetch('/paypal-transaction-complete', {
return fetch( 'https://typejoy.biz/pay/ppal_1025/EN/pay', {
method: 'POST',
body: JSON.stringify({
orderID: data.orderID,
payPp: 1
})
}); // return fetch( 'https://typejoy.biz/pay/ppal_1025/EN/pay
}); // return actions.order.capture().then( function(details
} // onApprove: function(data, actions
}).render('#payPp');
} // window.onload = function() {
</script>
在 clinet 侧控制台中,我可以记录 orderID,但我不明白如何将它传递到服务器端。获取功能如何工作?我应该使用其他类型的功能吗?如何以正确的方式做到这一点?
最佳答案
问题似乎是我的服务端既不处理 JSON 请求,也不生成 JSON 响应。官方文档提供了 fetch
的例子。
有两种解决方案 - 为获取创建适当的后端或使用像这样的不同方法:
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) {
//alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
//console.log('Transaction completed' ); alert(details);
console.log( 'data.orderID = ' + data.orderID );
document.getElementById('orderID').value = data.orderID;
console.log('data'); console.log(data);
console.log('Transaction completed by ' + details.payer.name.given_name );
var form = document.getElementById('ppalForm'); // $("#paymentForm");
var btn = document.createElement("input");
btn.type = "submit";
btn.id = "submitPp";
btn.value= 1;
form.appendChild(btn); // container.appendChild(input);
//form.append("<input hidden id='payPp' name='payPp' value='1' />");
console.log('form ='); console.log(form);
form.submit();
} ); // return actions.order.capture().then( function(details
} // onApprove: function(data, actions
}).render('#payPp');
主视图.php
<form id='ppalForm' method='POST' action='<?php echo $payPpFrmAction; ?>' >
<div id='payPp' >
//.. here goes form fields
</form>
Action .php
use PayBundle\actions\Order;
if( isset($_POST['payPp'] ) ) {
//if ( !count(debug_backtrace()) ) {
$ppalOrder = new GetOrder();
$ppalOrder->getOrder( $this->vdArr['orderID'], true);
//} // if ( !count(debug_backtrace())
} else {
$tokenPpal = Braintree_ClientToken::generate();
//TO BE PASSED TO VIEW TO THE FORM, TO BE FETCHED IN CLIENT SIDE
关于javascript - PayPal JavaScript SDK 客户端不发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55049164/