dptsivmg82908 2018-11-13 16:34
浏览 44
已采纳

处理卷曲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)

  • 写回答

1条回答 默认 最新

  • douzizang7783 2018-11-13 21:22
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型