|
1 |
| -# Customer |
| 1 | + ## Usage |
2 | 2 |
|
3 |
| -`customer` for get future payment as like `subscription fee`. |
| 3 | +Here's how you can use the Stripe Customer Library to perform various customer-related actions in your Laravel application. |
4 | 4 |
|
5 |
| -LaraStripe has LaraStripeCustomer alias for customer related task. |
6 |
| - |
7 |
| -# create new customer |
8 |
| - |
9 |
| -## methods |
10 |
| - |
11 |
| -### setup |
12 |
| - |
13 |
| -Set secret key . |
14 |
| - |
15 |
| -* `secret_key` required |
| 5 | +### Initialization |
16 | 6 |
|
17 | 7 | ```php
|
18 |
| -LaraStripeCustomer::setup([ |
19 |
| - 'secret_key'=>'sk_test_mBGoFuccDy2KCD4pobbaixKK00qUW0ghu1' |
20 |
| -]) |
| 8 | +use Code4mk\LaraStripe\Lib\StripeCustomer; |
21 | 9 |
|
| 10 | +// Initialize the Stripe Customer instance |
| 11 | +$stripeCustomer = new StripeCustomer(); |
22 | 12 | ```
|
23 | 13 |
|
24 |
| -### create |
25 |
| - |
26 |
| -Customer important data as like source(card token),email |
| 14 | +### Creating a Customer |
27 | 15 |
|
28 |
| -* `source` required |
29 |
| -* [see more on stripe](https://stripe.com/docs/api/customers/create) |
| 16 | +You can create a new customer by specifying their name, email, and optional metadata. |
30 | 17 |
|
31 | 18 | ```php
|
32 |
| -LaraStripeCustomer::create([ |
33 |
| - ['source' => 'card token','email' => ' [email protected]'] |
34 |
| -]) |
| 19 | +$customer = $stripeCustomer |
| 20 | + ->name($customerName) |
| 21 | + ->email($customerEmail) |
| 22 | + ->metadata($metadata) |
| 23 | + ->create(); |
| 24 | + |
| 25 | +if ($customer->isError) { |
| 26 | + // Handle errors |
| 27 | + echo $customer->message; |
| 28 | +} else { |
| 29 | + // Customer created successfully |
| 30 | +} |
35 | 31 | ```
|
36 | 32 |
|
37 |
| -### metadata |
| 33 | +### Retrieving a Customer |
38 | 34 |
|
39 |
| -* customer extra info or data set with metadata as array. |
| 35 | +You can retrieve a customer by their ID. |
40 | 36 |
|
41 | 37 | ```php
|
42 |
| -LaraStripeCustomer::metadata([ |
43 |
| - 'phone' => '212', |
44 |
| - 'age' => 23 |
45 |
| -]) |
| 38 | +$customer = $stripeCustomer->retrieve($customerId); |
| 39 | + |
| 40 | +if ($customer->isError) { |
| 41 | + // Handle errors |
| 42 | + echo $customer->message; |
| 43 | +} else { |
| 44 | + // Use $customer to access customer data |
| 45 | +} |
46 | 46 | ```
|
47 | 47 |
|
48 |
| -### get |
| 48 | +### Deleting a Customer |
49 | 49 |
|
50 |
| -`get` method return customer data after customer create complete. |
51 |
| - |
52 |
| -* return type `object` |
| 50 | +You can delete a customer by their ID. |
53 | 51 |
|
54 | 52 | ```php
|
55 |
| -LaraStripeCustomer::get(); |
| 53 | +$customer = $stripeCustomer->delete($customerId); |
| 54 | + |
| 55 | +if ($customer->isError) { |
| 56 | + // Handle errors |
| 57 | + echo $customer->message; |
| 58 | +} else { |
| 59 | + // Customer deleted successfully |
| 60 | +} |
56 | 61 | ```
|
57 | 62 |
|
58 |
| -## full code |
| 63 | +### Retrieving All Customers |
| 64 | + |
| 65 | +You can retrieve a list of all customers. |
59 | 66 |
|
60 | 67 | ```php
|
61 |
| -$cus = LaraStripeCustomer::setup([ |
62 |
| - 'secret_key'=>'sk_test_mBGoFuccDy2KCD4pobbaixKK00qUW0ghu1' |
63 |
| -]) |
64 |
| -->create(['source' => 'tok_visa','email' => '[email protected]']) |
65 |
| -->metadata(['phone' => '212','age'=>23]) |
66 |
| -->get(); |
67 |
| - |
68 |
| -return response()->json($cus); |
| 68 | +$customers = $stripeCustomer->lists(); |
| 69 | + |
| 70 | +if ($customers->isError) { |
| 71 | + // Handle errors |
| 72 | + echo $customers->message; |
| 73 | +} else { |
| 74 | + // Use $customers to access the list of customers |
| 75 | +} |
69 | 76 | ```
|
70 | 77 |
|
71 |
| -`~ NB: store customer id on db for future payment` |
| 78 | +### Managing Customer Cards |
72 | 79 |
|
73 |
| -## `see - charge with customer ` |
| 80 | +You can manage a customer's cards, such as listing, adding, deleting, or setting a default card. |
74 | 81 |
|
75 |
| - same as [charge create with card](https://github.com/code4mk/lara-stripe/blob/master/doc/charge.md#full-code) with card. only `card method` exchage with `customer method` |
76 |
| - |
77 |
| -* `customer($cusId)` |
| 82 | +#### Listing Customer Cards |
78 | 83 |
|
79 | 84 | ```php
|
80 |
| -$charge = LaraStripeCharge::setup([ |
81 |
| - 'secret_key'=>'sk_test_mBGoFuccDy2KCD4pobbaixKK00qUW0ghu1', |
82 |
| - 'public_key' => 'pk_test_VNi7F1zcwwffZIi1tAkX1dVs00JfKPsCGR', |
83 |
| - 'currency'=>'usd' |
84 |
| - ]) |
85 |
| - ->customer('cus_G2L2KoumL45hzn') |
86 |
| - ->amount(25.54567) |
87 |
| - ->metadata(['tnx_id' => 'tnx-32343','purchase_id' => 'trgtrg45']) |
88 |
| - ->description('charge with customer id ') |
89 |
| - ->purchase() |
90 |
| - ->get(); |
91 |
| -return response()->json($charge); |
| 85 | +$cards = $stripeCustomer->cards($customerId); |
| 86 | + |
| 87 | +if ($cards->isError) { |
| 88 | + // Handle errors |
| 89 | + echo $cards->message; |
| 90 | +} else { |
| 91 | + // Use $cards to access the list of customer cards |
| 92 | +} |
92 | 93 | ```
|
93 | 94 |
|
94 |
| -# retrieve customer |
95 |
| - |
96 |
| -* `retrieve` method has `cusId` param which is customer id. |
97 |
| -* return type `object` |
| 95 | +#### Adding a New Card |
98 | 96 |
|
99 | 97 | ```php
|
100 |
| -$cus = LaraStripeCustomer::setup([ |
101 |
| - 'secret_key'=>'sk_test_mBGoFuccDy2KCD4pobbaixKK00qUW0ghu1' |
102 |
| -]) |
103 |
| -->retrieve('cus_G2L2KoumL45hzn'); |
| 98 | +$cardToken = 'your_stripe_card_token'; |
| 99 | +$cardMaxLimit = 3; // Set the maximum number of cards per customer |
104 | 100 |
|
105 |
| -return response()->json($cus); |
| 101 | +$response = $stripeCustomer->addCard($customerId, $cardToken, $cardMaxLimit); |
| 102 | + |
| 103 | +if ($response->isError) { |
| 104 | + // Handle errors |
| 105 | + echo $response->message; |
| 106 | +} else { |
| 107 | + // Card added successfully |
| 108 | +} |
106 | 109 | ```
|
107 | 110 |
|
108 |
| -# change card |
| 111 | +#### Deleting a Card |
| 112 | + |
| 113 | +```php |
| 114 | +$cardId = 'stripe_card_id'; |
| 115 | + |
| 116 | +$response = $stripeCustomer->deleteCard($customerId, $cardId); |
109 | 117 |
|
110 |
| -Sometimes customer want to change their card. |
| 118 | +if ($response->isError) { |
| 119 | + // Handle errors |
| 120 | + echo $response->message; |
| 121 | +} else { |
| 122 | + // Card deleted successfully |
| 123 | +} |
| 124 | +``` |
111 | 125 |
|
112 |
| -* `changeCard($cusId,$cardToken)` has `cusId` param which is customer id and `cardToken` param which is card token . |
113 |
| -* return type `object` |
| 126 | +#### Setting Default Card |
114 | 127 |
|
115 | 128 | ```php
|
116 |
| -$cus = LaraStripeCustomer::setup([ |
117 |
| - 'secret_key'=>'sk_test_mBGoFuccDy2KCD4pobbaixKK00qUW0ghu1' |
118 |
| -]) |
119 |
| -->changeCard('cus_G2L2KoumL45hzn','tok_amex'); |
| 129 | +$defaultCardId = 'stripe_default_card_id'; |
120 | 130 |
|
121 |
| -return response()->json($cus); |
| 131 | +$response = $stripeCustomer->setDefaultCard($customerId, $defaultCardId); |
122 | 132 |
|
| 133 | +if ($response->isError) { |
| 134 | + // Handle errors |
| 135 | + echo $response->message; |
| 136 | +} else { |
| 137 | + // Default card set successfully |
| 138 | +} |
123 | 139 | ```
|
| 140 | + |
0 commit comments