1final class OrderPaymentService
2{
3 public function pay(Order $order): string
4 {
5 $liqpay = new LiqPay(config('services.liqpay.public'), config('services.liqpay.private'));
6
7 $response = $liqpay->api('request', [
8 'action' => 'pay',
9 'version' => '3',
10 'amount' => $order->total / 100,
11 'currency' => 'UAH',
12 'order_id' => $order->number,
13 ]);
14
15 // RefundJob і SubscriptionRenewal роблять те саме своїми словами
16 return $this->parseLiqPayResponse($response);
17 }
18}
1<?php
2
31interface PaymentGateway
4{
5 public function charge(string $orderId, int $kopecks): string;
6}
7
8// Спрощені SDK платіжних сервісів: їх код не наш, і змінювати його ми не можемо.
9final class LiqPayClient
10{
11 /** @param array<string, string|float> $params */
12 public function api(string $path, array $params): array
13 {
14 return ['status' => 'success', 'payment_id' => 'lp-' . $params['order_id']];
15 }
16}
17
18final class MonobankAcquiring
19{
20 public function createInvoice(int $amount, int $ccy, string $reference): object
21 {
22 return (object) ['invoiceId' => 'mono-' . $reference, 'amount' => $amount, 'ccy' => $ccy];
23 }
24}
25
26final readonly class LiqPayGateway implements PaymentGateway
27{
28 public function __construct(private LiqPayClient $client) {}
29
302 public function charge(string $orderId, int $kopecks): string
31 {
32 $response = $this->client->api('request', [
33 'action' => 'pay',
34 'amount' => $kopecks / 100,
35 'currency' => 'UAH',
36 'order_id' => $orderId,
37 ]);
38
39 return $response['payment_id'];
40 }
41}
42
43final readonly class MonobankGateway implements PaymentGateway
44{
453 private const int UAH = 980;
46
47 public function __construct(private MonobankAcquiring $acquiring) {}
48
49 public function charge(string $orderId, int $kopecks): string
50 {
51 return $this->acquiring->createInvoice($kopecks, self::UAH, $orderId)->invoiceId;
52 }
53}
54
554final readonly class Checkout
56{
57 public function __construct(private PaymentGateway $payments) {}
58
59 public function pay(string $orderId, int $kopecks): string
60 {
61 $paymentId = $this->payments->charge($orderId, $kopecks);
62
63 return sprintf('%s на %.2f грн, платіж %s', $orderId, $kopecks / 100, $paymentId);
64 }
65}
66
67$gateways = [new LiqPayGateway(new LiqPayClient()), new MonobankGateway(new MonobankAcquiring())];
68
69foreach ($gateways as $gateway) {
70 echo str_pad($gateway::class, 17), (new Checkout($gateway))->pay('UA-1042', 185000), "\n";
71}