Payfort.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Payment\Lib;
  3. use Dever;
  4. class Payfort extends Core
  5. {
  6. private $config = array();
  7. private $language = 'en';
  8. private $url = 'https://checkout.payfort.com/FortAPI/paymentPage';
  9. public function __construct($name, $config)
  10. {
  11. $this->name = $name;
  12. $this->config = $config;
  13. }
  14. private function getType($type, $currency)
  15. {
  16. if ($type < 5 && $currency == 'SAR') {
  17. $type = 4;
  18. }
  19. $name = $value = $method = $command = '';
  20. switch ($type) {
  21. case 1:
  22. # 目前不支持
  23. $name = 'Pay with credit cards (Redirection)';
  24. $method = 'redirection';
  25. break;
  26. case 2:
  27. $name = 'Pay with installments (Redirection)';
  28. $method = 'redirection';
  29. break;
  30. case 3:
  31. $name = 'Pay with NAPS';
  32. $value = 'NAPS';
  33. $method = 'redirection';
  34. break;
  35. case 4:
  36. $name = 'Pay with SADAD';
  37. $value = 'SADAD';
  38. $currency = 'SAR';
  39. $method = 'redirection';
  40. $command = 'AUTHORIZATION';
  41. break;
  42. case 11:
  43. $name = 'Pay with credit cards (Merchant Page)';
  44. $method = 'page';
  45. $currency = '';
  46. $command = 'TOKENIZATION';
  47. break;
  48. case 12:
  49. $name = 'Pay with installments (Merchant Page)';
  50. $method = 'page';
  51. break;
  52. case 21:
  53. $name = 'Pay with credit cards (Merchant Page 2.0)';
  54. $method = 'page_v2';
  55. break;
  56. }
  57. $currency = strtoupper($currency);
  58. return array
  59. (
  60. 'id' => $type,
  61. 'name' => $name,
  62. 'value' => $value,
  63. 'method' => $method,
  64. 'currency' => $currency,
  65. 'command' => $command,
  66. );
  67. }
  68. /**
  69. * 获取统一下单的基本信息
  70. */
  71. public function order($uid, $account, $product_name, $amount, $currency, $type = 1)
  72. {
  73. $type = $this->getType($type, $currency);
  74. $order_id = $this->createOrder($uid, $this->name, $product_name, $amount, $type['currency'], $type['name']);
  75. $method = $type['method'];
  76. $param = array();
  77. if ($type['currency']) {
  78. $param['amount'] = $amount;
  79. $param['currency'] = $type['currency'];
  80. }
  81. $param = $this->param($order_id, $type['command'], $type['value'], $param);
  82. return $this->$method($param);
  83. }
  84. /**
  85. * 通知回调
  86. *
  87. * @return mixed
  88. */
  89. public function notify()
  90. {
  91. return 1;
  92. }
  93. /**
  94. * 生成基本参数
  95. *
  96. * @return mixed
  97. */
  98. private function param($order_id, $command, $payment_option, $param = array())
  99. {
  100. $param['access_code'] = $this->config['access_code'];
  101. $param['merchant_identifier'] = $this->config['merchant_id'];
  102. $param['merchant_reference'] = $order_id;
  103. $param['language'] = $this->language;
  104. if ($payment_option) {
  105. $param['payment_option'] = $payment_option;
  106. }
  107. # AUTHORIZATION(授权)、PURCHASE(购买) TOKENIZATION CHECK_STATUS
  108. $param['command'] = $command;
  109. $param['return_url'] = Dever::url('api.notify?order_id=' . $order_id, 'payment');
  110. $param['signature'] = $this->signature($this->config['request_phrase'], $param);
  111. return $param;
  112. }
  113. /**
  114. * 页面付款,下单后需要填写用户信息
  115. *
  116. * @return mixed
  117. */
  118. public function page($param)
  119. {
  120. print_r($param);die;
  121. $result = Dever::curl($this->url, $param, 'post');
  122. print_r($result);die;
  123. }
  124. /**
  125. * 直接付款 Redirection
  126. *
  127. * @return mixed
  128. */
  129. public function redirection($param)
  130. {
  131. $html = '<div>loading...</div><form method="post" action="'.$this->url.'" id="payment" name="payment" style="display:none;">';
  132. foreach ($param as $k => $v) {
  133. $html .= '<p>'.$k.':<input type="text" name="'.htmlentities($k).'" value="'.htmlentities($v).'"></p>';
  134. }
  135. $html .= '<input type="submit" />';
  136. $html .= '</form><script>document.getElementById("payment").submit();</script>';
  137. echo $html;die;
  138. }
  139. /**
  140. * signature
  141. *
  142. * @return mixed
  143. */
  144. private function signature($phrase, $request)
  145. {
  146. ksort($request);
  147. $signature_string = '';
  148. foreach ($request as $k => $v) {
  149. $signature_string .= $k . '=' . $v;
  150. }
  151. $signature_string = $phrase . $signature_string . $phrase;
  152. return hash('sha256', $signature_string);
  153. }
  154. }