1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Service\Braintree; |
4
|
|
|
|
5
|
|
|
use Braintree\Result\Error; |
6
|
|
|
use Braintree\Result\Successful; |
7
|
|
|
use Braintree\Transaction; |
8
|
|
|
use Braintree\Exception\NotFound; |
9
|
|
|
use Braintree\TransactionLineItem; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PaymentService |
14
|
|
|
* @package App\Service\Braintree |
15
|
|
|
*/ |
16
|
|
|
class PaymentService extends AbstractBraintreeService |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param string|null $customerId |
20
|
|
|
* @return string|null |
21
|
|
|
*/ |
22
|
|
|
public function getClientToken(string $customerId = null): ?string |
23
|
|
|
{ |
24
|
|
|
try { |
25
|
|
|
if ($customerId !== null) { |
26
|
|
|
return $this->gateway->clientToken()->generate(['customerId' => $customerId]); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
return $this->gateway->clientToken()->generate(); |
29
|
|
|
} catch (Exception $exception) { |
30
|
|
|
$this->logger->error( |
31
|
|
|
'Error on ' . __CLASS__ . '->' . __FUNCTION__ . ': ' . $exception->getMessage() |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $amount |
39
|
|
|
* @param $paymentNonce |
40
|
|
|
* @param $deviceDataFromTheClient |
41
|
|
|
* @param $serverOptions |
42
|
|
|
* @return Error|Successful |
43
|
|
|
*/ |
44
|
|
|
public function createSale($amount, $paymentNonce, $deviceDataFromTheClient, $serverOptions) |
45
|
|
|
{ |
46
|
|
|
$lineItem = [ |
47
|
|
|
'kind' => TransactionLineItem::DEBIT, |
48
|
|
|
'name' => $this->settingsService->getSetting('settings-item-name'), |
49
|
|
|
'description' => $this->settingsService->getSetting('settings-item-description'), |
50
|
|
|
'productCode' => $this->settingsService->getSetting('settings-item-sku'), |
51
|
|
|
'totalAmount' => $this->settingsService->getSetting('settings-item-price'), |
52
|
|
|
'unitAmount' => $this->settingsService->getSetting('settings-item-price'), |
53
|
|
|
'quantity' => 1 |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$defaultOptions = [ |
57
|
|
|
'lineItems' => [$lineItem], |
58
|
|
|
'amount' => $amount, |
59
|
|
|
'paymentMethodNonce' => $paymentNonce, |
60
|
|
|
'deviceData' => $deviceDataFromTheClient, |
61
|
|
|
'options' => [ |
62
|
|
|
'submitForSettlement' => false, |
63
|
|
|
] |
64
|
|
|
]; |
65
|
|
|
$serverOptions = json_decode($serverOptions, true); |
66
|
|
|
return $this->gateway->transaction()->sale(array_replace_recursive($defaultOptions, $serverOptions)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $transactionId |
71
|
|
|
* @param $amount |
72
|
|
|
* @return Error|Successful |
73
|
|
|
*/ |
74
|
|
|
public function captureSale($transactionId, $amount) |
75
|
|
|
{ |
76
|
|
|
return $this->gateway->transaction()->submitForSettlement($transactionId, $amount); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $transactionId |
81
|
|
|
* @return Transaction |
82
|
|
|
* @throws NotFound |
83
|
|
|
*/ |
84
|
|
|
public function getTransaction($transactionId) |
85
|
|
|
{ |
86
|
|
|
return $this->gateway->transaction()->find($transactionId); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|