@@ -123,18 +123,74 @@ public function retrieve($cusId)
123
123
124
124
}
125
125
126
+ public function cards ($ cusId )
127
+ {
128
+ try {
129
+ Stripe::setApiKey ($ this ->secretKey );
130
+ $ cus = Customer::retrieve ($ cusId );
131
+ $ data = [];
132
+ $ cards = $ cus ->sources ->data ;
133
+ $ defaultCard = $ cus ->default_source ;
134
+
135
+ foreach ($ cards as $ key => $ value ) {
136
+ if ($ value ->id === $ defaultCard ) {
137
+ $ data [$ key ] = ['cardId ' => $ value ->id ,'last4 ' => $ value ->last4 ,'brand ' =>$ value ->brand ,'customer ' => $ value ->customer ,'isDefault ' => true ];
138
+ } else {
139
+ $ data [$ key ] = ['cardId ' => $ value ->id ,'last4 ' => $ value ->last4 ,'brand ' =>$ value ->brand ,'customer ' => $ value ->customer ,'isDefault ' => false ];
140
+ }
141
+ }
142
+ return $ data ;
143
+
144
+ } catch (\Exception $ e ) {
145
+ return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
146
+ }
147
+ }
148
+
149
+
150
+ public function addCard ($ cusId ,$ cardToken ,$ max =3 )
151
+ {
152
+ try {
153
+ Stripe::setApiKey ($ this ->secretKey );
154
+ if (count ($ this ->cards ($ cusId )) <= $ max -1 ) {
155
+ $ cus = Customer::createSource ($ cusId ,[
156
+ 'source ' => $ cardToken
157
+ ]);
158
+ return $ cus ;
159
+ }
160
+ return "You already exceed card quota $ {max}" ;
161
+
162
+ } catch (\Exception $ e ) {
163
+ return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
164
+ }
165
+ }
166
+
167
+ public function deleteCard ($ cusId ,$ cardToken )
168
+ {
169
+ try {
170
+ Stripe::setApiKey ($ this ->secretKey );
171
+ if (count ($ this ->cards ($ cusId )) > 1 ) {
172
+ $ cus = Customer::deleteSource ($ cusId ,$ cardToken );
173
+ return $ cus ;
174
+ }
175
+ return "you can't delete the card " ;
176
+
177
+ } catch (\Exception $ e ) {
178
+ return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
179
+ }
180
+ }
181
+
126
182
/**
127
- * Change customer card.
128
- * @param string $cusId [description]
129
- * @param string $cardToken create with stripe.js or manually create.
183
+ * Set customer default card.
184
+ * @param string $cusId
185
+ * @param string $cusSourceId
130
186
* @return object
131
187
*/
132
- public function changeCard ($ cusId ,$ cardToken )
188
+ public function setDefaultCard ($ cusId ,$ cusSourceId )
133
189
{
134
190
try {
135
191
Stripe::setApiKey ($ this ->secretKey );
136
192
$ cus = Customer::update ($ cusId ,[
137
- 'source ' => $ cardToken
193
+ 'default_source ' => $ cusSourceId
138
194
]);
139
195
return $ cus ;
140
196
} catch (\Exception $ e ) {
0 commit comments