起因
業務需求要集成Paypal,實現循環扣款功能,然而百度和GOOGLE了一圈,除官網外,沒找到相關開發教程,只好在Paypal上看,花了兩天后集成成功,這里對如何使用Paypal的支付接口做下總結。
Paypal現在有多套接口:
Braintree的接口
Braintree是Paypal收購的一家公司,它除了支持Paypal的支付外,還提供了升級計劃,信用卡,客戶信息等一系列全套的管理,使用上更方便;這些功能Paypal第二套REST接口其實也集成了大部分,但是Paypal的Dashboard不能直接管理這些信息而Braintree可以,所以我其實我更愿意用Braintree。關鍵是我使用的后端框架是Laravel,它的cashier解決方案默認可以支持Braintee,所以這套接口是我的首選。但是當我把它的功能都實現后發現一個蛋疼的問題:Braintree在國內不支持。。。。。。卒。。。
REST API
這是順應時代發展的產物,如果你之前用過OAuth 2.0與REST API,那看這些接口應該不會有什么困惑。
舊接口
除非REST API接口有不能滿足的,比如政策限制,否則不推薦使用。全世界都在往OAuth 2.0的認證方式和REST API的API使用方式遷移,干嘛逆勢而行呢。因此在REST API能解決問題情況下,我也沒對這套接口做深入比較。
REST API的介紹
官方的API參考文檔https://developer.paypal.com/webapps/developer/docs/api/對于其API和使用方式有較詳細的介紹,但是如果自己直接調這些API還是很繁瑣的,同時我們只想盡快完成業務要求而不是陷入對API的深入了解。
那么如何開始呢,建議直接安裝官方提供的PayPal-PHP-SDK,通過其Wiki作為起點。
在完成首個例子之前,請確保你有Sandbox帳號,并正確配置了:
在完成Wiki的首個例子后,理解下接口的分類有助于完成你的業務需求,下面我對接口分類做個介紹,請結合例子理解http://paypal.github.io/PayPal-PHP-SDK/sample/#payments。
如何實現循環扣款
分四個步驟:
1.創建升級計劃
升級計劃對應Plan這個類。這一步有幾個注意點:
以創建一個Standard的計劃為例,其參數如下:
$param = [ "name" => "standard_monthly", "display_name" => "Standard Plan", "desc" => "standard Plan for one month", "type" => "REGULAR", "frequency" => "MONTH", "frequency_interval" => 1, "cycles" => 0, "amount" => 20, "currency" => "USD" ];
創建并激活計劃代碼如下:
 //上面的$param例子是個數組,我的實際應用傳入的實際是個對象,用戶理解下就好。 public function createPlan($param) { $apiContext = $this->getApiContext(); $plan = new Plan(); // # Basic Information // Fill up the basic information that is required for the plan $plan->setName($param->name) ->setDescription($param->desc) ->setType('INFINITE');//例子總是設置為無限循環 // # Payment definitions for this billing plan. $paymentDefinition = new PaymentDefinition(); // The possible values for such setters are mentioned in the setter method documentation. // Just open the class file. e.g. lib/PayPal/Api/PaymentDefinition.php and look for setFrequency method. // You should be able to see the acceptable values in the comments. $paymentDefinition->setName($param->name) ->setType($param->type) ->setFrequency($param->frequency) ->setFrequencyInterval((string)$param->frequency_interval) ->setCycles((string)$param->cycles) ->setAmount(new Currency(array('value' => $param->amount, 'currency' => $param->currency))); // Charge Models $chargeModel = new ChargeModel(); $chargeModel->setType('TAX') ->setAmount(new Currency(array('value' => 0, 'currency' => $param->currency))); $returnUrl = config('payment.returnurl'); $merchantPreferences = new MerchantPreferences(); $merchantPreferences->setReturnUrl("$returnUrl?success=true") ->setCancelUrl("$returnUrl?success=false") ->setAutoBillAmount("yes") ->setInitialFailAmountAction("CONTINUE") ->setMaxFailAttempts("0") ->setSetupFee(new Currency(array('value' => $param->amount, 'currency' => 'USD'))); $plan->setPaymentDefinitions(array($paymentDefinition)); $plan->setMerchantPreferences($merchantPreferences); // For Sample Purposes Only. $request = clone $plan; // ### Create Plan try { $output = $plan->create($apiContext); } catch (Exception $ex) { return false; } $patch = new Patch(); $value = new PayPalModel('{"state":"ACTIVE"}'); $patch->setOp('replace') ->setPath('/') ->setValue($value); $patchRequest = new PatchRequest(); $patchRequest->addPatch($patch); $output->update($patchRequest, $apiContext); return $output; }2.創建訂閱(創建Agreement),然后將跳轉到Paypal的網站等待用戶同意
Plan創建后,要怎么讓用戶訂閱呢,其實就是創建Agreement,關于Agreement,有以下注意點:
例子參數如下:
$param = [ 'id' => 'P-26T36113JT475352643KGIHY',//上一步創建Plan時生成的ID 'name' => 'Standard', 'desc' => 'Standard Plan for one month'];
代碼如下:
 public function createPayment($param) { $apiContext = $this->getApiContext(); $agreement = new Agreement(); $agreement->setName($param['name']) ->setDescription($param['desc']) ->setStartDate(Carbon::now()->addMonths(1)->toIso8601String()); // Add Plan ID // Please note that the plan Id should be only set in this case. $plan = new Plan(); $plan->setId($param['id']); $agreement->setPlan($plan); // Add Payer $payer = new Payer(); $payer->setPaymentMethod('paypal'); $agreement->setPayer($payer); // For Sample Purposes Only. $request = clone $agreement; // ### Create Agreement try { // Please note that as the agreement has not yet activated, we wont be receiving the ID just yet. $agreement = $agreement->create($apiContext); // ### Get redirect url // The API response provides the url that you must redirect // the buyer to. Retrieve the url from the $agreement->getApprovalLink() // method $approvalUrl = $agreement->getApprovalLink(); } catch (Exception $ex) { return "create payment failed, please retry or contact the merchant."; } return $approvalUrl;//跳轉到$approvalUrl,等待用戶同意 }函數執行后返回$approvalUrl,記得通過redirect($approvalUrl)跳轉到Paypal的網站等待用戶支付。
用戶同意后,執行訂閱
用戶同意后,訂閱還未完成,必須執行Agreement的execute方法才算完成真正的訂閱。這一步的注意點在于
代碼片段如下:
 public function onPay($request) { $apiContext = $this->getApiContext(); if ($request->has('success') && $request->success == 'true') { $token = $request->token; $agreement = new /PayPal/Api/Agreement(); try { $agreement->execute($token, $apiContext); } catch(/Exception $e) { return ull; return $agreement; } return null; }獲取交易記錄
訂閱后,可能不會立刻產生交易扣費的交易記錄,如果為空則過幾分鐘再次嘗試。本步驟注意點:
 /** 獲取交易記錄 * @param $id subscription payment_id * @warning 總是獲取該subscription的所有記錄 */ public function transactions($id) { $apiContext = $this->getApiContext(); $params = ['start_date' => date('Y-m-d', strtotime('-15 years')), 'end_date' => date('Y-m-d', strtotime('+5 days'))]; try { $result = Agreement::searchTransactions($id, $params, $apiContext); } catch(/Exception $e) { Log::error("get transactions failed" . $e->getMessage()); return null; } return $result->getAgreementTransactionList() ; }最后,Paypal官方當然也有對應的教程,不過是調用原生接口的,跟我上面流程不一樣點在于只說了前3步,供有興趣的參考:https://developer.paypal.com/docs/integration/direct/billing-plans-and-agreements/。
需要考慮的問題
功能是實現了,但是也發現不少注意點:
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!
新聞熱點
疑難解答
圖片精選