PayPal JavaScript SDK客户端未发布到服务器 [英] PayPal JavaScript SDK client side does not post to server
问题描述
我应该如何使用Paypal SDK将orderID发布到服务器?该示例代码在localhost或远程主机上均无效.我提交PayPal按钮后,帖子为空.
How i should post orderID to server using paypal SDK? The example code does not work neither on localhost nor on remote host. Post is empty after i submit PayPal button.
据此, https://developer.paypal.com/docs/checkout /integrate/#6-验证交易
PayBundle \ Resources \ actions \ ppal \ ppalAction_brt_stkovr.php
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,但是我不知道如何将其传递到服务器端.提取功能如何工作?我应该使用其他类型的功能吗?如何以正确的方式做到这一点?
In clinet side console i can log orderID, but i do not understand how to pass it to the server side. How fetch function works? shall i use other type of function? How to make this in correct way?
推荐答案
问题似乎是我的服务方既不处理JSON请求,也不生成JSON响应.官方文档提供了带有fetch
的示例.
The problem seems is that my serer side does nor deal with JSON requests, nor generate JSON responses. The official documentation provides example with fetch
.
有两种解决方案-制作适当的后端以进行提取或使用其他类似的方法:
There are two solutions - make proper back-end for fetch or use different approach like this:
paypal.Buttons({
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');
}).render('#payPp');
main_view.php
main_view.php
<form id='ppalForm' method='POST' action='<?php echo $payPpFrmAction; ?>' >
<div id='payPp' >
//.. here goes form fields
</form>
action.php
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 }
这篇关于PayPal JavaScript SDK客户端未发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!