1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
-
-
- namespace MirfalahTech\Laravel\Payment\Gateway\SenangPay;
-
-
- 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;
-
- public function __construct(string $endpoint, string $merchant_id, string $secret_key){
- $this->endpoint = rtrim($endpoint, '/');
- $this->merchant_id = $merchant_id;
- $this->secret_key = $secret_key;
- }
-
- public function createPaymentURL(PayableEntity $payable, array $options = []): string
- {
- $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 "$this->endpoint/payment/$this->merchant_id?".
- http_build_query(compact('detail', 'order_id', 'amount', 'hash'));
- }
-
- public function verifyGatewayReturn(Request $request): bool
- {
- $hash = $request->query->get('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('hash');
- return $hash == $this->computeHash($request->request->all());
- }
-
- public function getBillIdFromRequest(Request $request): ?string{
- return $request->get('order_id');
- }
-
- public function getBillStatus(Request $request): ?int
- {
- return $request->request->get('status') == 1 ? Payment::SUCCESS : Payment::PENDING;
- }
-
- public function computeHash($payload){
- $payload['hash'] = '[HASH]';
- return md5($this->secret_key.'?'.http_build_query($payload));
- }
- }
|