《PHP實例:Paypal實現循環扣款(訂閱)功能》要點:
本文介紹了PHP實例:Paypal實現循環扣款(訂閱)功能,希望對您有用。如果有疑問,可以聯系我們。
起因PHP編程
業務需求要集成Paypal,實現循環扣款功能,然而百度和GOOGLE了一圈,除官網外,沒找到相關開發教程,只好在Paypal上看,花了兩天后集成成功,這里對如何使用Paypal的支付接口做下總結.PHP編程
Paypal現在有多套接口:PHP編程
Braintree的接口PHP編程
Braintree是Paypal收購的一家公司,它除了支持Paypal的支付外,還提供了升級計劃,信用卡,客戶信息等一系列全套的管理,使用上更方便;這些功能Paypal第二套REST接口其實也集成了大部分,但是Paypal的Dashboard不能直接管理這些信息而Braintree可以,所以我其實我更愿意用Braintree.關鍵是我使用的后端框架是Laravel,它的cashier解決方案默認可以支持Braintee,所以這套接口是我的首選.但是當我把它的功能都實現后發現一個蛋疼的問題:Braintree在國內不支持......卒...PHP編程
REST APIPHP編程
這是順應時代發展的產物,如果你之前用過OAuth 2.0與REST API,那看這些接口應該不會有什么困惑.PHP編程
舊接口PHP編程
除非REST API接口有不能滿足的,比如政策限制,否則不推薦使用.全世界都在往OAuth 2.0的認證方式和REST API的API使用方式遷移,干嘛逆勢而行呢.因此在REST API能解決問題情況下,我也沒對這套接口做深入比較.PHP編程
REST API的介紹PHP編程
官方的API參考文檔https://developer.paypal.com/webapps/developer/docs/api/對于其API和使用方式有較詳細的介紹,但是如果自己直接調這些API還是很繁瑣的,同時我們只想盡快完成業務要求而不是陷入對API的深入了解.PHP編程
那么如何開始呢,建議直接安裝官方提供的PayPal-PHP-SDK,通過其Wiki作為起點.PHP編程
在完成首個例子之前,請確保你有Sandbox帳號,并正確配置了:PHP編程
在完成Wiki的首個例子后,理解下接口的分類有助于完成你的業務需求,下面我對接口分類做個介紹,請結合例子理解http://paypal.github.io/PayPal-PHP-SDK/sample/#payments.PHP編程
如何實現循環扣款PHP編程
分四個步驟:PHP編程
1.創建升級計劃PHP編程
升級計劃對應Plan這個類.這一步有幾個注意點:PHP編程
以創建一個Standard的計劃為例,其參數如下:PHP編程
$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" ];
創建并激活計劃代碼如下:PHP編程
//上面的$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的網站等待用戶同意PHP編程
Plan創建后,要怎么讓用戶訂閱呢,其實就是創建Agreement,關于Agreement,有以下注意點:PHP編程
例子參數如下:PHP編程
$param = [ 'id' => 'P-26T36113JT475352643KGIHY',//上一步創建Plan時生成的ID 'name' => 'Standard', 'desc' => 'Standard Plan for one month' ];
代碼如下:PHP編程
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的網站等待用戶支付.PHP編程
用戶同意后,執行訂閱PHP編程
用戶同意后,訂閱還未完成,必須執行Agreement的execute方法才算完成真正的訂閱.這一步的注意點在于PHP編程
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/1333.html