Passed
Pull Request — master (#45)
by Cesar
02:48
created

ForwardApiService   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A directTokenization() 0 29 2
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 $DIRECT_TOKEN_ENDPOINT = "https://forwarding.sandbox.braintreegateway.com/tsp";
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
            $ch = curl_init();
27
            curl_setopt($ch, CURLOPT_URL, $this->DIRECT_TOKEN_ENDPOINT);
28
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
29
            curl_setopt(
30
                $ch,
31
                CURLOPT_USERPWD,
32
                $this->gateway->config->getPublicKey() . ':' . $this->gateway->config->getPrivateKey()
33
            );
34
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
35
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
36
                'merchant_id' => $this->gateway->config->getMerchantId(),
37
                'payment_method_nonce' => $paymentNonce,
38
                'device_data' => $deviceData,
39
            ], true));
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $flags of json_encode(). ( Ignorable by Annotation )

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

39
            ], /** @scrutinizer ignore-type */ true));
Loading history...
40
41
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
42
            $result = curl_exec($ch);
43
            $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
44
            curl_close($ch);
45
        } catch (Exception $e) {
46
            $this->logger->error('Error on ForwardAPI::'.$this->DIRECT_TOKEN_ENDPOINT.' = ' . $e->getMessage());
47
            return null;
48
        }
49
        return ([
50
            'result' => (object) json_decode($result),
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
            'result' => (object) json_decode(/** @scrutinizer ignore-type */ $result),
Loading history...
51
            'statusCode' => $statusCode
52
        ]);
53
    }
54
}
55