AllinPay.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\pay\extend\allinpay;
  12. /**
  13. *
  14. * Class AllinPay
  15. * @author 等风来
  16. * @email 136327134@qq.com
  17. * @date 2022/12/27
  18. * @package crmeb\services\pay\extend\allinpay
  19. */
  20. class AllinPay extends Client
  21. {
  22. //统一支付接口
  23. const UNITODER_PAY_API = 'unitorder/pay';
  24. //统一扫码接口
  25. const UNITORDER_SCANQRPAY = 'unitorder/scanqrpay';
  26. //退款
  27. const UNITODER_TRANX_REFUND = 'tranx/refund';
  28. //订单查询
  29. const UNITODER_TRANX_QUERY = 'tranx/query';
  30. const UNITODER_QPAY_AGREEAPPLY = 'qpay/agreeapply';
  31. //微信公众号内支付请求地址
  32. const UNITODER_H5UNIONPAY = 'https://vsp.allinpay.com/apiweb/h5unionpay/unionorder';
  33. /**
  34. * @var string
  35. */
  36. protected $api = '';
  37. /**
  38. * 支付类型
  39. * @var string
  40. */
  41. protected $payType = '';
  42. /**
  43. * @var string
  44. */
  45. protected $version = '';
  46. /**
  47. * @var string
  48. */
  49. protected $orderKey = 'reqsn';
  50. /**
  51. * @var int
  52. */
  53. protected $validtime = 720;
  54. /**
  55. * 创建支付订单
  56. * @param string $trxamt
  57. * @param string $orderId
  58. * @param string $body
  59. * @param string|null $openId
  60. * @param string|null $appid
  61. * @param string $frontUrl
  62. * @param string $remark
  63. * @return mixed
  64. */
  65. public function create(string $trxamt, string $orderId, string $body, string $returl = null, string $openId = null, string $appid = null, string $frontUrl = '', string $remark = '')
  66. {
  67. $totalFee = (int)bcmul($trxamt, '100');
  68. $data = [
  69. 'trxamt' => $totalFee,
  70. $this->orderKey => $orderId,
  71. 'validtime' => $this->validtime,
  72. 'notify_url' => $this->notifyUrl
  73. ];
  74. if ($body) {
  75. $data['body'] = $body;
  76. }
  77. if ($remark) {
  78. $data['remark'] = $remark;
  79. }
  80. if ($this->payType) {
  81. $data['paytype'] = $this->payType;
  82. }
  83. if ($returl) {
  84. $data['returl'] = $returl;
  85. }
  86. if ($openId) {
  87. $data['acct'] = $openId;
  88. }
  89. if ($appid) {
  90. $data['sub_appid'] = $appid;
  91. }
  92. if ($frontUrl) {
  93. $data['front_url'] = $frontUrl;
  94. }
  95. if ($this->version) {
  96. $data['version'] = $this->version;
  97. }
  98. $form = !$this->api;
  99. $api = $this->api;
  100. $this->version = '';
  101. $this->payType = '';
  102. $this->api = '';
  103. $this->orderKey = 'reqsn';
  104. $this->validtime = 720;
  105. return $this->send($api, ['data' => $data, 'form' => $form]);
  106. }
  107. /**
  108. * 微信H5支付
  109. * @param string $trxamt
  110. * @param string $orderId
  111. * @param string $body
  112. * @param string $returl
  113. * @param string $remark
  114. * @return mixed
  115. */
  116. public function h5Pay(string $trxamt, string $orderId, string $body, string $returl, string $remark = '')
  117. {
  118. $this->version = self::VERSION_NUM_12;
  119. return $this->create($trxamt, $orderId, $body, $returl, null, null, '', $remark);
  120. }
  121. /**
  122. * 微信js支付
  123. * @param string $trxamt
  124. * @param string $orderId
  125. * @param string $body
  126. * @param string $openId
  127. * @param string $appId
  128. * @param string $frontUrl
  129. * @param string $remark
  130. * @return mixed
  131. */
  132. public function wechatPay(string $trxamt, string $orderId, string $body, string $openId, string $appId, string $frontUrl, string $remark = '')
  133. {
  134. $this->api = self::UNITODER_PAY_API;
  135. return $this->create($trxamt, $orderId, $body, null, $openId, $appId, $frontUrl, $remark);
  136. }
  137. /**
  138. * app支付 微信和支付宝
  139. * @param string $trxamt
  140. * @param string $orderId
  141. * @param string $body
  142. * @param bool $isWechat
  143. * @param string $remark
  144. * @return mixed
  145. * @author 等风来
  146. * @email 136327134@qq.com
  147. * @date 2023/1/14
  148. */
  149. public function appPay(string $trxamt, string $orderId, string $body, string $appid, string $openid = null, bool $isWechat = true, string $remark = '')
  150. {
  151. $this->api = self::UNITODER_PAY_API;
  152. $this->payType = $isWechat ? 'A02' : 'A01';
  153. $this->version = self::VERSION_NUM_11;
  154. return $this->create($trxamt, $orderId, $body, null, $openid, $appid, '', $remark);
  155. }
  156. /**
  157. * PC 收银台支付
  158. * @param string $trxamt
  159. * @param string $orderId
  160. * @param string $body
  161. * @return mixed
  162. * @author 等风来
  163. * @email 136327134@qq.com
  164. * @date 2023/1/15
  165. */
  166. public function pcPay(string $trxamt, string $orderId, string $body, string $remark, bool $isWechat = true)
  167. {
  168. $this->api = self::UNITODER_PAY_API;
  169. $this->payType = $isWechat ? 'W01' : 'A01';
  170. $this->version = self::VERSION_NUM_11;
  171. $res = $this->create($trxamt, $orderId, $body, null, null, null, '', $remark);
  172. $invalid = time() + 60;
  173. if ($isWechat) {
  174. $key = 'code_url';
  175. } else {
  176. $key = 'qrCode';
  177. }
  178. return ['invalid' => $invalid, 'logo' => sys_config('wap_login_logo'), $key => $res['payinfo']];
  179. }
  180. /**
  181. * @param string $trxamt
  182. * @param string $orderId
  183. * @param string $body
  184. * @param string $returl
  185. * @param string $remark
  186. * @return array
  187. * @author 等风来
  188. * @email 136327134@qq.com
  189. * @date 2023/1/15
  190. */
  191. public function miniproPay(string $trxamt, string $orderId, string $body, string $remark = '')
  192. {
  193. $totalFee = bcmul($trxamt, '100');
  194. $data = [
  195. 'paytype' => 'W06',
  196. 'trxamt' => $totalFee,
  197. 'reqsn' => $orderId,
  198. 'notify_url' => $this->notifyUrl,
  199. 'body' => $body,
  200. 'validtime' => (string)$this->validtime,
  201. 'cusid' => $this->cusid,
  202. 'appid' => $this->appid,
  203. 'signtype' => $this->signType,
  204. 'randomstr' => uniqid(),
  205. 'remark' => $remark,
  206. 'version' => self::VERSION_NUM_12
  207. ];
  208. $data['sign'] = $this->sign($data);
  209. return $data;
  210. }
  211. public function agreeapply()
  212. {
  213. $data = [
  214. 'meruserid' => 'eesssxxx',
  215. 'accttype' => '00',
  216. 'acctno' => '',
  217. 'idtype' => 0,
  218. 'idno' => '',
  219. 'acctname' => '',
  220. 'mobile' => '',
  221. 'cvv2' => '',
  222. 'reqip' => request()->ip(),
  223. 'reqtime' => date('Y-m-d H:i:s'),
  224. 'version' => self::VERSION_NUM_11,
  225. ];
  226. $data['cusid'] = $this->cusid;
  227. $data['appid'] = $this->appid;
  228. $data['signtype'] = $this->signType;
  229. $data['randomstr'] = uniqid();
  230. $data['sign'] = $this->sign($data);
  231. return $this->request(self::UNITODER_QPAY_AGREEAPPLY, $data);
  232. }
  233. /**
  234. * 查询订单
  235. * @param string $reqsn
  236. * @return array|mixed
  237. * @author 等风来
  238. * @email 136327134@qq.com
  239. * @date 2023/1/15
  240. */
  241. public function query(string $reqsn)
  242. {
  243. $data = [
  244. 'reqsn' => $reqsn
  245. ];
  246. return $this->send(self::UNITODER_TRANX_QUERY, ['data' => $data]);
  247. }
  248. /**
  249. * 发起退款
  250. * @param string $trxamt
  251. * @param string $reqsn
  252. * @return array|mixed
  253. * @author 等风来
  254. * @email 136327134@qq.com
  255. * @date 2023/1/15
  256. */
  257. public function refund(string $trxamt, string $orderId, string $reqsn)
  258. {
  259. $totalFee = (int)bcmul($trxamt, '100');
  260. $data = [
  261. 'trxamt' => $totalFee,
  262. 'reqsn' => $orderId,
  263. 'oldtrxid' => $reqsn,
  264. ];
  265. return $this->send(self::UNITODER_TRANX_REFUND, ['data' => $data]);
  266. }
  267. /**
  268. * 异步回调
  269. * @param callable $callback
  270. * @return string
  271. * @author 等风来
  272. * @email 136327134@qq.com
  273. * @date 2023/1/15
  274. */
  275. public function handleNotify(callable $callback)
  276. {
  277. $params = [];
  278. foreach (request()->post() as $key => $val) {
  279. $params[$key] = $val;
  280. }
  281. $this->debugLog('通联支付回调数据' . json_encode($params));
  282. if (count($params) < 1) {
  283. //如果参数为空,则不进行处理
  284. return "error";
  285. }
  286. $res = $this->validSign($params);
  287. $this->debugLog('通联支付回调验证:' . json_encode(['res' => $res]));
  288. if ($res && isset($params['trxstatus']) && $params['trxstatus'] === '0000') {
  289. //验签成功
  290. return $callback($params) ? 'success' : 'error';
  291. } else {
  292. return "error";
  293. }
  294. }
  295. }