简体   繁体   English

Paypal REST API - 在 Laravel 中

[英]Paypal REST API - in Laravel

I'm new to this payment gateway integration.我是这个支付网关集成的新手。 In my project i'm using paypal recurring process.在我的项目中,我使用贝宝重复流程。 I perfectly completed Paypal integration.我完美地完成了 Paypal 整合。 And the Paypal IPN also completed. Paypal IPN 也完成了。 I used this code:我使用了这个代码:

$plan  = new Plan();
    $plan->setName('Mysite')
         ->setDescription('Paypal')
         ->setType('INFINITE');
    $paymentDefinition = new PaymentDefinition();
    $paymentDefinition->setName('Regular Payments')
            ->setType('REGULAR')
            ->setFrequency($plan_fr)
            ->setFrequencyInterval("1")
            ->setAmount(new Currency(array('value' => $item_price, 'currency' => 'USD')));
    $chargeModel = new ChargeModel();
    $chargeModel->setType('SHIPPING')
        ->setAmount(new Currency(array('value' => 0, 'currency' => 'USD')));

    $paymentDefinition->setChargeModels(array($chargeModel));
    $merchantPreferences = new MerchantPreferences();
    $merchantPreferences->setReturnUrl(ENV('PAYPAL_RETURN_URL'))
        ->setCancelUrl(ENV('PAYPAL_CANCEL_URL'))
        ->setAutoBillAmount("yes")
        ->setInitialFailAmountAction("CONTINUE")
        ->setMaxFailAttempts("0")
        ->setSetupFee(new Currency(array('value' => $item_price, 'currency' => 'USD')));

    $plan->setPaymentDefinitions(array($paymentDefinition));
    $plan->setMerchantPreferences($merchantPreferences);

    try {  $output = $plan->create($this->apiContext);  } catch (Exception $ex) { exit(1); }
    return $output;

This code includes : crate plan, update plan, create agreement, and execute agreement.此代码包括:crate 计划、更新计划、创建协议和执行协议。 This all code takes 12 seconds to load.. that is my problem.. How to reduce this timings.这所有代码加载需要 12 秒.. 那是我的问题.. 如何减少这个时间。 I don't know... Anyone know that?我不知道……有人知道吗?

Thanks advanced.谢谢先进。

Here is my Pay Pal REST API code.这是我的 Pay Pal REST API 代码。

        //Request Perms
        $cardtype = $request->cardtype;
        $account_number = $request->cardnumber;
        $expire_date =$request->expire_date;
        $cvv = $request->cvv;
        $plan_id =$request->plan_id;
        $amount = $request->amount;
        $userid = $request->user_id;
        $payment_type = $request->plan_type;
         
       //Genrate tokens
        $ch = curl_init();
        $clientId ='Your Client ID';
        $clientSecret= 'Your Secret ID';
        //you get Clientid and clientSecret  
           https://developer.paypal.com/developer/applications
        curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
        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, $clientId.":".$clientSecret);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

        $result = curl_exec($ch);

        if(empty($result))die("Error: No response.");
        else
        {
            $json = json_decode($result);
        }
        curl_close($ch);
                              
        $product_id = '';
         //you can create payment plan this link 
           https://www.sandbox.paypal.com/billing/plans

        if ($plan_id == 1) {
            $product_id = 'your plan id';
        }elseif ($plan_id == 2) {
            $product_id = 'your plan id'; 
        }
         
            $ch = curl_init();

                $payment_data = '{
                   "plan_id":"'.$product_id.'",
                   "start_time":"'.gmdate("Y-m-d\TH:i:s\Z",strtotime("+1 day")).'",
                   "shipping_amount":{
                      "currency_code":"USD",
                      "value":"'.$amount.'"
                   },
                   "subscriber":{
                      "name":{
                         "given_name":"",
                         "surname":""
                      },
                      "email_address":"'.$users->email.'",
                      "shipping_address":{
                         "name":{
                            "full_name":""
                         },
                         "address":{
                            "address_line_1":"",
                            "address_line_2":"",
                            "admin_area_2":"",
                            "admin_area_1":"",
                            "postal_code":"",
                            "country_code":"US"
                         }
                      },
                      "payment_source":{
                         "card":{
                            "number":"'.$account_number.'",
                            "expiry":"'. $expiry_date.'",
                            "security_code":"'.$cvv.'",
                            "name":"",
                            "billing_address":{
                               "address_line_1":"",
                               "address_line_2":"",
                               "admin_area_1":"",
                               "admin_area_2":"",
                               "postal_code":"",
                               "country_code":"US"
                            }
                         }
                      }
                   }
                }';

        curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/billing/subscriptions');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payment_data);
        $headers = array();
        $headers[] = 'Accept: application/json';
        $headers[] = 'Authorization: Bearer '.$json->access_token.'';
        $headers[] = 'Paypal-Request-Id: SUBSCRIPTION-'. rand() .'';
        $headers[] = 'Prefer: return=representation';
        $headers[] = 'Content-Type: application/json';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);    
        $payment_id = json_decode($result);
        $data =$headers[2];    
        $subid = substr($data, strpos($data, ":") + 2);

       //save data in database
        $payment = new Subscription(); 
        $payment->userid=$userid; 
        $payment->plan_id=$plan_id;    
        $payment->price=$amount;
        $payment->sub_id=$subid;
        $payment->transaction_id=$payment_id->id;
        $payment->payment_type='Paypal';  
        $payment->charge=$paypal_charge;
        $payment->plan_type=$plan_type;
        $payment->subscription_startdate= $subscription_startdate;
        $payment->subscription_enddate= $subscription_enddate;
        $payment->subscription_status= 'active';
        $payment->save();

        return response()->json(['status' => true,'message'=>'Payment has been successfully Done','data'=>$payment]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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