1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Larium\Pay; |
6
|
|
|
|
7
|
|
|
use Larium\CreditCard\CreditCard; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A fluent interface ({@link http://martinfowler.com/bliki/FluentInterface.html}) for creating transaction objects. |
12
|
|
|
* |
13
|
|
|
* @author Andreas Kollaros <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class TransactionBuilder |
16
|
|
|
{ |
17
|
|
|
private $transaction; |
18
|
|
|
|
19
|
|
|
private $currency; |
20
|
|
|
|
21
|
|
|
private $transactionInstance; |
22
|
|
|
|
23
|
|
|
private $extraOptions = []; |
24
|
|
|
|
25
|
|
|
private $description; |
26
|
|
|
|
27
|
|
|
private $clientIp; |
28
|
|
|
|
29
|
|
|
private $merchantReference; |
30
|
|
|
|
31
|
|
|
private $constructArgs = []; |
32
|
|
|
|
33
|
|
|
private $address = []; |
34
|
|
|
|
35
|
|
|
private $customerEmail; |
36
|
|
|
|
37
|
|
|
private static $saleMethods = [ |
38
|
|
|
'currency', |
39
|
|
|
'description', |
40
|
|
|
'clientIp', |
41
|
|
|
'address', |
42
|
|
|
'customerEmail', |
43
|
|
|
]; |
44
|
|
|
|
45
|
1 |
|
private function __construct($transaction) |
46
|
|
|
{ |
47
|
1 |
|
$this->transaction = $transaction; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public static function purchase($amount) |
51
|
|
|
{ |
52
|
1 |
|
$instance = new self('Larium\Pay\Transaction\PurchaseTransaction'); |
53
|
1 |
|
$instance->constructArgs[] = $amount; |
54
|
|
|
|
55
|
1 |
|
return $instance; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function authorize($amount) |
59
|
|
|
{ |
60
|
|
|
$instance = new self('Larium\Pay\Transaction\AuthorizeTransaction'); |
61
|
|
|
$instance->constructArgs[] = $amount; |
62
|
|
|
|
63
|
|
|
return $instance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function capture($amount) |
67
|
|
|
{ |
68
|
|
|
$instance = new self('Larium\Pay\Transaction\CaptureTransaction'); |
69
|
|
|
$instance->constructArgs[] = $amount; |
70
|
|
|
|
71
|
|
|
return $instance; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function refund($amount) |
75
|
|
|
{ |
76
|
|
|
$instance = new self('Larium\Pay\Transaction\RefundTransaction'); |
77
|
|
|
$instance->constructArgs[] = $amount; |
78
|
|
|
|
79
|
|
|
return $instance; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function cancel($transactionId) |
83
|
|
|
{ |
84
|
|
|
$instance = new self('Larium\Pay\Transaction\CancelTransaction'); |
85
|
|
|
$instance->constructArgs[] = $transactionId; |
86
|
|
|
|
87
|
|
|
return $instance; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function charge(CreditCard $card) |
91
|
|
|
{ |
92
|
1 |
|
$this->constructArgs[] = $card; |
93
|
|
|
|
94
|
1 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function with($currency) |
98
|
|
|
{ |
99
|
1 |
|
$this->currency = $currency; |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function withExtraOptions(array $extraOptions) |
105
|
|
|
{ |
106
|
1 |
|
$this->extraOptions = $extraOptions; |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function describedAs($description) |
112
|
|
|
{ |
113
|
1 |
|
$this->description = $description; |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function billTo(array $address) |
119
|
|
|
{ |
120
|
1 |
|
$this->address = $address; |
121
|
|
|
|
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
public function mailTo($customerEmail) |
126
|
|
|
{ |
127
|
1 |
|
$this->customerEmail = $customerEmail; |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function withMerchantReference($merchantReference) |
133
|
|
|
{ |
134
|
|
|
$this->merchantReference = $merchantReference; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function withClientIp($clientIp) |
140
|
|
|
{ |
141
|
1 |
|
$this->clientIp = $clientIp; |
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function getTransaction() |
147
|
|
|
{ |
148
|
1 |
|
$this->build(); |
149
|
|
|
|
150
|
1 |
|
return $this->transactionInstance; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
private function build() |
154
|
|
|
{ |
155
|
1 |
|
$reflection = new ReflectionClass($this->transaction); |
156
|
|
|
|
157
|
1 |
|
$args = $this->constructArgs; |
158
|
1 |
|
$args[] = $this->extraOptions; |
159
|
|
|
|
160
|
1 |
|
$this->transactionInstance = $reflection->newInstanceArgs($args); |
161
|
|
|
|
162
|
1 |
|
if ($this->transactionInstance instanceof Transaction\Sale) { |
163
|
1 |
|
foreach (self::$saleMethods as $prop) { |
164
|
1 |
|
$method = 'set' . (ucwords($prop)); |
165
|
1 |
|
$this->transactionInstance->$method($this->$prop); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|