1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Rap2hpoutre\LaravelStripeConnect; |
5
|
|
|
|
6
|
|
|
use Stripe\Account as StripeAccount; |
7
|
|
|
use Stripe\Charge; |
8
|
|
|
use Stripe\Customer; |
9
|
|
|
use Stripe\Stripe as StripeBase; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Transaction |
14
|
|
|
* @package Rap2hpoutre\LaravelStripeConnect |
15
|
|
|
*/ |
16
|
|
|
class Transaction |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var |
20
|
|
|
*/ |
21
|
|
|
private $from, $to, $value, $currency, $to_params, $token, $fee, $from_params, $saved_customer; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Transaction constructor. |
25
|
|
|
* @param null $token |
26
|
|
|
*/ |
27
|
|
|
public function __construct($token = null) |
28
|
|
|
{ |
29
|
|
|
$this->token = $token; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set the Customer. |
34
|
|
|
* |
35
|
|
|
* @param $user |
36
|
|
|
* @param array $params |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function from($user, $params = []) |
40
|
|
|
{ |
41
|
|
|
$this->from = $user; |
42
|
|
|
$this->from_params = $params; |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function useSavedCustomer() |
50
|
|
|
{ |
51
|
|
|
$this->saved_customer = true; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the Vendor. |
57
|
|
|
* |
58
|
|
|
* @param $user |
59
|
|
|
* @param array $params |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function to($user, $params = []) |
63
|
|
|
{ |
64
|
|
|
$this->to = $user; |
65
|
|
|
$this->to_params = $params; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The amount of the transaction. |
71
|
|
|
* |
72
|
|
|
* @param $value |
73
|
|
|
* @param $currency |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function amount($value, $currency) |
77
|
|
|
{ |
78
|
|
|
$this->value = $value; |
79
|
|
|
$this->currency = $currency; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Take your fees here. |
85
|
|
|
* |
86
|
|
|
* @param $amount |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function fee($amount) |
90
|
|
|
{ |
91
|
|
|
$this->fee = $amount; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create the transaction: charge customer and credit vendor. |
97
|
|
|
* This function saves the two accounts. |
98
|
|
|
* |
99
|
|
|
* @param array $params |
100
|
|
|
* @return Charge |
101
|
|
|
*/ |
102
|
|
|
public function create($params = []) |
103
|
|
|
{ |
104
|
|
|
// Prepare vendor |
105
|
|
|
$vendor = StripeConnect::createAccount($this->to, $this->to_params); |
106
|
|
|
// Prepare customer |
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
|
|
|
} |
113
|
|
|
|
114
|
|
|
return Charge::create(array_merge([ |
115
|
|
|
"amount" => $this->value, |
116
|
|
|
"currency" => $this->currency, |
117
|
|
|
"destination" => [ |
118
|
|
|
"account" => $vendor->account_id, |
|
|
|
|
119
|
|
|
], |
120
|
|
|
"application_fee" => $this->fee ?? null, |
121
|
|
|
]), $params); |
122
|
|
|
} |
123
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.