Implement Payum/Laravel recurring payment

后端 未结 3 689
抹茶落季
抹茶落季 2021-01-17 09:35

I have some issues trying to get this working, I\'ve implemented the checkout express (or seems to be) successfully, but also my system needs subscription option, following

3条回答
  •  死守一世寂寞
    2021-01-17 10:32

    Here is my Pay Pal REST API code.

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

    It's working fine for me.

提交回复
热议问题