Issues (38)

src/Service/Paypal/PaymentService.php (2 issues)

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\HttpException;
11
use PayPalHttp\HttpResponse;
12
use Exception;
13
14
/**
15
 * Class PaymentService
16
 * @package App\Service\Paypal
17
 */
18
class PaymentService extends AbstractPaypalService
19
{
20
    /**
21
     * @return PayPalHttpClient
22
     */
23
    public function getHttpClient(): PayPalHttpClient
24
    {
25
        $sandboxEnvironment = new SandboxEnvironment($this->clientId, $this->clientSecret);
26
        return new PayPalHttpClient($sandboxEnvironment);
27
    }
28
29
    /**
30
     * @param string $orderId
31
     * @return object|array
32
     */
33
    public function capturePayment(string $orderId):  object|array
34
    {
35
        try {
36
            $request = new OrdersCaptureRequest($orderId);
37
            $request->headers["prefer"] = "return=representation";
38
            $this->addNegativeTestingSetting($request);
39
            $client = $this->getHttpClient();
40
            $response = $client->execute($request);
41
        } catch (HttpException $e) {
42
            $this->logger->error('Error on PayPal::capturePayment = ' . $e->getMessage());
43
            return [
44
                'code' => $e->statusCode,
45
                'message' => json_decode($e->getMessage()),
46
                'headers' => $e->headers
47
            ];
48
        }
49
50
        return $response->result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->result could return the type string which is incompatible with the type-hinted return array|object. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    /**
54
     * @param string $body
55
     * @param array $headers
56
     * @return object|array
57
     */
58
    public function createOrder(string $body, array $headers = []): object|array
59
    {
60
        try {
61
            $request = new OrdersCreateRequest();
62
            $request->body = $body;
63
            $request->headers = array_merge($request->headers, $headers);
64
            $this->addNegativeTestingSetting($request);
65
            $client = $this->getHttpClient();
66
            $response = $client->execute($request);
67
        } catch (HttpException $e) {
68
            $this->logger->error('Error on PayPal::createOrder = ' . $e->getMessage());
69
            return [
70
                'code' => $e->statusCode,
71
                'message' => json_decode($e->getMessage()),
72
                'headers' => $e->headers
73
            ];
74
        }
75
76
        return $response->result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->result could return the type string which is incompatible with the type-hinted return array|object. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
}
79