You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SenangPayDriver.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace MirfalahTech\Laravel\Payment\Gateway\SenangPay;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Response as Res;
  5. use MirfalahTech\Laravel\Payment\Contracts\PayableEntity;
  6. use MirfalahTech\Laravel\Payment\Contracts\PaymentGatewayDriver;
  7. use MirfalahTech\Laravel\Payment\Facade\Payment;
  8. use MirfalahTech\Laravel\Payment\Traits\BillStatusBoolean;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class SenangPayDriver implements PaymentGatewayDriver
  11. {
  12. use BillStatusBoolean;
  13. /**
  14. * @var string
  15. */
  16. protected $endpoint;
  17. /**
  18. * @var string
  19. */
  20. protected $merchant_id;
  21. /**
  22. * @var string
  23. */
  24. protected $secret_key;
  25. public function __construct(string $endpoint, string $merchant_id, string $secret_key){
  26. $this->endpoint = rtrim($endpoint, '/');
  27. $this->merchant_id = $merchant_id;
  28. $this->secret_key = $secret_key;
  29. }
  30. public function createPaymentURL(PayableEntity $payable, array $options = []): string
  31. {
  32. $detail = $payable->getBillDescription();
  33. $amount = $payable->getBillAmount();
  34. $order_id = $payable->getBillId();
  35. $detail = preg_replace('/[^A-Z0-9,\-_]/i', '_', $detail);
  36. $amount = sprintf('%0.2f', $amount);
  37. $hash = md5($this->secret_key.$detail.$amount.$order_id);
  38. return "$this->endpoint/payment/$this->merchant_id?".
  39. http_build_query(compact('detail', 'order_id', 'amount', 'hash'));
  40. }
  41. public function verifyGatewayReturn(Request $request): bool
  42. {
  43. $hash = $request->query->get('hash');
  44. return $hash == $this->computeHash($request->request->all());
  45. }
  46. public function verifyGatewayCallback(Request $request, Response &$response = null): bool
  47. {
  48. $response = Res::make('OK');
  49. $hash = $request->request->get('hash');
  50. return $hash == $this->computeHash($request->request->all());
  51. }
  52. public function getBillIdFromRequest(Request $request): ?string{
  53. return $request->get('order_id');
  54. }
  55. public function getBillStatus(Request $request): ?int
  56. {
  57. return $request->request->get('status') == 1 ? Payment::SUCCESS : Payment::PENDING;
  58. }
  59. public function computeHash($payload){
  60. $payload['hash'] = '[HASH]';
  61. return md5($this->secret_key.'?'.http_build_query($payload));
  62. }
  63. }