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.

130 lines
3.1 KiB

  1. <?php
  2. namespace App\Utilities\Zarinpal;
  3. use App\Utilities\Zarinpal\Drivers\RestDriver;
  4. use App\Utilities\Zarinpal\Drivers\DriverInterface;
  5. class Zarinpal
  6. {
  7. private $redirectUrl = 'https://www.zarinpal.com/pg/StartPay/%u';
  8. private $merchantID;
  9. private $driver;
  10. private $Authority;
  11. public function __construct($merchantID, DriverInterface $driver = null)
  12. {
  13. if (is_null($driver)) {
  14. $driver = new RestDriver();
  15. }
  16. $this->merchantID = $merchantID;
  17. $this->driver = $driver;
  18. }
  19. /**
  20. * send request for money to zarinpal
  21. * and redirect if there was no error.
  22. *
  23. * @param string $callbackURL
  24. * @param string $Amount
  25. * @param string $Description
  26. * @param string $Email
  27. * @param string $Mobile
  28. * @param null $additionalData
  29. *
  30. * @return array|@redirect
  31. */
  32. public function request($callbackURL, $Amount, $Description, $Email = null, $Mobile = null, $additionalData = null)
  33. {
  34. $inputs = [
  35. 'MerchantID' => $this->merchantID,
  36. 'CallbackURL' => $callbackURL,
  37. 'Amount' => $Amount,
  38. 'Description' => $Description,
  39. ];
  40. if (!is_null($Email)) {
  41. $inputs['Email'] = $Email;
  42. }
  43. if (!is_null($Mobile)) {
  44. $inputs['Mobile'] = $Mobile;
  45. }
  46. if (!is_null($additionalData)) {
  47. $inputs['AdditionalData'] = $additionalData;
  48. $results = $this->driver->requestWithExtra($inputs);
  49. } else {
  50. $results = $this->driver->request($inputs);
  51. }
  52. if (empty($results['Authority'])) {
  53. $results['Authority'] = null;
  54. }
  55. $this->Authority = $results['Authority'];
  56. return $results;
  57. }
  58. /**
  59. * verify that the bill is paid or not
  60. * by checking authority, amount and status.
  61. *
  62. * @param $amount
  63. * @param $authority
  64. *
  65. * @return array
  66. */
  67. public function verify($amount, $authority)
  68. {
  69. // backward compatibility
  70. if (count(func_get_args()) == 3) {
  71. $amount = func_get_arg(1);
  72. $authority = func_get_arg(2);
  73. }
  74. $inputs = [
  75. 'MerchantID' => $this->merchantID,
  76. 'Authority' => $authority,
  77. 'Amount' => $amount,
  78. ];
  79. return $this->driver->verifyWithExtra($inputs);
  80. }
  81. public function redirect()
  82. {
  83. header('Location: ' . sprintf($this->redirectUrl, $this->Authority));
  84. die;
  85. }
  86. /**
  87. * @return string
  88. */
  89. public function redirectUrl()
  90. {
  91. return sprintf($this->redirectUrl, $this->Authority);
  92. }
  93. /**
  94. * @return DriverInterface
  95. */
  96. public function getDriver()
  97. {
  98. return $this->driver;
  99. }
  100. /**
  101. * active sandbox mod for test env.
  102. */
  103. public function enableSandbox()
  104. {
  105. $this->redirectUrl = 'https://sandbox.zarinpal.com/pg/StartPay/%u';
  106. $this->getDriver()->enableSandbox();
  107. }
  108. /**
  109. * active zarinGate mode.
  110. */
  111. public function isZarinGate()
  112. {
  113. $this->redirectUrl = $this->redirectUrl . '/ZarinGate';
  114. }
  115. }