Passed
Pull Request — master (#44)
by Cesar
12:37
created

PaymentService::getClientToken()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
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]);
0 ignored issues
show
Bug introduced by
array('customerId' => $customerId) of type array<string,string> is incompatible with the type Braintree\Optional expected by parameter $params of Braintree\ClientTokenGateway::generate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
                return $this->gateway->clientToken()->generate(/** @scrutinizer ignore-type */ ['customerId' => $customerId]);
Loading history...
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