Sdk.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace addons\eepay;
  3. use addons\third\model\Third;
  4. use app\common\library\Auth;
  5. use Exception;
  6. use think\Session;
  7. use Yansongda\Pay\Pay;
  8. use Yansongda\Supports\Str;
  9. class Sdk
  10. {
  11. /**
  12. * 支付网关
  13. * @var string
  14. */
  15. protected $url;
  16. /**
  17. * 商户秘钥
  18. * @var string
  19. */
  20. protected $key;
  21. /**
  22. * 参数列表
  23. * @var array
  24. */
  25. protected $param = [
  26. 'sitename' => '在线支付',
  27. 'sign_type' => 'MD5',
  28. ];
  29. /**
  30. * 提交订单
  31. * @access public
  32. * @return $this
  33. * @throws EpayException
  34. */
  35. public function submit()
  36. {
  37. // 验证参数
  38. $this->verifyParam();
  39. // 设置默认参数
  40. $this->setDefaultParam();
  41. // 设置Sign签名
  42. $this->sign($this->getSign());
  43. return $this;
  44. }
  45. /**
  46. * 清空参数
  47. * @access public
  48. * @return $this
  49. */
  50. public function clearParam()
  51. {
  52. // 清空参数
  53. $this->param = [];
  54. return $this;
  55. }
  56. /**
  57. * 获取HTML FORM表单
  58. * @access public
  59. * @return string
  60. */
  61. public function getHtmlForm()
  62. {
  63. $html = '<form id="epay-submit" name="epay-submit" action="' . $this->url() . '/submit.php" method="POST">';
  64. foreach ($this->param as $key => $val) {
  65. $html .= '<input type="hidden" name="' . $key . '" value="' . $val . '">';
  66. }
  67. $html .= /** @lang text */
  68. '<input type="submit" value="正在提交"></form>';
  69. $html .= /** @lang text */
  70. '<script>document . forms[\'epay-submit\'].submit();</script>';
  71. // 清空参数
  72. $this->clearParam();
  73. return $html;
  74. }
  75. // public function getHtmlForm()
  76. // {
  77. // if($this->param['type'] =='alipay'){
  78. // if (strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') !== false) {
  79. // $html = '<form id="epay-submit" name="epay-submit" action="' . $this->url() . '/submit.php" method="POST">';
  80. // foreach ($this->param as $key => $val) {
  81. // // $html .= '<input type="hidden" name="' . $key . '" value="' . $val . '">';
  82. // $html .= '<input type="hidden" name="' . $key . '" value="' . $val . '">';
  83. // }
  84. // $html .= '<input type="hidden" value="正在提交"></form>';
  85. // $html .='<div style="background:#000;height:100%;display:block;">
  86. // <img style="width:100%;" src="/475885449890d32ad89fa4a28a83769.png"/>
  87. // <div>';
  88. // // $html .= /** @lang text */
  89. // // '<script>document . forms[\'epay-submit\'].submit();</script>';
  90. // // 清空参数
  91. // $this->clearParam();
  92. // return $html;
  93. // }else{
  94. // $html = '<form id="epay-submit" name="epay-submit" action="' . $this->url() . '/submit.php" method="POST">';
  95. // foreach ($this->param as $key => $val) {
  96. // // $html .= '<input type="hidden" name="' . $key . '" value="' . $val . '">';
  97. // $html .= '<input type="hidden" name="' . $key . '" value="' . $val . '">';
  98. // }
  99. // $html .= '<input type="submit" value="正在提交"></form>';
  100. // $html .= /** @lang text */
  101. // '<script>document . forms[\'epay-submit\'].submit();</script>';
  102. // // 清空参数
  103. // $this->clearParam();
  104. // return $html;
  105. // }
  106. // }
  107. // }
  108. /**
  109. * 签名验证
  110. * @access public
  111. * @param array $param
  112. * @return bool
  113. * @throws EpayException
  114. */
  115. public function signVerify($param = null)
  116. {
  117. if (is_null($param)) {
  118. $param = $_REQUEST;
  119. }
  120. // 赋值参数
  121. $this->param = $param;
  122. if (!isset($param['sign'])) {
  123. return false;
  124. }
  125. // 验证参数
  126. $this->verifyParam(['key']);
  127. return $param['sign'] === $this->getSign();
  128. }
  129. /**
  130. * 设置默认参数
  131. * @access public
  132. * @return void
  133. */
  134. protected function setDefaultParam()
  135. {
  136. // 默认订单名称
  137. if (empty($this->name())) {
  138. $this->name("# {$this->outTradeNo()} 在线支付");
  139. }
  140. }
  141. /**
  142. * 获取Sign
  143. * @access protected
  144. * @return string
  145. */
  146. protected function getSign()
  147. {
  148. $string = '';
  149. $param = $this->param;
  150. // 对待签名参数数组排序
  151. ksort($param);
  152. reset($param);
  153. foreach ($param as $k => $v) {
  154. if ('sign' !== $k && 'sign_type' !== $k && '' !== trim($v) && $v !== null) {
  155. $string .= $k . '=' . $v . '&';
  156. }
  157. }
  158. //去掉最后一个&字符
  159. $string = substr($string, 0, strlen($string) - 1);
  160. $string .= $this->key();
  161. //如果存在转义字符,那么去掉转义
  162. if (get_magic_quotes_gpc()) {
  163. $string = stripslashes($string);
  164. }
  165. return md5($string);
  166. }
  167. /**
  168. * 验证参数
  169. * @access protected
  170. * @param array $item 验证项
  171. * @return void
  172. * @throws EpayException
  173. */
  174. protected function verifyParam($item = null)
  175. {
  176. if (is_null($item)) {
  177. $item = ['pid', 'key', 'url', 'out_trade_no', 'notify_url', 'return_url', 'money'];
  178. }
  179. foreach ($item as $argsName) {
  180. if (empty($this->$argsName())) {
  181. $this->showMessage('参数‘' . $argsName . '’不能为空');
  182. }
  183. }
  184. }
  185. /**
  186. * 显示消息
  187. * @access protected
  188. * @return void
  189. * @throws EpayException
  190. */
  191. protected function showMessage()
  192. {
  193. $content = '<div style="text-align: center;margin-top: 50px;color: #FF0000">';
  194. foreach (func_get_args() as $args) {
  195. $content .= '<h4>' . $args . '</h4>';
  196. }
  197. $content .= '<div>';
  198. throw new EpayException($content);
  199. }
  200. /**
  201. * 驼峰转下划线
  202. * @access protected
  203. * @param string $str
  204. * @return string
  205. */
  206. protected function snake($str)
  207. {
  208. $str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
  209. return '_' . strtolower($matches[0]);
  210. }, $str);
  211. return $str;
  212. }
  213. /**
  214. * 设置支付网关
  215. * @access public
  216. * @param string $value
  217. * @return $this|string
  218. */
  219. public function url($value = null)
  220. {
  221. if (is_null($value)) {
  222. return $this->url;
  223. }
  224. $this->url = $value;
  225. return $this;
  226. }
  227. /**
  228. * 设置商户秘钥
  229. * @access public
  230. * @param string $value
  231. * @return $this|string
  232. */
  233. public function key($value = null)
  234. {
  235. if (is_null($value)) {
  236. return $this->key;
  237. }
  238. $this->key = $value;
  239. return $this;
  240. }
  241. /**
  242. * 参数操作
  243. * @access public
  244. * @param string $name
  245. * @param array $arguments
  246. * @return mixed|null|$this
  247. */
  248. public function __call($name, $arguments)
  249. {
  250. // 驼峰转下划线
  251. $name = $this->snake($name);
  252. // 获取参数值
  253. if (empty($arguments)) {
  254. return isset($this->param[$name]) ? trim($this->param[$name]) : null;
  255. }
  256. // 参数赋值
  257. $this->param[$name] = $arguments[0];
  258. return $this;
  259. }
  260. }