Completed
Pull Request — master (#4)
by Cesar
01:32
created

PaymentService::capturePayment()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 5
nop 1
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 PayPalHttp\HttpResponse;
10
use Exception;
11
12
/**
13
 * Class PaymentService
14
 * @package App\Service\Paypal
15
 */
16
class PaymentService extends AbstractPaypalService
17
{
18
    /**
19
     * @return PayPalHttpClient
20
     */
21
    public function getHttpClient(): PayPalHttpClient
22
    {
23
        $sandboxEnvironment = new SandboxEnvironment($this->clientId, $this->clientSecret);
24
        return new PayPalHttpClient($sandboxEnvironment);
25
    }
26
27
    /**
28
     * @param string $orderId
29
     * @return Payment|null
30
     */
31
    public function capturePayment(string $orderId): ?HttpResponse
32
    {
33
        try {
34
            $request = new OrdersCaptureRequest($orderId);
35
            $request->headers["prefer"] = "return=representation";
36
            $client = $this->getHttpClient();
37
            $response = $client->execute($request);
38
        } catch (Exception $e) {
39
            $this->logger->error('Error on PayPal::capturePayment = ' . $e->getMessage());
40
            return null;
41
        }
42
43
        return $response;
44
    }
45
}
46