个人包/vovanmix / laravel5-billing-braintree
Laravel 5 的 Braintree 服务提供商
v1.0.7
2015-12-14 22:30 UTC
Requires
- php: >=5.3.0
- braintree/braintree_php: 3.6.x
- illuminate/support: 5.x
- laravel/framework: 5.x
README
安装
将以下行添加到 composer.json 中
"vovanmix/laravel5-billing-braintree": "1.*",
将以下内容添加到 config/app.php 的 "providers" 部分
"Vovanmix\Laravel5BillingBraintree\BraintreeServiceProvider",
就这样!
配置
在控制台中运行以下命令以发布包配置文件
php artisan vendor:publish
然后打开 config/billing_braintree.php
以设置环境和密钥
用法
获取客户端令牌
Billing::getClientToken()
在 Blade 模板中使用
{{ Billing::getClientToken() }}
获取计划摘要
$planExternalId = 'test'; $planAddOns = [1, 2]; // IDs of add ons, optional $planDiscounts = [1, 2]; // IDs of discounts, optional $removeAddOns = [3, 4]; // IDs of add ons that default for this plan but needed to be removed from it, optional $removeDiscounts = [3, 4]; // IDs of discounts that default for this plan but needed to be removed from it, optional $summary = \Billing::getPlanSummary($planExternalId, $planAddOns, $planDiscounts, $removeAddOns, $removeDiscounts);
创建客户
$messageBag = new MessageBag(); // catching errors is optional but is a good practice try { $customerData = [ 'first_name' => Input::get('first_name'), 'last_name' => Input::get('last_name'), 'nonce' => Input::get('nonce'), // payment method nonce, obtained at the front end using Braintree Javascript library. See more in Braintree Docs 'address' => Input::get('address'), 'city' => Input::get('city'), 'state' => Input::get('state'), 'zip' => Input::get('zip') ]; $customerId = \Billing::createCustomer($customerData); } catch (\Exception $e){ $messageBag->add('error', $e->getMessage()); }
创建订阅
$messageBag = new MessageBag(); try { $planExternalId = 'test'; $planAddOns = [1, 2]; // IDs of add ons, optional $planDiscounts = [1, 2]; // IDs of discounts, optional $removeAddOns = [3, 4]; // IDs of add ons that default for this plan but needed to be removed from it, optional $removeDiscounts = [3, 4]; // IDs of discounts that default for this plan but needed to be removed from it, optional $subscriptionId = \Billing::createSubscription($customerId, $planExternalId, $planAddOns, $planDiscounts, $removeAddOns, $removeDiscounts); } catch (\Exception $e){ $messageBag->add('error', $e->getMessage()); }
更新订阅支付方式
$customerData = [ 'first_name' => Input::get('first_name'), 'last_name' => Input::get('last_name'), 'nonce' => Input::get('nonce'), // payment method nonce, obtained at the front end using Braintree Javascript library. See more in Braintree Docs 'address' => Input::get('address'), 'city' => Input::get('city'), 'state' => Input::get('state'), 'zip' => Input::get('zip') ]; $success = \Billing::updatePaymentMethod($subscriptionId, $customerData);
获取订阅详情
$subscriptionInfo = \Billing::getSubscriptionInfo($subscriptionId);
检查
检查可以轻松获取订阅状态信息。这些方法实现了额外的逻辑,例如逾期处理和葡萄期
启用
\Billing::checkIfSubscriptionIsEnabled($subscriptionId);
最重要的检查。启用表示用户仍然可以使用订阅
对于以下状态返回 True
- 活动
- 已取消(直到宽限期结束)
- 逾期(如果配置允许)
活动
\Billing::checkIfSubscriptionIsActive($subscriptionId);
活动表示订阅可以在未来使用。已取消和已过期的订阅不能更新。
对于以下状态返回 True
- 活动
- 逾期
- 挂起
已成功计费
\Billing::checkIfSubscriptionWasSuccessfullyBilled($subscriptionId)
如果此订阅中至少有一次成功的付款,则返回 true
已支付
\Billing::checkIfSubscriptionIsPaid($subscriptionId);
已支付表示现在用户不需要欠款
对于以下状态返回 True
- 活动
- 挂起
逾期
\Billing::checkIfSubscriptionIsPastDue($subscriptionId);
如果订阅已逾期,则返回 true
宽限期
宽限期可以在此插件的配置中启用或禁用,参数 allowGracePeriod
。默认启用
如果启用,用户在取消订阅后可以使用订阅直到账单周期的结束。例如,如果支付日期是每月的10日,用户在11月10日支付了他的账单,并在11月11日取消了订阅,他仍然可以使用订阅直到12月10日。
可以使用订阅的能力意味着 checkIfSubscriptionIsEnabled()
方法将返回 TRUE
。
##逾期处理默认情况下,逾期订阅将被视为启用。您必须取消这些订阅才能阻止访问
这可以在配置中更改,参数 allowAccessForPastDue
。
取消订阅
\Billing::cancelSubscription($subscriptionId);