|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WyChoong\Mpgs\Clients; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Exception; |
| 7 | +use Illuminate\Http\Client\Response; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | +use Illuminate\Support\Facades\Log; |
| 10 | + |
| 11 | +class Mpgs |
| 12 | +{ |
| 13 | + protected static string $gateway; |
| 14 | + |
| 15 | + protected static string $merchantId; |
| 16 | + |
| 17 | + protected static string $apiPassword; |
| 18 | + |
| 19 | + protected static string $version; |
| 20 | + |
| 21 | + private static Closure | array $setupUsing = []; |
| 22 | + |
| 23 | + public function __construct() |
| 24 | + { |
| 25 | + $this->setupClient(); |
| 26 | + } |
| 27 | + |
| 28 | + public static function setupClientUsing(Closure | array $setupUsing) |
| 29 | + { |
| 30 | + static::$setupUsing = $setupUsing; |
| 31 | + } |
| 32 | + |
| 33 | + public static function setupClient() |
| 34 | + { |
| 35 | + $params = static::$setupUsing; |
| 36 | + |
| 37 | + if ($params instanceof Closure) { |
| 38 | + $params = $params(); |
| 39 | + } |
| 40 | + |
| 41 | + foreach ($params as $key => $value) { |
| 42 | + if (in_array($key, ['gateway', 'merchantId', 'apiPassword', 'version']) && filled($value)) { |
| 43 | + static::$$key = $value; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + static::$gateway ??= static::config('gateway'); |
| 48 | + static::$merchantId ??= static::config('merchant_id'); |
| 49 | + static::$apiPassword ??= static::config('api_password'); |
| 50 | + static::$version ??= static::config('version'); |
| 51 | + } |
| 52 | + |
| 53 | + public static function config($key, $default = null): ?string |
| 54 | + { |
| 55 | + $value = config("lunar-mpgs.{$key}", $default); |
| 56 | + |
| 57 | + if (!$value && str($key)->contains('action.')) { |
| 58 | + $action = str($key)->afterLast('action.')->toString(); |
| 59 | + $value = match ($action) { |
| 60 | + 'initiate_checkout' => '/session', |
| 61 | + 'retrieve_session' => '/session/{sessionId}', |
| 62 | + 'retrieve_order' => '/order/{orderId}', |
| 63 | + default => throw new Exception('Invalid action'), |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + return $value; |
| 68 | + } |
| 69 | + |
| 70 | + protected function getUrl(string $action): string |
| 71 | + { |
| 72 | + return '{+gateway}/version/{version}/merchant/{merchantId}' . self::config("action.{$action}"); |
| 73 | + } |
| 74 | + |
| 75 | + protected function execute(string $method, string $action, array $data = [], array $urlParams = []): Response |
| 76 | + { |
| 77 | + $client = Http::withUrlParameters(array_merge([ |
| 78 | + 'gateway' => static::$gateway, |
| 79 | + 'version' => static::$version, |
| 80 | + 'merchantId' => static::$merchantId, |
| 81 | + ], $urlParams)); |
| 82 | + |
| 83 | + return $client |
| 84 | + ->withBasicAuth('merchant.' . static::$merchantId, static::$apiPassword) |
| 85 | + ->{$method}($this->getUrl($action), $data); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Create a MPGS checkout session and return the result. |
| 90 | + * |
| 91 | + */ |
| 92 | + public static function initiateCheckout(array $data) |
| 93 | + { |
| 94 | + /** @var self $static */ |
| 95 | + $static = app(static::class); |
| 96 | + |
| 97 | + $data['apiOperation'] = 'INITIATE_CHECKOUT'; |
| 98 | + $data['interaction']['operation'] = 'PURCHASE'; |
| 99 | + |
| 100 | + $response = $static->execute('post', 'initiate_checkout', data: $data); |
| 101 | + |
| 102 | + return $response->object(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Retrieve MPGS checkout session details |
| 107 | + * |
| 108 | + */ |
| 109 | + public static function retrieveSession(string $sessionId) |
| 110 | + { |
| 111 | + /** @var self $static */ |
| 112 | + $static = app(static::class); |
| 113 | + |
| 114 | + $response = $static->execute('get', 'retrieve_session', urlParams: [ |
| 115 | + 'sessionId' => $sessionId, |
| 116 | + ]); |
| 117 | + |
| 118 | + return $response->object(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Retrieve the order details from MPGS. |
| 123 | + * |
| 124 | + */ |
| 125 | + public static function retrieveOrder($orderId) |
| 126 | + { |
| 127 | + /** @var self $static */ |
| 128 | + $static = app(static::class); |
| 129 | + |
| 130 | + $response = $static->execute('get', 'retrieve_order', urlParams: [ |
| 131 | + 'orderId' => $orderId, |
| 132 | + ]); |
| 133 | + |
| 134 | + return $response->object(); |
| 135 | + } |
| 136 | +} |
0 commit comments