《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.vephp.com/jiaocheng/1333.html