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