|
1 |
| -# Subscription |
| 1 | +## Usage |
2 | 2 |
|
3 |
| -lara-stripe has `LaraStripeSubs` alias for subscription related task. |
| 3 | +Here are some common operations you can perform using the `StripeSubscription` library: |
4 | 4 |
|
5 |
| -# Methods |
6 |
| - |
7 |
| -## `setup()` `required` |
8 |
| - |
9 |
| -setup method as usual setup secret key |
10 |
| - |
11 |
| -* `secret_key` `required` |
12 |
| - |
13 |
| -```php |
14 |
| -setup([ |
15 |
| - 'secret_key' => '********' |
16 |
| -]) |
17 |
| -``` |
18 |
| - |
19 |
| -## `customer()` `required` |
20 |
| - |
21 |
| -which customer subscribe. |
22 |
| - |
23 |
| -* [create customer first](https://github.com/code4mk/lara-stripe/blob/master/doc/customer.md) |
24 |
| - |
25 |
| -```php |
26 |
| -customer('customer_id') |
27 |
| -``` |
28 |
| - |
29 |
| -## `plan()` `required` |
30 |
| - |
31 |
| -Customer subscription which plan. |
32 |
| - |
33 |
| -* [create a plan first](https://github.com/code4mk/lara-stripe/blob/master/doc/customer.md) |
34 |
| - |
35 |
| -```php |
36 |
| -plan('plan_id') |
37 |
| -``` |
38 |
| - |
39 |
| -## `trialPlan()` `optional` |
40 |
| - |
41 |
| -If want to set trial period from plan. |
| 5 | +### Creating a Subscription |
42 | 6 |
|
43 | 7 | ```php
|
44 |
| -trialPlan() |
45 |
| -``` |
| 8 | +use Code4mk\LaraStripe\Lib\StripeSubscription; |
46 | 9 |
|
47 |
| -## `trail()` `optional` |
| 10 | +$stripeSubscription = new StripeSubscription(); |
48 | 11 |
|
49 |
| -Set tril period for the subscription. This override the plan trail. |
| 12 | +$customerId = 'customer_id_here'; |
| 13 | +$priceId = 'price_id_here'; |
50 | 14 |
|
51 |
| -```php |
52 |
| -trial(5) |
| 15 | +$stripeSubscription->customer($customerId) // Set the customer ID |
| 16 | + ->priceId($priceId) // Set the price (plan) ID |
| 17 | + ->metaData(['key' => 'value']) // Set additional metadata (optional) |
| 18 | + ->trialPlan() // Use the default trial from the plan (optional) |
| 19 | + ->trial(7) // Set a custom trial period in days (optional) |
| 20 | + ->source('card_source_id') // Set the card source (optional) |
| 21 | + ->coupon('coupon_code_here') // Apply a coupon (optional) |
| 22 | + ->create(); // Create the subscription |
53 | 23 | ```
|
54 | 24 |
|
55 |
| -## `source()` `optional` |
56 |
| - |
57 |
| -set source (card token generate from stipe.js). |
| 25 | +### Retrieving a Subscription |
58 | 26 |
|
59 | 27 | ```php
|
60 |
| -source('tok_***') |
61 |
| -``` |
| 28 | +use Code4mk\LaraStripe\Lib\StripeSubscription; |
62 | 29 |
|
63 |
| -## `coupon()` `optioanl` |
| 30 | +$stripeSubscription = new StripeSubscription(); |
64 | 31 |
|
65 |
| -Subscription with a coupon |
| 32 | +$subscriptionId = 'subscription_id_here'; |
66 | 33 |
|
67 |
| -```php |
68 |
| -coupon('coupon_code') |
| 34 | +$stripeSubscription->retrieve($subscriptionId); // Retrieve the subscription by ID |
69 | 35 | ```
|
70 | 36 |
|
71 |
| -## extra `optional` |
72 |
| - |
73 |
| -Add optional attributes which are not include this package for creating subscription. |
74 |
| - |
75 |
| -* [all attributes lists](https://stripe.com/docs/api/subscriptions/create) |
| 37 | +### Canceling a Subscription |
76 | 38 |
|
77 | 39 | ```php
|
78 |
| -extra([ |
79 |
| - ..... |
80 |
| -]) |
81 |
| -``` |
| 40 | +use Code4mk\LaraStripe\Lib\StripeSubscription; |
82 | 41 |
|
83 |
| -## `get()` `required` |
| 42 | +$stripeSubscription = new StripeSubscription(); |
84 | 43 |
|
85 |
| -* create the subscription and retrive created subscription data. |
86 |
| -* return `object` |
| 44 | +$subscriptionId = 'subscription_id_here'; |
87 | 45 |
|
88 |
| -```php |
89 |
| -get() |
| 46 | +$stripeSubscription->cancel($subscriptionId); // Cancel the subscription by ID |
90 | 47 | ```
|
91 | 48 |
|
92 |
| -# `Create a Subscription -> Full code` |
93 |
| - |
| 49 | +For more details on available methods and parameters, refer to the inline code comments and the [Stripe API documentation](https://stripe.com/docs/api/subscriptions). |
94 | 50 |
|
95 |
| -```php |
96 |
| -LaraStripeSubs::setup(['secret_key'=>'******']) |
97 |
| - ->customer('customer_id') |
98 |
| - ->plan('plan_id') |
99 |
| - ->source('tok_***') |
100 |
| - ->trialPlan() |
101 |
| - ->coupon('coupon_id') |
102 |
| - ->get(); |
103 |
| -``` |
| 51 | +## Error Handling |
104 | 52 |
|
105 |
| -# `Retrieve the subscription` |
| 53 | +The library provides basic error handling. If an error occurs during an operation, it returns an object with an `isError` property set to `true`, along with an error message. |
106 | 54 |
|
107 | 55 | ```php
|
108 |
| -LaraStripeSubs::setup(['secret_key'=>'******']) |
109 |
| - ->retrieve('subs_id') |
110 |
| -``` |
111 |
| - |
112 |
| -# `Cancel the subscription` |
| 56 | +$result = $stripeSubscription->create(); |
113 | 57 |
|
114 |
| -```php |
115 |
| -LaraStripeSubs::setup(['secret_key'=>'******']) |
116 |
| - ->cancel('subs_id') |
117 |
| -``` |
| 58 | +if ($result->isError === true) { |
| 59 | + // Handle the error |
| 60 | + echo "Error: " . $result->message; |
| 61 | +} |
| 62 | +``` |
0 commit comments