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

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre committed Jan 24, 2018
1 parent 0756845 commit 954b15a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Then, (later) charge the customer without token.
```php
StripeConnect::transaction()
->amount(1000, 'usd')
->useSavedCustomer()
->from($customer)
->to($vendor)
->create();
Expand Down
20 changes: 20 additions & 0 deletions src/StripeConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ public static function createCustomer($token, $from, $params = [])
});
}

/**
* @param $token
* @param $from
* @param array $params
* @return Stripe
*/
public function createOrUpdateCustomer($token, $from, $params = [])
{
self::prepare();
$user = self::getStripeModel($from);
if (!$user) {
return self::createCustomer($token, $from, $params);
} else {
$customer = \Stripe\Customer::retrieve($token->customer_id);
$customer->source = $token;
$customer->save();
return $user;
}
}

/**
* @param $user
* @param $id_key
Expand Down
20 changes: 17 additions & 3 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Transaction
/**
* @var
*/
private $from, $to, $value, $currency, $to_params, $token, $fee, $from_params;
private $from, $to, $value, $currency, $to_params, $token, $fee, $from_params, $saved_customer;

/**
* Transaction constructor.
Expand All @@ -43,6 +43,15 @@ public function from($user, $params = [])
return $this;
}

/**
* @return $this
*/
public function useSavedCustomer()
{
$this->saved_customer = true;
return $this;
}

/**
* Set the Vendor.
*
Expand Down Expand Up @@ -85,6 +94,7 @@ public function fee($amount)

/**
* Create the transaction: charge customer and credit vendor.
* This function saves the two accounts.
*
* @param array $params
* @return Charge
Expand All @@ -94,12 +104,16 @@ public function create($params = [])
// Prepare vendor
$vendor = StripeConnect::createAccount($this->to, $this->to_params);
// Prepare customer
$customer = StripeConnect::createCustomer($this->token, $this->from, $this->from_params);
if ($this->saved_customer) {
$customer = StripeConnect::createCustomer($this->token, $this->from, $this->from_params);
$params["customer"] = $customer->customer_id;
} else {
$params["source"] = $this->token;
}

return Charge::create(array_merge([
"amount" => $this->value,
"currency" => $this->currency,
"customer" => $customer->customer_id,
"destination" => [
"account" => $vendor->account_id,
],
Expand Down

0 comments on commit 954b15a

Please sign in to comment.