簡體   English   中英

處理curl PayPal API結帳

[英]Handle curl PayPal API Checkout

我正在嘗試在網站上實現PayPal Checkout服務器端,因此我試圖隔離付款並獲取付款ID,但我在卷曲時遇到了麻煩

文檔示例使用node.js

實施PayPal Checkout服務器集成

這是我的代碼:

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

}

但是我只是在布爾(false)

UPDATE

我只是想出了實現方法,請閱讀有關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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM