简体   繁体   中英

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

documentation examples use node.js

Implement a PayPal Checkout Server Integration

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)

UPDATE

I just figured out how to implement, reading more about cURL and it works very well:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM