PHP100 2019-03-28
起因
业务需求要集成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/。
需要考虑的问题
功能是实现了,但是也发现不少注意点: