AllinPay.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\services\pay\storage;
  14. use app\services\pay\PayServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\exceptions\PayException;
  17. use crmeb\services\pay\BasePay;
  18. use crmeb\services\pay\PayInterface;
  19. use crmeb\services\pay\extend\allinpay\AllinPay as AllinPayService;
  20. use EasyWeChat\Payment\Order;
  21. use think\facade\Event;
  22. /**
  23. * 通联支付
  24. * Class AllinPay
  25. * @author 等风来
  26. * @email 136327134@qq.com
  27. * @date 2023/2/1
  28. * @package crmeb\services\pay\storage
  29. */
  30. class AllinPay extends BasePay implements PayInterface
  31. {
  32. /**
  33. * @var AllinPayService
  34. */
  35. protected $pay;
  36. /**
  37. * @param array $config
  38. * @return mixed|void
  39. * @author 等风来
  40. * @email 136327134@qq.com
  41. * @date 2023/1/15
  42. */
  43. protected function initialize(array $config)
  44. {
  45. $this->pay = new AllinPayService([
  46. 'appid' => sys_config('allin_appid'),
  47. 'cusid' => sys_config('allin_cusid'),
  48. 'privateKey' => sys_config('allin_private_key'),
  49. 'publicKey' => sys_config('allin_public_key'),
  50. 'notifyUrl' => trim(sys_config('site_url')) . '/api/pay/notify/allin',
  51. 'isBeta' => false,
  52. ]);
  53. }
  54. /**
  55. * 创建支付
  56. * @param string $orderId
  57. * @param string $totalFee
  58. * @param string $attach
  59. * @param string $body
  60. * @param string $detail
  61. * @param array $options
  62. * @return array|mixed
  63. * @author 等风来
  64. * @email 136327134@qq.com
  65. * @date 2023/1/15
  66. */
  67. public function create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = [])
  68. {
  69. $this->authSetPayType();
  70. $options['returl'] = sys_config('site_url') . '/pages/index/index';
  71. if ($options['returl']) {
  72. $options['returl'] = str_replace('http://', 'https://', $options['returl']);
  73. }
  74. $options['appid'] = sys_config('routine_appId');
  75. $notifyUrl = trim(sys_config('site_url')) . '/api/pay/notify/allin' . $attach;
  76. $this->pay->setNotifyUrl($notifyUrl);
  77. switch ($this->payType) {
  78. case Order::APP:
  79. return $this->pay->appPay($totalFee, $orderId, $body, '', '', false, $attach);
  80. case Order::JSAPI:
  81. if (request()->isRoutine()) {
  82. return $this->pay->miniproPay($totalFee, $orderId, $body, $attach);
  83. } else {
  84. return $this->pay->h5Pay($totalFee, $orderId, $body, $options['returl'] ?? '', $attach);
  85. }
  86. case Order::NATIVE:
  87. return $this->pay->pcPay($totalFee, $orderId, $body, $attach, !empty($options['wechat']));
  88. default:
  89. throw new PayException('通联支付:支付类型错误或者暂不支持此环境下支付');
  90. }
  91. }
  92. public function merchantPay(string $openid, string $orderId, string $amount, array $options = [])
  93. {
  94. throw new PayException('通联支付:暂不支持商家转账');
  95. }
  96. /**
  97. * 发起退款
  98. * @param string $outTradeNo
  99. * @param array $options
  100. * @return array|mixed
  101. * @author 等风来
  102. * @email 136327134@qq.com
  103. * @date 2023/1/15
  104. */
  105. public function refund(string $outTradeNo, array $options = [])
  106. {
  107. $result = $this->pay->refund($options['refund_price'], $options['order_id'], $outTradeNo);
  108. if ($result['retcode'] != 'SUCCESS') throw new AdminException($result['retmsg']);
  109. }
  110. public function queryRefund(string $outTradeNo, string $outRequestNo, array $other = [])
  111. {
  112. // TODO: Implement queryRefund() method.
  113. }
  114. /**
  115. * 异步回调
  116. * @return mixed|string
  117. * @author 等风来
  118. * @email 136327134@qq.com
  119. * @date 2023/1/15
  120. */
  121. public function handleNotify(string $attach = '')
  122. {
  123. $attach = str_replace('allin', '', $attach);
  124. return $this->pay->handleNotify(function ($notify) use ($attach) {
  125. if (isset($notify['cusorderid'])) {
  126. $data = [
  127. 'attach' => $attach,
  128. 'out_trade_no' => $notify['cusorderid'],
  129. 'transaction_id' => $notify['trxid']
  130. ];
  131. return Event::until('NotifyListener', [$data, PayServices::ALLIN_PAY]);
  132. }
  133. return false;
  134. });
  135. }
  136. }