处理curl PayPal API结帐

时间:2018-11-13 16:34:34

标签: php curl

我正在尝试在网站上实现PayPal Checkout服务器端 所以我试图分期付款并获得付款ID 但是我在卷发时遇到了麻烦

文档示例使用node.js

https://petercollingridge.appspot.com/svg-editor

这是我的代码:

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);

}

但我只是得到bool(false)

1 个答案:

答案 0 :(得分:0)

更新

我刚刚弄清楚如何实现,阅读了有关cURL的更多信息,并且效果很好:

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);

}}

和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');