|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\PaymentProtocol; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Address\AddressInterface; |
|
6
|
|
|
use BitWasp\Bitcoin\PaymentProtocol\Protobufs\Output; |
|
7
|
|
|
use BitWasp\Bitcoin\PaymentProtocol\Protobufs\PaymentDetails; |
|
8
|
|
|
use BitWasp\Bitcoin\PaymentProtocol\Protobufs\PaymentRequest; |
|
9
|
|
|
use BitWasp\Bitcoin\Script\ScriptFactory; |
|
10
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionOutput; |
|
11
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class RequestBuilder |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Optional |
|
17
|
|
|
* @var string|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private $network; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Repeated |
|
23
|
|
|
* @var TransactionOutputInterface[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $outputs = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Required |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $time; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Optional |
|
35
|
|
|
* @var int|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $expires; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Optional |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $memo; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Optional |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $payment_url; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Optional |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
private $merchant_data; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var RequestSigner |
|
59
|
|
|
*/ |
|
60
|
|
|
private $signer; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $memo |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setMemo($memo) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->memo = $memo; |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $network |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setNetwork($network) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->network = $network; |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param array $outputs |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setOutputs(array $outputs) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->outputs = []; |
|
89
|
|
|
foreach ($outputs as $output) { |
|
90
|
|
|
$this->addOutput($output); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param TransactionOutputInterface $output |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function addOutput(TransactionOutputInterface $output) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->outputs[] = $output; |
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param AddressInterface $address |
|
108
|
|
|
* @param $value |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function addAddressPayment(AddressInterface $address, $value) |
|
112
|
|
|
{ |
|
113
|
|
|
$script = ScriptFactory::scriptPubKey()->payToAddress($address); |
|
114
|
|
|
$output = new TransactionOutput($value, $script); |
|
115
|
|
|
return $this->addOutput($output); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param int $time |
|
120
|
|
|
* @return RequestBuilder |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setTime($time) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->time = $time; |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param int $expires |
|
130
|
|
|
* @return RequestBuilder |
|
131
|
|
|
*/ |
|
132
|
|
|
public function setExpires($expires) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->expires = $expires; |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $payment_url |
|
140
|
|
|
* @return RequestBuilder |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setPaymentUrl($payment_url) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->payment_url = $payment_url; |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $merchant_data |
|
150
|
|
|
* @return RequestBuilder |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setMerchantData($merchant_data) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->merchant_data = $merchant_data; |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param RequestSigner $signer |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setSigner(RequestSigner $signer) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->signer = $signer; |
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return PaymentDetails |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getPaymentDetails() |
|
172
|
|
|
{ |
|
173
|
|
|
if (is_null($this->time)) { |
|
174
|
|
|
throw new \RuntimeException('Time not set on PaymentDetails'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$details = new PaymentDetails(); |
|
178
|
|
|
if (!is_null($this->network)) { |
|
179
|
|
|
$details->setNetwork($this->network); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$c = 0; |
|
183
|
|
|
array_map(function (TransactionOutputInterface $output) use ($details, &$c) { |
|
184
|
|
|
$details->setOutputs( |
|
185
|
|
|
(new Output()) |
|
186
|
|
|
->setAmount($output->getValue()) |
|
187
|
|
|
->setScript($output->getScript()->getBinary()), |
|
188
|
|
|
$c++ |
|
189
|
|
|
); |
|
190
|
|
|
}, $this->outputs); |
|
191
|
|
|
|
|
192
|
|
|
$details->setTime($this->time); |
|
193
|
|
|
if (!is_null($this->expires)) { |
|
194
|
|
|
$details->setExpires($this->expires); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if (!is_null($this->memo)) { |
|
198
|
|
|
$details->setMemo($this->memo); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if (!is_null($this->payment_url)) { |
|
202
|
|
|
$details->setPaymentUrl($this->payment_url); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
if (!is_null($this->merchant_data)) { |
|
206
|
|
|
$details->setMerchantData($this->merchant_data); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $details; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return PaymentRequest |
|
214
|
|
|
*/ |
|
215
|
|
|
public function getPaymentRequest() |
|
216
|
|
|
{ |
|
217
|
|
|
// Serialize the payment details, and apply a signature based on instance of PaymentRequestSigner |
|
218
|
|
|
$request = new PaymentRequest(); |
|
219
|
|
|
$request->setSerializedPaymentDetails($this->getPaymentDetails()->serialize()); |
|
220
|
|
|
|
|
221
|
|
|
if ($this->signer instanceof RequestSigner) { |
|
222
|
|
|
$request = $this->signer->sign($request); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $request; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|