Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 954b15a

Browse files
committed
fix
1 parent 0756845 commit 954b15a

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Then, (later) charge the customer without token.
6767
```php
6868
StripeConnect::transaction()
6969
->amount(1000, 'usd')
70+
->useSavedCustomer()
7071
->from($customer)
7172
->to($vendor)
7273
->create();

src/StripeConnect.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ public static function createCustomer($token, $from, $params = [])
7171
});
7272
}
7373

74+
/**
75+
* @param $token
76+
* @param $from
77+
* @param array $params
78+
* @return Stripe
79+
*/
80+
public function createOrUpdateCustomer($token, $from, $params = [])
81+
{
82+
self::prepare();
83+
$user = self::getStripeModel($from);
84+
if (!$user) {
85+
return self::createCustomer($token, $from, $params);
86+
} else {
87+
$customer = \Stripe\Customer::retrieve($token->customer_id);
88+
$customer->source = $token;
89+
$customer->save();
90+
return $user;
91+
}
92+
}
93+
7494
/**
7595
* @param $user
7696
* @param $id_key

src/Transaction.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Transaction
1818
/**
1919
* @var
2020
*/
21-
private $from, $to, $value, $currency, $to_params, $token, $fee, $from_params;
21+
private $from, $to, $value, $currency, $to_params, $token, $fee, $from_params, $saved_customer;
2222

2323
/**
2424
* Transaction constructor.
@@ -43,6 +43,15 @@ public function from($user, $params = [])
4343
return $this;
4444
}
4545

46+
/**
47+
* @return $this
48+
*/
49+
public function useSavedCustomer()
50+
{
51+
$this->saved_customer = true;
52+
return $this;
53+
}
54+
4655
/**
4756
* Set the Vendor.
4857
*
@@ -85,6 +94,7 @@ public function fee($amount)
8594

8695
/**
8796
* Create the transaction: charge customer and credit vendor.
97+
* This function saves the two accounts.
8898
*
8999
* @param array $params
90100
* @return Charge
@@ -94,12 +104,16 @@ public function create($params = [])
94104
// Prepare vendor
95105
$vendor = StripeConnect::createAccount($this->to, $this->to_params);
96106
// Prepare customer
97-
$customer = StripeConnect::createCustomer($this->token, $this->from, $this->from_params);
107+
if ($this->saved_customer) {
108+
$customer = StripeConnect::createCustomer($this->token, $this->from, $this->from_params);
109+
$params["customer"] = $customer->customer_id;
110+
} else {
111+
$params["source"] = $this->token;
112+
}
98113

99114
return Charge::create(array_merge([
100115
"amount" => $this->value,
101116
"currency" => $this->currency,
102-
"customer" => $customer->customer_id,
103117
"destination" => [
104118
"account" => $vendor->account_id,
105119
],

0 commit comments

Comments
 (0)