romeritoCL /
paypal-playground
| 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 | * @param string|null $merchantAccountId |
||
| 21 | * @return string|null |
||
| 22 | */ |
||
| 23 | public function getClientToken(string $customerId = null, string $merchantAccountId = null): ?string |
||
| 24 | { |
||
| 25 | try { |
||
| 26 | if ($customerId !== null) { |
||
| 27 | return $this->gateway->clientToken()->generate( |
||
| 28 | [ |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 29 | 'customerId' => $customerId, |
||
| 30 | 'merchantAccountId' => $merchantAccountId ?? |
||
| 31 | $this->settingsService->getSetting('settings-merchant-maid'), |
||
| 32 | ] |
||
| 33 | ); |
||
| 34 | } |
||
| 35 | return $this->gateway->clientToken()->generate(); |
||
| 36 | } catch (Exception $exception) { |
||
| 37 | $this->logger->error( |
||
| 38 | 'Error on ' . __CLASS__ . '->' . __FUNCTION__ . ': ' . $exception->getMessage() |
||
| 39 | ); |
||
| 40 | } |
||
| 41 | return null; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param $amount |
||
| 46 | * @param $paymentNonce |
||
| 47 | * @param $deviceDataFromTheClient |
||
| 48 | * @param $serverOptions |
||
| 49 | * @return Error|Successful |
||
| 50 | */ |
||
| 51 | public function createSale($amount, $paymentNonce, $deviceDataFromTheClient, $serverOptions) |
||
| 52 | { |
||
| 53 | $lineItem = [ |
||
| 54 | 'kind' => TransactionLineItem::DEBIT, |
||
| 55 | 'name' => $this->settingsService->getSetting('settings-item-name'), |
||
| 56 | 'description' => $this->settingsService->getSetting('settings-item-description'), |
||
| 57 | 'productCode' => $this->settingsService->getSetting('settings-item-sku'), |
||
| 58 | 'totalAmount' => $this->settingsService->getSetting('settings-item-price'), |
||
| 59 | 'unitAmount' => $this->settingsService->getSetting('settings-item-price'), |
||
| 60 | 'quantity' => 1 |
||
| 61 | ]; |
||
| 62 | |||
| 63 | $defaultOptions = [ |
||
| 64 | 'merchantAccountId' => $merchantAccountId ?? |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 65 | $this->settingsService->getSetting('settings-merchant-maid'), |
||
| 66 | 'lineItems' => [$lineItem], |
||
| 67 | 'amount' => $amount, |
||
| 68 | 'paymentMethodNonce' => $paymentNonce, |
||
| 69 | 'deviceData' => $deviceDataFromTheClient, |
||
| 70 | 'options' => [ |
||
| 71 | 'submitForSettlement' => false, |
||
| 72 | ] |
||
| 73 | ]; |
||
| 74 | $serverOptions = json_decode($serverOptions, true); |
||
| 75 | return $this->gateway->transaction()->sale(array_replace_recursive($defaultOptions, $serverOptions)); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param $transactionId |
||
| 80 | * @param $amount |
||
| 81 | * @return Error|Successful |
||
| 82 | */ |
||
| 83 | public function captureSale($transactionId, $amount) |
||
| 84 | { |
||
| 85 | return $this->gateway->transaction()->submitForSettlement($transactionId, $amount); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param $transactionId |
||
| 90 | * @return Transaction |
||
| 91 | * @throws NotFound |
||
| 92 | */ |
||
| 93 | public function getTransaction($transactionId) |
||
| 94 | { |
||
| 95 | return $this->gateway->transaction()->find($transactionId); |
||
| 96 | } |
||
| 97 | } |
||
| 98 |