【问题标题】:Implement Payum/Laravel recurring payment实施 Payum/Laravel 定期付款
【发布时间】:2014-12-10 23:22:19
【问题描述】:

我在尝试使其正常工作时遇到了一些问题,我已经成功实施了结帐快递(或似乎是),但我的系统也需要订阅选项,遵循此 example

现在,我的问题是,在 Laravel 中你不能简单地放置一些随机文件,所以我试图以正确的方式来做,遗憾的是,库中没有包含类和方法的文档。

我在控制器中创建了一些功能(我不知道这是否正确)我现在面临的问题是尝试 createRecurringPayment() 以应用所需的定期付款金额,这是最后一步我猜。

感谢您的帮助。

  • app/controllers/PaypalController.php

    public function prepareExpressCheckout(){
        $storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
        $details = $storage->createModel();
        $details['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD';
        $details['PAYMENTREQUEST_0_AMT'] = 1.23;
        $storage->updateModel($details);
        $captureToken = $this->getTokenFactory()->createCaptureToken('paypal_es', $details, 'payment_done');
        $details['RETURNURL'] = $captureToken->getTargetUrl();
        $details['CANCELURL'] = $captureToken->getTargetUrl();
        $storage->updateModel($details);
        return \Redirect::to($captureToken->getTargetUrl());
    }
    
    public function prepareSubscribe(){
        $storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
        $details = $storage->createModel();
    
        $details['PAYMENTREQUEST_0_AMT'] = 0;
        $details['L_BILLINGTYPE0'] = Api::BILLINGTYPE_RECURRING_PAYMENTS;
        $details['L_BILLINGAGREEMENTDESCRIPTION0'] = "Suscripción por X meses";
        $details['NOSHIPPING'] = 1;
    
        $storage->updateModel($details);
        $captureToken = $this->getTokenFactory()->createCaptureToken('paypal_es', $details, 'payment_done');
        $storage->updateModel($details);
    
        return \Redirect::to($captureToken->getTargetUrl());
    }
    
    public function createRecurringPayment(){
        $payum_token = Input::get('payum_token');
        $request = \App::make('request');
        $request->attributes->set('payum_token', $payum_token);
        $token = ($request);
        //$this->invalidate($token);
    
        $agreementStatus = new GetHumanStatus($token);
        $payment->execute($agreementStatus);
    
        if (!$agreementStatus->isSuccess()) {
            header('HTTP/1.1 400 Bad Request', true, 400);
            exit;
        }
    
        $agreementDetails = $agreementStatus->getModel();
    
        $storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
    
        $recurringPaymentDetails = $storage->createModel();
        $recurringPaymentDetails['TOKEN'] = $agreementDetails['TOKEN'];
        $recurringPaymentDetails['DESC'] = 'Subscribe to weather forecast for a week. It is 0.05$ per day.';
        $recurringPaymentDetails['EMAIL'] = $agreementDetails['EMAIL'];
        $recurringPaymentDetails['AMT'] = 0.05;
        $recurringPaymentDetails['CURRENCYCODE'] = 'USD';
        $recurringPaymentDetails['BILLINGFREQUENCY'] = 7;
        $recurringPaymentDetails['PROFILESTARTDATE'] = date(DATE_ATOM);
        $recurringPaymentDetails['BILLINGPERIOD'] = Api::BILLINGPERIOD_DAY;
    
        $payment->execute(new CreateRecurringPaymentProfile($recurringPaymentDetails));
        $payment->execute(new Sync($recurringPaymentDetails));
    
        $doneToken = $this->createToken('paypal_es', $recurringPaymentDetails, 'payment_done');
    
        return \Redirect::to($doneToken->getTargetUrl());
    }
    
  • app/routes.php

        Route::get('/payment', array('as' => 'payment', 'uses' => 'PaymentController@payment'));
        Route::get('/payment/done', array('as' => 'payment_done', 'uses' => 'PaymentController@done'));
        Route::get('/payment/paypal/express-checkout/prepare', array('as' => 'paypal_es_prepare', 'uses' => 'PaypalController@prepareExpressCheckout'));
        Route::get('/payment/paypal/subscribe/prepare', array('as' => 'paypal_re_prepare', 'uses' => 'PaypalController@prepareSubscribe'));
        Route::get('/payment/paypal/subscribe/create', array('as' => 'payment_create', 'uses' => 'PaypalController@createRecurringPayment'));
    

【问题讨论】:

  • “你不能简单地放一些随机文件”是什么意思你得到了什么错误?
  • 我的意思是,在 Laravel 中,您必须将文件放在特定文件夹(模型、控制器等)中。我试图通过这个插件遵循该结构。
  • 显然没有定期付款的例子。我猜@maksim-kotlyar 在开发团队中?
  • plain php 有一个例子。它可以很容易地被 Laravel 采用。不需要专门的 laravel 教程。
  • 你有什么错误吗?在 laravel 中,您可以在目录中添加类(例如:库)并使用命名空间访问函数(您必须在使用前自动加载目录)。

标签: php laravel paypal payum


【解决方案1】:

在我的项目中,我使用了以下库,它对我有很大帮助:

https://github.com/amirduran/duranius-paypal-rest-api-php-library

这里有一些功能:

  • 易于安装 - 只需一个文件
  • 库被实现为 PHP 类
  • 它支持定期付款它支持 ExpressCheckout 付款
  • 所有可用的 PayPal API 方法都包含在所属方法中
  • 有据可查

【讨论】:

    【解决方案2】:

    我发现了问题。它与我们传递给创建循环支付功能的参数一起使用。这是协议和付款创建的功能。它应该可以正常工作。

    <?php
    
    namespace App\Http\Controllers;
    
    use Payum\Core\Request\GetHumanStatus;
    use Payum\LaravelPackage\Controller\PayumController;
    use Payum\Paypal\ExpressCheckout\Nvp\Api;
    use Payum\Core\Request\Sync;
    use Payum\Paypal\ExpressCheckout\Nvp\Request\Api\CreateRecurringPaymentProfile;
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    
    class PayPalController extends PayumController {
    
        public function prepareSubscribeAgreement() {
    
            $storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
    
            $details = $storage->create();
            $details['PAYMENTREQUEST_0_AMT'] = 0;
            $details['L_BILLINGTYPE0'] = Api::BILLINGTYPE_RECURRING_PAYMENTS;
            $details['L_BILLINGAGREEMENTDESCRIPTION0'] = "Weather subscription";
            //$details['NOSHIPPING'] = 1;
            $storage->update($details);
    
            $captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken('paypal_ec', $details, 'paypal_subscribe');
    
            return \Redirect::to($captureToken->getTargetUrl());
        }
    
        public function createSubscribePayment(Request $request) {
            $request->attributes->set('payum_token', $request->input('payum_token'));
    
            $token = $this->getPayum()->getHttpRequestVerifier()->verify($request);
            $gateway = $this->getPayum()->getGateway($token->getGatewayName());
    
            $agreementStatus = new GetHumanStatus($token);
            $gateway->execute($agreementStatus);
    
            if (!$agreementStatus->isCaptured()) {
                header('HTTP/1.1 400 Bad Request', true, 400);
                exit;
            }
    
            $agreement = $agreementStatus->getModel();
    
            $storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
    
            $recurringPayment = $storage->create();
            $recurringPayment['TOKEN'] = $agreement['TOKEN'];
            $recurringPayment['PAYERID'] = $agreement['PAYERID'];
            $recurringPayment['PROFILESTARTDATE'] = date(DATE_ATOM);
            $recurringPayment['DESC'] = $agreement['L_BILLINGAGREEMENTDESCRIPTION0'];
            $recurringPayment['BILLINGPERIOD'] = Api::BILLINGPERIOD_DAY;
            $recurringPayment['BILLINGFREQUENCY'] = 7;
            $recurringPayment['AMT'] = 0.05;
            $recurringPayment['CURRENCYCODE'] = 'USD';
            $recurringPayment['COUNTRYCODE'] = 'US';
            $recurringPayment['MAXFAILEDPAYMENTS'] = 3;
    
            $gateway->execute(new CreateRecurringPaymentProfile($recurringPayment));
            $gateway->execute(new Sync($recurringPayment));
    
            $captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken('paypal_ec', $recurringPayment, 'payment_done');
    
            return \Redirect::to($captureToken->getTargetUrl());
        }
    
        public function done(Request $request) {
            /** @var Request $request */
            //$request = \App::make('request');
            $request->attributes->set('payum_token', $request->input('payum_token'));
    
            $token = $this->getPayum()->getHttpRequestVerifier()->verify($request);
            $gateway = $this->getPayum()->getGateway($token->getGatewayName());
    
            $gateway->execute($status = new GetHumanStatus($token));
    
            return \Response::json(array(
                        'status' => $status->getValue(),
                        'details' => iterator_to_array($status->getFirstModel())
            ));
        }
    
    }
    

    路线:

    Route::get('paypal/agreement', 'PayPalController@prepareSubscribeAgreement');
        Route::get('paypal/subscribe', [
            'as' => 'paypal_subscribe',
            'uses' => 'PayPalController@createSubscribePayment'
        ]);
        Route::get('paydone', [
            'as' => 'payment_done',
            'uses' => 'PayPalController@done'
        ]);
    

    只需打开 www.example.com/paypal/agreement,它就会将您带到 PayPal

    【讨论】:

    • amer,感谢您分享您的代码。由于缺乏文档,我和许多其他人也面临着很多问题。如果你已经成功地将这个包集成到 laravel 中,那么我建议你创建一个 git repo 并分享一个有效的 paypal demo + ipn 解析器。那肯定会让我很开心,非常感谢:)
    【解决方案3】:

    这是我的 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]);
    

    对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 2014-06-27
      • 1970-01-01
      • 2015-08-23
      • 2023-03-31
      • 2020-01-03
      相关资源
      最近更新 更多