Skip to content

Commit 3d5103f

Browse files
author
Mostafa Kamal
committed
customer refactoring
1 parent 949622c commit 3d5103f

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

src/StripeCustomer.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,74 @@ public function retrieve($cusId)
123123

124124
}
125125

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+
126182
/**
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
130186
* @return object
131187
*/
132-
public function changeCard($cusId,$cardToken)
188+
public function setDefaultCard($cusId,$cusSourceId)
133189
{
134190
try {
135191
Stripe::setApiKey($this->secretKey);
136192
$cus = Customer::update($cusId,[
137-
'source' => $cardToken
193+
'default_source' => $cusSourceId
138194
]);
139195
return $cus;
140196
} catch (\Exception $e) {

0 commit comments

Comments
 (0)