Completed
Push — master ( d601ba...2d4509 )
by Cesar
21s queued 15s
created

PaymentService::createOrder()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 5
nop 2
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace App\Service\Paypal;
4
5
use PayPal\Api\Payment;
6
use PayPalCheckoutSdk\Core\PayPalHttpClient;
7
use PayPalCheckoutSdk\Core\SandboxEnvironment;
8
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
9
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
10
use PayPalHttp\HttpResponse;
11
use Exception;
12
13
/**
14
 * Class PaymentService
15
 * @package App\Service\Paypal
16
 */
17
class PaymentService extends AbstractPaypalService
18
{
19
    /**
20
     * @return PayPalHttpClient
21
     */
22
    public function getHttpClient(): PayPalHttpClient
23
    {
24
        $sandboxEnvironment = new SandboxEnvironment($this->clientId, $this->clientSecret);
25
        return new PayPalHttpClient($sandboxEnvironment);
26
    }
27
28
    /**
29
     * @param string $orderId
30
     * @return Payment|null
31
     */
32
    public function capturePayment(string $orderId): ?HttpResponse
33
    {
34
        try {
35
            $request = new OrdersCaptureRequest($orderId);
36
            $request->headers["prefer"] = "return=representation";
37
            $client = $this->getHttpClient();
38
            $response = $client->execute($request);
39
        } catch (Exception $e) {
40
            $this->logger->error('Error on PayPal::capturePayment = ' . $e->getMessage());
41
            return null;
42
        }
43
44
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type PayPalHttp\HttpResponse which is incompatible with the documented return type PayPal\Api\Payment|null.
Loading history...
45
    }
46
47
    /**
48
     * @param string $body
49
     * @param array $headers
50
     * @return array|string
51
     */
52
    public function createOrder(string $body, array $headers = []): object|null
53
    {
54
        try {
55
            $request = new OrdersCreateRequest();
56
            $request->body = $body;
57
            $request->headers = array_merge($request->headers, $headers);
58
            $client = $this->getHttpClient();
59
            $response = $client->execute($request);
60
        } catch (Exception $e) {
61
            $this->logger->error('Error on PayPal::createOrder = ' . $e->getMessage());
62
            return null;
63
        }
64
65
        return $response->result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->result returns the type array|string which is incompatible with the type-hinted return null|object.
Loading history...
66
    }
67
}
68