AllinPay.php 4.8 KB

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