|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service\Braintree; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ForwardApiService |
|
9
|
|
|
* @package App\Service\Braintree |
|
10
|
|
|
*/ |
|
11
|
|
|
class ForwardApiService extends AbstractBraintreeService |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
public string $ENDPOINT = "https://forwarding.sandbox.braintreegateway.com"; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $paymentNonce |
|
20
|
|
|
* @param string $deviceData |
|
21
|
|
|
* @return array|null |
|
22
|
|
|
*/ |
|
23
|
|
|
public function directTokenization(string $paymentNonce, string $deviceData): ?array |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
|
|
$ch = curl_init(); |
|
27
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->ENDPOINT); |
|
28
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); |
|
29
|
|
|
curl_setopt( |
|
30
|
|
|
$ch, |
|
31
|
|
|
CURLOPT_USERPWD, |
|
32
|
|
|
$this->gateway->config->getPublicKey() . ':' . $this->gateway->config->getPrivateKey() |
|
33
|
|
|
); |
|
34
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
|
35
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ |
|
36
|
|
|
'debug_transformations' => true, |
|
37
|
|
|
'name' => 'default', |
|
38
|
|
|
'merchant_id' => $this->gateway->config->getMerchantId(), |
|
39
|
|
|
'payment_method_nonce' => $paymentNonce, |
|
40
|
|
|
'device_data' => $deviceData, |
|
41
|
|
|
'method' => 'POST', |
|
42
|
|
|
'data' => [ |
|
43
|
|
|
'card-id' => 'custom_card_id_1', |
|
44
|
|
|
'open-id-token' => sha1(time()), |
|
45
|
|
|
], |
|
46
|
|
|
'url' => 'https://test.com', |
|
47
|
|
|
'config' => [ |
|
48
|
|
|
'name' => 'default', |
|
49
|
|
|
'url' => '^https://test.com', |
|
50
|
|
|
'methods' => ['POST'], |
|
51
|
|
|
'types' => ['NetworkTokenizedCard'], |
|
52
|
|
|
'request_format' => [ |
|
53
|
|
|
'body' => 'json' |
|
54
|
|
|
], |
|
55
|
|
|
'transformations' => [ |
|
56
|
|
|
[ |
|
57
|
|
|
'path' => '/body/card/pan', |
|
58
|
|
|
'value' => '$number' |
|
59
|
|
|
], |
|
60
|
|
|
[ |
|
61
|
|
|
'path' => '/body/card/card-cvv', |
|
62
|
|
|
'value' => '$cvv' |
|
63
|
|
|
], |
|
64
|
|
|
[ |
|
65
|
|
|
'path' => '/body/card/expiration-month', |
|
66
|
|
|
'value' => '$expiration_month' |
|
67
|
|
|
], |
|
68
|
|
|
[ |
|
69
|
|
|
'path' => '/body/card/expiration-year', |
|
70
|
|
|
'value' => '$expiration_year' |
|
71
|
|
|
], |
|
72
|
|
|
[ |
|
73
|
|
|
'path' => '/body/custom-fields/card-id', |
|
74
|
|
|
'value' => '$card-id' |
|
75
|
|
|
], |
|
76
|
|
|
[ |
|
77
|
|
|
'path' => '/header/Authorization', |
|
78
|
|
|
'value' => '$open-id-token' |
|
79
|
|
|
] |
|
80
|
|
|
] |
|
81
|
|
|
|
|
82
|
|
|
] |
|
83
|
|
|
], true)); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
86
|
|
|
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$headers) { |
|
87
|
|
|
$len = strlen($header); |
|
88
|
|
|
$header = explode(':', $header, 2); |
|
89
|
|
|
if (count($header) < 2) { |
|
90
|
|
|
return $len; |
|
91
|
|
|
} |
|
92
|
|
|
$headers[(trim($header[0]))] = trim($header[1]); |
|
93
|
|
|
return $len; |
|
94
|
|
|
}); |
|
95
|
|
|
$result = curl_exec($ch); |
|
96
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
97
|
|
|
|
|
98
|
|
|
curl_close($ch); |
|
99
|
|
|
} catch (Exception $e) { |
|
100
|
|
|
$this->logger->error('Error on ForwardAPI::'.$this->ENDPOINT.' = ' . $e->getMessage()); |
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
return ([ |
|
104
|
|
|
'headers' => (object) $headers, |
|
105
|
|
|
'body' => (object) json_decode($result), |
|
|
|
|
|
|
106
|
|
|
'statusCode' => $statusCode |
|
107
|
|
|
]); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|