[英]Handle curl PayPal API Checkout
i'm trying to implement PayPal Checkout server side on a web site so im trying to septup the payment and get the payment ID but im having troubles whth the curl 我正在尝试在网站上实现PayPal Checkout服务器端,因此我试图隔离付款并获取付款ID,但我在卷曲时遇到了麻烦
documentation examples use node.js 文档示例使用node.js
Implement a PayPal Checkout Server Integration 实施PayPal Checkout服务器集成
this is my code: 这是我的代码:
public function Setupthepayment($total_boletos){
$paypalURL = "https://api.sandbox.paypal.com";
$paypalClientID = 'xxx';
$paypalSecret = 'xxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypalURL."/v1/payments/payment");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $paypalClientID.":".$paypalSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$data = array('intent' => 'sale',
'payer' => array('payment_method' => 'paypal'),
'transactions' => array('amount' => array('total' => $total_boletos, 'currency' => 'MXN')));
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if(empty($response)){
return false;
}else{
// Transaction data
$result = json_decode($response);
return $result;
}
curl_close($ch);
}
but im just getting bool(false) 但是我只是在布尔(false)
UPDATE UPDATE
I just figured out how to implement, reading more about cURL and it works very well: 我只是想出了实现方法,请阅读有关cURL的更多信息,它工作得很好:
php: PHP:
class PayPalCheckout{
public function executeThePayment($paymentID,$payerID,$total_boletos){
$paypalURL = "https://api.sandbox.paypal.com";
$paypalClientID = 'xxx';
$paypalSecret = 'xxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypalURL."/v1/payments/payment/".$paymentID."/execute");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $paypalClientID.":".$paypalSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = '{
"payer_id": "'.$payerID.'",
"transactions":[
{
"amount":{
"total":'.$total_boletos.',
"currency":"MXN"
}
}
]
}';
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($ch);
if(empty($response)){
return false;
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($httpStatusCode);
}else{
// Transaction data
return $response;
/*$result = json_decode($response, true);
return $result['id'];*/
}
curl_close($ch);
}
public function Setupthepayment($total_boletos){
$paypalURL = "https://api.sandbox.paypal.com";
$paypalClientID = 'xxx';
$paypalSecret = 'xxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypalURL."/v1/payments/payment");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $paypalClientID.":".$paypalSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = '{
"intent":"sale",
"redirect_urls":{
"return_url":"http://localhost:8888/index.php",
"cancel_url":"http://localhost:8888/index.php"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":'.$total_boletos.',
"currency":"MXN"
}
}
]
}';
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($ch);
if(empty($response)){
return false;
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($httpStatusCode);
}else{
// Transaction data
return $response;
/*$result = json_decode($response, true);
return $result['id'];*/
}
curl_close($ch);
}}
and JS 和JS
paypal.Button.render({
env: ambiente, // Or 'production'
// Set up the payment:
// 1. Add a payment callback
payment: function(data, actions) {
// 2. Make a request to your server
return actions.request.post('funciones/php/configurar_pago_paypal.php')
.then(function(res) {
if (ambiente == 'sandbox') {
console.log(res);
}
// 3. Return res.id from the response
return res.id;
});
},
// Execute the payment:
// 1. Add an onAuthorize callback
onAuthorize: function(data, actions) {
// 2. Make a request to your server
return actions.request.post('funciones/php/ejecutar_pago_paypal.php', {
paymentID: data.paymentID,
payerID: data.payerID
})
.then(function(res) {
if (ambiente == 'sandbox') {
console.log(res);
}
if (res == "Aprobado") {
} else{
alert('Error tu pago no fue procesado.');
}
});
} }, '#paypal-button');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.