大家好,我正在使用PayPal结帐API从我的网站付款
这是我用来在服务器端捕获产品的代码,效果很好
<?php
require('../ini.php');
require 'vendor/autoload.php';
$ids = [
'id' => getSetting('paypalLiveKey','return',$con),
'secret' => getSetting('paypalSecretKey','return',$con)
];
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
$ids['id'],
$ids['secret']
)
);
$redirectUrls = (new \PayPal\Api\RedirectUrls())
->setReturnUrl('/include/paypalPHP/payProducts.php')
->setCancelUrl('/checkout.php');
$list = new \PayPal\Api\ItemList();
$item = (new \PayPal\Api\Item())
->setName('title')
->setPrice('100')
->setCurrency('USD')
->setQuantity('1');
$list->addItem($item);
$details = (new \PayPal\Api\Details())
->setSubtotal($total )
->setTax('0.00')
->setShipping('0.00')
->setShippingDiscount('0.00');
$amount = (new \PayPal\Api\Amount())
->setTotal('100')
->setCurrency("USD")
->setDetails($details);
$transaction = (new \PayPal\Api\Transaction())
->setItemList($list)
->setDescription('New Payment from ')
->setAmount($amount)
->setCustom('site');
$payer = (new \PayPal\Api\Payer())
->setPaymentMethod('paypal');
$payment = (new \PayPal\Api\Payment())
->setTransactions([$transaction])
->setIntent('sale')
->setRedirectUrls($redirectUrls)
->setPayer($payer);
try {
$payment->Create($apiContext);
// header("location: ".$payment->getApprovalLink());
echo json_encode([
'token' => $payment->getToken(),
]);
} catch (\PayPal\Exception\PayPalConnectionException $e){
header('loaction: '.ERROR_PAGE);
var_dump(json_decode($e->getData()));
}
我用来付款的第二个代码
<?php
require('../ini.php');
require 'vendor/autoload.php';
$ids = [
'id' => getSetting('paypalLiveKey','return',$con),
'secret' => getSetting('paypalSecretKey','return',$con)
];
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
$ids['id'],
$ids['secret']
)
);
$payment = \PayPal\Api\Payment::get($_POST['paymentID'],$apiContext);
$execution = (new \PayPal\Api\PaymentExecution())
->setPayerId($_POST['payerID'])
->setTransactions($payment->getTransactions());
try{
$payment->execute($execution,$apiContext);
// payment is done
echo json_encode([
'id' => $payment->getId()
]);
var_dump($payment->getTransactions()[0]->getCustom());
var_dump($payment);
}catch(\PayPal\Exeception\PayPalConnectionException $e){
header('loaction: '.ERROR_PAGE);
var_dump(json_decode($e->getData()));
}
我在客户端使用的代码来获取此代码
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return fetch('/include/paypalPHP/prepareProducts.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.token;
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return fetch('/include/paypalPHP/payProducts.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
paymentID: data.paymentID,
payerID: data.payerID
})
})
}
}).render('#paypal-button-container');
现在,如果我单击贝宝(Paypal)按钮,产品项目显示得很好,但是问题是当他在贝宝(PayPal)窗口上单击“付款”时,没有付款,我认为问题出在onApprove
功能上
请帮忙
答案 0 :(得分:0)
问题在于setReturnUrl
的某些部分与批准功能后的页面不应在同一页面上。所以您必须将其更改为其他内容