-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass_TransferWise.php
More file actions
331 lines (285 loc) · 13.1 KB
/
class_TransferWise.php
File metadata and controls
331 lines (285 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
//
// Filename: .../includes/class_TransferWise.php
//
include 'includes/configure.php'; //Edit this file with your API KEYs, and ProfileIDs (when known)
class TransferWise {
// property declaration
private $tw;
private $OTT; //One Time Token. See https://api-docs.transferwise.com/#payouts-guide-strong-customer-authentication
// method declarations
public function __construct(
$profileId, //a valid ProfileID, or PROFILE_ID_UNKNOWN if not known
$readOnly=true //true (default): use the Read Only Token. false: use the full access token
) {
$this->tw = new stdClass();
$this->tw->profileId = $profileId;
switch($profileId){
case PROFILE_ID_UNKNOWN:
case PROFILE_ID_PERSONAL:
case PROFILE_ID_BUSINESS:
$this->tw->api_key = ($readOnly?API_KEY_TOKEN_READONLY:API_KEY_TOKEN_FULL);
$this->tw->url = 'https://api.transferwise.com';
$this->tw->priv_pem= PRIV_PEM;
break;
case SANDBOX_ID_UNKNOWN:
case SANDBOX_ID_PERSONAL:
case SANDBOX_ID_BUSINESS:
$this->tw->api_key = ($readOnly?SANDBOX_TOKEN_READONLY:SANDBOX_TOKEN_FULL);
$this->tw->url = 'https://api.sandbox.transferwise.tech';
$this->tw->priv_pem= SANDBOX_PRIV_PEM;
break;
default:
echo "Error: Unknown profileId: $profileId \n";
}
}
public function __destruct() {
;
}
public function getProfileId() {
return $this->tw->profileId;
}
public function getProfiles(){
return $this->GET('/v1/profiles');
}
public function getTransferById(
$transferId //a valid transferId
){
return $this->GET("/v1/transfers/$transferId");
}
public function getExchangeRate(
$source, //a 3-char currency code. e.g. 'USD'
$target //a 3-char currency code. e.g. 'EUR'
){
return $this->GET("/v1/rates?source=$source&target=$target");
}
public function getAccountBalance(
$currency=null //a 3-char currency code. e.g. 'EUR'. If null, then return account balances for all currencies
){
$json = $this->GET("/v1/borderless-accounts?profileId=".$this->tw->profileId);
if(!$currency) return $json;
$accounts = json_decode($json);
$details=$accounts[0];
if(!$details) return "Error: getAccountBalance() No Details returned: $json";
foreach($details->balances as $balance){
if($balance->currency == $currency)
return json_encode($balance);
}
}
public function getStatement(
$currency, //a 3-char currency code. e.g. 'EUR'.
$type='json', //'json' (default), 'pdf', or 'csv'
$intervalStart=null, //starting time of statement. Default: 1 month ago
$intervalEnd=null //ending time of statement. Default: now
){
//Defaults
if(!$intervalStart){
//1 month ago
$intervalStart = gmdate("Y-m-d\TH:i:s\Z", strtotime('-1 month'));
}
if(!$intervalEnd){
//now
$intervalEnd = gmdate("Y-m-d\TH:i:s\Z");
}
//get borderlessAccountId
$accountBalances = json_decode($this->getAccountBalance());
$borderlessAccountId = $accountBalances[0]->id;
//get statement
return $this->GET('/v3/profiles/'.$this->tw->profileId."/borderless-accounts/$borderlessAccountId/statement.$type?currency=$currency&intervalStart=$intervalStart&intervalEnd=$intervalEnd");
}
public function getRecipientAccounts(
$profileId=null, //[optional] Personal or business profile id
$currency=null //[optional] a 3-char currency code. e.g. 'EUR'.
){
$profileId && $data[] = array('profileId' => $profileId);
$currency && $data[] = array('currency' => $currency);
$data && ($args='?'.http_build_query($data));
return $this->GET('/v1/accounts'.$args);
}
public function postCreateAddress(
$country, //see https://api-docs.transferwise.com/#addresses-create
$firstLine,
$postCode,
$city,
$state=NULL,
$occupation=NULL
){
$data = new stdClass();
$data->profile = $this->tw->profileId;
$data->details = new stdClass();
$data->details->country = $country;
$data->details->firstLine = $firstLine;
$data->details->postCode = $postCode;
$data->details->city = $city;
if($state) $data->details->state = $state;
if($occupation)$data->details->occupation = $occupation;
return $this->POST('/v1/addresses',$data);
}
public function postCreateAccount(
$accountHolderName, //Name (string)
$currency, //a 3-char currency code. e.g. 'EUR'.
$type, //depends on curreny. See //see https://api-docs.transferwise.com/#recipient-accounts-create
$details, //see https://api-docs.transferwise.com/#recipient-accounts-create
$ownedByCustomer=false //true = you own this acct. false = you don't own this acct
){
$data = new stdClass();
$data->profile = $this->tw->profileId;
$data->accountHolderName = $accountHolderName;
$data->currency = $currency;
$data->type = $type;
$data->ownedByCustomer = $ownedByCustomer;
$data->details = $details;
return $this->POST('/v1/accounts',$data);
}
public function postCreateQuote(
$type, //'BALANCE_PAYOUT' for payments or 'BALANCE_CONVERSION' for conversion between balances
$sourceCurrency, //a 3-char currency code. e.g. 'EUR'.
$targetCurrency, //a 3-char currency code. e.g. 'EUR'.
$sourceAmount=null, //Amount in source currency. If specified, $targetAmount must be null.
$targetAmount=null //Amount in target currency. If specified, $sourceAmount must be null.
){
$data = new stdClass();
$data->profile = $this->tw->profileId;
$data->target = $targetCurrency;
$data->source = $sourceCurrency;
$data->rateType = 'FIXED';
if($targetAmount) $data->targetAmount = $targetAmount;
else $data->sourceAmount = $sourceAmount;
$data->type = $type;
return $this->POST('/v1/quotes',$data);
}
public function postCreateTransfer(
$targetAccount, //recipient account id
$quoteId, //quote id
$reference, //Recipient will see this reference text in their bank statement
$transferPurpose =null, //[Conditional] see: https://api-docs.transferwise.com/#transfers-requirements
$sourceOfFunds =null //[Conditional]see: https://api-docs.transferwise.com/#transfers-requirements
){
$data = new stdClass();
$data->targetAccount = $targetAccount;
$data->quote = $quoteId;
$data->customerTransactionId = $this->createUUID();
$data->details = new stdClass();
$data->details->reference = $reference;
$transferPurpose && $data->details->transferPurpose = $transferPurpose;
$sourceOfFunds && $data->details->sourceOfFunds = $sourceOfFunds;
return $this->POST('/v1/transfers',$data);
}
public function postFundTransfer(
$transferId //transferID from postCreateTransfer()
){
$data = new stdClass();
$data->type = 'BALANCE';
return $this->POST("/v3/profiles/".$this->tw->profileId."/transfers/$transferId/payments",$data);
}
public function postProfileWebhookCreate(
$name, //any nickname
$trigger_on, //'transfers#state-change', 'transfers#active-cases', or 'balances#credit'
$url //the URL where your server will be listening for events
){
$data = new stdClass();
$data->name = $name;
$data->trigger_on = $trigger_on;
$data->delivery = new stdClass();
$data->delivery->version = '2.0.0';
$data->delivery->url = $url;
return $this->POST('/v3/profiles/'.$this->tw->profileId.'/subscriptions',$data);
}
public function getProfileWebhookList(){
return $this->GET('/v3/profiles/'.$this->tw->profileId.'/subscriptions');
}
public function deleteProfileWebhook(
$id //id of ProfileWebhook to delete, as returned by getProfileWebhookList()
){
return $this->DELETE('/v3/profiles/'.$this->tw->profileId."/subscriptions/$id");
}
public function deleteAccount(
$accountId //id of account to delete.
){
return $this->DELETE("/v1/accounts/$accountId");
}
//////////////// Internal (private) worker functions ////////////////////////////
private function POST($url,$data){
return $this->curl('POST',$url,$data);
}
private function GET($url){
return $this->curl('GET',$url);
}
private function DELETE($url){
return $this->curl('DELETE',$url);
}
private function PUT($url){
return $this->curl('PUT',$url);
}
private function headerLineCallback($curl, $headerLine){
$len = strlen($headerLine);
$header = explode(':', $headerLine, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
if(strtolower(trim($header[0])) == 'x-2fa-approval')
$this->OTT = trim($header[1]);
return $len;
}
private function curl($mode, $curl_url,$data=NULL,$headers=NULL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
//Need to view headers for SCA (https://api-docs.transferwise.com/#payouts-guide-strong-customer-authentication)
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this,'headerLineCallback'));
curl_setopt($ch, CURLOPT_URL, $this->tw->url."$curl_url");
$headerArray[] = "Authorization: Bearer ".$this->tw->api_key;
if($mode=='POST'){
$payload = json_encode($data);
$headerArray[] = "Content-Type: application/json";
$headerArray[] = 'Content-Length: ' . strlen($payload);
if($headers){
foreach($headers as $header){
$headerArray[] = $header;
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
//Reset One Time Token
$this->OTT = '';
$response = curl_exec($ch);
if($response === false){
echo 'Curl error: ' . curl_error($ch);
}
curl_close ($ch);
//See if need to resend because of SCA
if(!empty($this->OTT)){
//We have received a One Time Token
$SCA=json_decode($response);
if($SCA->status==403 && !empty($SCA->path)){
if(version_compare(PHP_VERSION, '5.4.8') >= 0){
//Requires PHP Version 5.4.8 or higher
$pkeyid = openssl_pkey_get_private('file://'.$this->tw->priv_pem);
openssl_sign($this->OTT, $Xsignature, $pkeyid,OPENSSL_ALGO_SHA256);
openssl_free_key($pkeyid);
$Xsignature= base64_encode( $Xsignature);
} else {
//Requires access to shell commands
$Xsignature= shell_exec("printf '$this->OTT' | openssl sha256 -sign ".$this->tw->priv_pem." | base64 -w 0") ;
}
$headers[] = "x-2fa-approval: $this->OTT";
$headers[] = "X-Signature: $Xsignature";
$response = $this->curl($mode, $SCA->path,$data,$headers);
}
}
return $response;
}
private function createUUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
?>