123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
-
-
- namespace MirfalahTech\Laravel\Payment\Gateway\SenangPay;
-
-
- use Illuminate\Contracts\Config\Repository;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Response as Res;
- use MirfalahTech\Laravel\Payment\Contracts\PayableEntity;
- use MirfalahTech\Laravel\Payment\Contracts\PaymentGatewayDriver;
- use MirfalahTech\Laravel\Payment\Facade\Payment;
- use MirfalahTech\Laravel\Payment\Traits\BillStatusBoolean;
- use Symfony\Component\HttpFoundation\Response;
-
- class SenangPayDriver implements PaymentGatewayDriver
- {
- use BillStatusBoolean;
-
- /**
- * @var string
- */
- protected $endpoint;
-
- /**
- * @var string
- */
- protected $merchant_id;
-
- /**
- * @var string
- */
- protected $secret_key;
-
- /**
- * @var array
- */
- protected $keyMap;
-
- public function __construct(Repository $config, array $options)
- {
- $finalConfig = array_merge($config->get('payment.gateway'), $options);
- $this->endpoint = rtrim($finalConfig['endpoint'], '/');
- $this->merchant_id = $finalConfig['merchant_id'];
- $this->secret_key = $finalConfig['secret_key'];
- $this->keyMap = $finalConfig['key_map'];
- }
-
- public function createPaymentURL(PayableEntity $payable, array $options = []): string
- {
- return "$this->endpoint/payment/$this->merchant_id?" .
- http_build_query($this->preparePaymentParam($payable, $options));
- }
-
- public function verifyGatewayReturn(Request $request): bool
- {
- $hash = $request->query->get($this->keyMap['hash']);
- return $hash == $this->computeHash($request->request->all());
- }
-
- public function verifyGatewayCallback(Request $request, Response &$response = null): bool
- {
- $response = Res::make('OK');
- $hash = $request->request->get($this->keyMap['hash']);
- return $hash == $this->computeHash($request->request->all());
- }
-
- public function getBillIdFromRequest(Request $request): ?string
- {
- return $request->get($this->keyMap['order_id']);
- }
-
- public function getBillStatus(Request $request): ?int
- {
- $status = $request->get($this->keyMap['status_id']);
- if ($status == null) {
- return null;
- } else {
- return $status == 1 ? Payment::SUCCESS : Payment::PENDING;
- }
- }
-
- public function computeHash($payload)
- {
- $query = http_build_query($payload);
- $key = preg_quote(http_build_query([$this->keyMap['hash']=>'']), '/');
- $query = preg_replace('/(^|&)'.$key.'[^&]*($|&)/','$1hash=[HASH]$2', $query);
- return md5($this->secret_key . '?' . $query);
- }
-
- public function isPostMethodSupported(): bool
- {
- return true;
- }
-
- public function createPostMethodPayment(PayableEntity $payable, array $options = []): Response
- {
- $params = $this->preparePaymentParam($payable, $options);
- ob_start();
- include __DIR__.'../res/view/response.php';
- return new Response(ob_get_clean());
- }
-
- protected function preparePaymentParam(PayableEntity $payable, array $options = []): array{
- $detail = $payable->getBillDescription();
- $amount = $payable->getBillAmount();
- $order_id = $payable->getBillId();
-
- $detail = preg_replace('/[^A-Z0-9,\-_]/i', '_', $detail);
- $amount = sprintf('%0.2f', $amount);
- $hash = md5($this->secret_key . $detail . $amount . $order_id);
-
- return compact('detail', 'order_id', 'amount', 'hash');
- }
- }
|