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
|
|
|
$body = [ |
27
|
|
|
'debug_transformations' => true, |
28
|
|
|
'name' => 'default', |
29
|
|
|
'merchant_id' => $this->gateway->config->getMerchantId(), |
30
|
|
|
'payment_method_nonce' => $paymentNonce, |
31
|
|
|
'device_data' => $deviceData, |
32
|
|
|
'method' => 'POST', |
33
|
|
|
'data' => [ |
34
|
|
|
'card-id' => 'custom_card_id_1', |
35
|
|
|
'open-id-token' => sha1(time()), |
36
|
|
|
], |
37
|
|
|
'url' => 'https://test.com', |
38
|
|
|
'config' => [ |
39
|
|
|
'name' => 'default', |
40
|
|
|
'url' => '^https://test.com', |
41
|
|
|
'methods' => ['POST'], |
42
|
|
|
'types' => ['NetworkTokenizedCard'], |
43
|
|
|
'request_format' => [ |
44
|
|
|
'body' => 'json' |
45
|
|
|
], |
46
|
|
|
'transformations' => [ |
47
|
|
|
[ |
48
|
|
|
'path' => '/body/card/pan', |
49
|
|
|
'value' => '$number' |
50
|
|
|
], |
51
|
|
|
[ |
52
|
|
|
'path' => '/body/card/card-cvv', |
53
|
|
|
'value' => '$cvv' |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'path' => '/body/card/expiration-month', |
57
|
|
|
'value' => '$expiration_month' |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
'path' => '/body/card/expiration-year', |
61
|
|
|
'value' => '$expiration_year' |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'path' => '/body/custom-fields/card-id', |
65
|
|
|
'value' => '$card-id' |
66
|
|
|
], |
67
|
|
|
[ |
68
|
|
|
'path' => '/header/Authorization', |
69
|
|
|
'value' => '$open-id-token' |
70
|
|
|
] |
71
|
|
|
] |
72
|
|
|
|
73
|
|
|
] |
74
|
|
|
]; |
75
|
|
|
$ch = curl_init(); |
76
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->ENDPOINT); |
77
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); |
78
|
|
|
curl_setopt( |
79
|
|
|
$ch, |
80
|
|
|
CURLOPT_USERPWD, |
81
|
|
|
$this->gateway->config->getPublicKey() . ':' . $this->gateway->config->getPrivateKey() |
82
|
|
|
); |
83
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
84
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, true)); |
|
|
|
|
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
|
|
|
'request' => $body, |
105
|
|
|
'response' => [ |
106
|
|
|
'headers' => (object) $headers, |
107
|
|
|
'body' => (object) json_decode($result), |
|
|
|
|
108
|
|
|
'statusCode' => $statusCode |
109
|
|
|
] |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|