|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusBraintreePlugin\ApiClient; |
|
14
|
|
|
|
|
15
|
|
|
use Braintree\ClientToken; |
|
16
|
|
|
use Braintree\Configuration; |
|
17
|
|
|
use Braintree\PaymentMethodNonce; |
|
18
|
|
|
use Braintree\Result\Error; |
|
19
|
|
|
use Braintree\Result\Successful; |
|
20
|
|
|
use Braintree\Transaction; |
|
21
|
|
|
use Payum\Core\Bridge\Spl\ArrayObject; |
|
22
|
|
|
|
|
23
|
|
|
class BraintreeApiClient implements BraintreeApiClientInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
protected $options = []; |
|
27
|
|
|
|
|
28
|
|
|
public function initialise(array $options): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->options = $options; |
|
31
|
|
|
|
|
32
|
|
|
Configuration::reset(); |
|
33
|
|
|
|
|
34
|
|
|
$environment = 'sandbox'; |
|
35
|
|
|
|
|
36
|
|
|
if (array_key_exists('environment', $this->options) && null !== $this->options['environment']) { |
|
37
|
|
|
$environment = $this->options['environment']; |
|
38
|
|
|
} elseif (array_key_exists('sandbox', $this->options) && null !== $this->options['sandbox']) { |
|
39
|
|
|
$environment = !$this->options['sandbox'] ? 'production' : 'sandbox'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
Configuration::environment($environment); |
|
43
|
|
|
|
|
44
|
|
|
Configuration::merchantId($this->options['merchantId']); |
|
45
|
|
|
Configuration::publicKey($this->options['publicKey']); |
|
46
|
|
|
Configuration::privateKey($this->options['privateKey']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function generateClientToken(array $params): string |
|
50
|
|
|
{ |
|
51
|
|
|
if (array_key_exists('merchantAccountId', $this->options) && null !== $this->options['merchantAccountId']) { |
|
52
|
|
|
$params['merchantAccountId'] = $this->options['merchantAccountId']; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return ClientToken::generate($params); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function findPaymentMethodNonce(string $nonceString): PaymentMethodNonce |
|
59
|
|
|
{ |
|
60
|
|
|
return PaymentMethodNonce::find($nonceString); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ArrayObject $params |
|
65
|
|
|
* |
|
66
|
|
|
* @return Error|Successful |
|
67
|
|
|
*/ |
|
68
|
|
|
public function sale(ArrayObject $params) |
|
69
|
|
|
{ |
|
70
|
|
|
$options = $params->offsetExists('options') ? $params['options'] : []; |
|
71
|
|
|
|
|
72
|
|
|
if (null !== $this->options['storeInVault'] && !isset($options['storeInVault'])) { |
|
73
|
|
|
$options['storeInVault'] = $this->options['storeInVault']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (null !== $this->options['storeInVaultOnSuccess'] && !isset($options['storeInVaultOnSuccess'])) { |
|
77
|
|
|
$options['storeInVaultOnSuccess'] = $this->options['storeInVaultOnSuccess']; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (null !== $this->options['addBillingAddressToPaymentMethod'] && |
|
81
|
|
|
!isset($options['addBillingAddressToPaymentMethod']) && |
|
82
|
|
|
$params->offsetExists('billing')) { |
|
83
|
|
|
$options['addBillingAddressToPaymentMethod'] = $this->options['addBillingAddressToPaymentMethod']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (null !== $this->options['storeShippingAddressInVault'] && |
|
87
|
|
|
!isset($options['storeShippingAddressInVault']) && |
|
88
|
|
|
$params->offsetExists('shipping')) { |
|
89
|
|
|
$options['storeShippingAddressInVault'] = $this->options['storeShippingAddressInVault']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$params['options'] = $options; |
|
93
|
|
|
|
|
94
|
|
|
if (array_key_exists('merchantAccountId', $this->options) && null !== $this->options['merchantAccountId']) { |
|
95
|
|
|
$params['merchantAccountId'] = $this->options['merchantAccountId']; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return Transaction::sale((array) $params); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $transactionId |
|
103
|
|
|
* |
|
104
|
|
|
* @return Successful|Error |
|
105
|
|
|
*/ |
|
106
|
|
|
public function refund(string $transactionId) |
|
107
|
|
|
{ |
|
108
|
|
|
return Transaction::refund($transactionId); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|