1 | <?php |
||
2 | |||
3 | namespace Sebdesign\VivaPayments; |
||
4 | |||
5 | use GuzzleHttp\Psr7\Uri; |
||
6 | use GuzzleHttp\RequestOptions; |
||
7 | use Psr\Http\Message\UriInterface; |
||
8 | use Sebdesign\VivaPayments\Requests\CreatePaymentOrder; |
||
9 | |||
10 | class Order |
||
11 | { |
||
12 | 3 | public function __construct(protected Client $client) |
|
13 | { |
||
14 | } |
||
15 | |||
16 | /** |
||
17 | * @see https://developer.vivawallet.com/apis-for-payments/payment-api/#tag/Payments/paths/~1checkout~1v2~1orders/post |
||
18 | * |
||
19 | * @param array<string,mixed> $guzzleOptions |
||
20 | */ |
||
21 | 3 | public function create(CreatePaymentOrder $order, array $guzzleOptions = []): string |
|
22 | { |
||
23 | 3 | $response = $this->client->post( |
|
24 | 3 | $this->client->getApiUrl()->withPath('/checkout/v2/orders'), |
|
25 | 3 | array_merge_recursive( |
|
26 | 1 | [RequestOptions::JSON => $order], |
|
27 | 3 | $this->client->authenticateWithBearerToken(), |
|
28 | $guzzleOptions, |
||
29 | ) |
||
30 | ); |
||
31 | |||
32 | 3 | return strval($response['orderCode'] ?? ''); |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * Get the redirect URL to the Smart Checkout for an order. |
||
37 | * |
||
38 | * @see https://developer.vivawallet.com/smart-checkout/smart-checkout-integration/#step-2-redirect-the-customer-to-smart-checkout-to-pay-the-payment-order |
||
39 | */ |
||
40 | 3 | public function redirectUrl( |
|
41 | string $ref, |
||
42 | ?string $color = null, |
||
43 | ?int $paymentMethod = null, |
||
44 | ): UriInterface { |
||
45 | 3 | return Uri::withQueryValues( |
|
46 | 3 | $this->client->getUrl()->withPath('/web/checkout'), |
|
47 | 3 | array_map(strval(...), array_filter([ |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
48 | 'ref' => $ref, |
||
49 | 'color' => $color, |
||
50 | 'paymentMethod' => $paymentMethod, |
||
51 | 3 | ], fn (int|string|null $value) => ! is_null($value)), |
|
52 | )); |
||
53 | } |
||
54 | } |
||
55 |