WechatPayV3Default.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace app\common\service\pay\wechat;
  3. use app\common\enum\PayEnum;
  4. use app\common\model\UserAuth;
  5. use app\common\service\pay\base\BasePayService;
  6. use app\common\service\pay\YansongdaPay;
  7. use app\common\service\WeChatConfigService;
  8. use EasyWeChat\Factory;
  9. use think\facade\Log;
  10. use think\helper\Str;
  11. use WeChatPay\Builder;
  12. use WeChatPay\BuilderChainable;
  13. use WeChatPay\Crypto\AesGcm;
  14. use WeChatPay\Crypto\Rsa;
  15. use WeChatPay\Formatter;
  16. use WeChatPay\Util\PemUtil;
  17. use Yansongda\Pay\Pay;
  18. /**
  19. * @notes 微信支付 v3
  20. * author lbzy
  21. * @datetime 2024-11-11 13:55:30
  22. * @class WechatPayV3Default
  23. * @package app\common\service\pay\wechat
  24. */
  25. class WechatPayV3Default extends BasePayService
  26. {
  27. protected array $fromArray = [
  28. 'order' => [
  29. 'body' => '商品',
  30. 'attach' => 'order',
  31. ],
  32. 'recharge' => [
  33. 'body' => '充值',
  34. 'attach' => 'recharge',
  35. ],
  36. 'integral' => [
  37. 'body' => '积分商城',
  38. 'attach' => 'integral',
  39. ],
  40. ];
  41. /**
  42. * 授权信息
  43. * @var UserAuth|array|\think\Model
  44. */
  45. protected $auth;
  46. protected array|null $wechatTerminalConfig = null;
  47. /**
  48. * 支付实例
  49. * @var
  50. */
  51. protected $pay;
  52. function __construct($params)
  53. {
  54. parent::__construct($params);
  55. if ($this->user_id) {
  56. $this->auth = UserAuth::where([ 'user_id' => $this->user_id, 'terminal' => $this->terminal ])->findOrEmpty();
  57. }
  58. $this->wechatTerminalConfig = WeChatConfigService::getWechatConfigByTerminal($this->terminal);
  59. Pay::config(YansongdaPay::getWechatConfig($this->wechatTerminalConfig));
  60. }
  61. /**
  62. * @notes notes
  63. * https://pay.weixin.qq.com/docs/merchant/apis/mini-program-payment/mini-prepay.html
  64. * @return array|false
  65. * @throws \Exception
  66. * @author lbzy
  67. * @datetime 2024-11-18 15:48:39
  68. */
  69. function payMnp() : bool|array
  70. {
  71. if (empty($this->auth['id'])) {
  72. throw new \Exception('获取授权信息失败');
  73. }
  74. $order = $this->payOrder;
  75. $suffix = mb_substr(time(), -4);
  76. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  77. $result = Pay::wechat()->mini([
  78. 'out_trade_no' => $out_trade_no,
  79. 'description' => $this->fromArray[$this->payAttach]['body'],
  80. 'amount' => [
  81. 'total' => (int) bcmul($order['order_amount'], 100),
  82. 'currency' => 'CNY',
  83. ],
  84. 'payer' => [
  85. 'openid' => $this->auth['openid'],
  86. ],
  87. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  88. ]);
  89. if (! isset($result['paySign'])) {
  90. throw new \Exception($result['message'] ?? '发起支付失败');
  91. }
  92. return $result->toArray();
  93. }
  94. function payOa()
  95. {
  96. return $this->payMnp();
  97. }
  98. function payIos()
  99. {
  100. $order = $this->payOrder;
  101. $suffix = mb_substr(time(), -4);
  102. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  103. $result = Pay::wechat()->app([
  104. 'out_trade_no' => $out_trade_no,
  105. 'description' => $this->fromArray[$this->payAttach]['body'],
  106. 'amount' => [
  107. 'total' => (int) bcmul($order['order_amount'], 100),
  108. 'currency' => 'CNY'
  109. ],
  110. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  111. ]);
  112. if (! isset($result['paySign'])) {
  113. throw new \Exception($result['message'] ?? '发起支付失败');
  114. }
  115. return $result->toArray();
  116. }
  117. function payAndroid()
  118. {
  119. return $this->payIos();
  120. }
  121. function payH5()
  122. {
  123. $order = $this->payOrder;
  124. $suffix = mb_substr(time(), -4);
  125. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  126. $result = Pay::wechat()->h5([
  127. 'out_trade_no' => $out_trade_no,
  128. 'description' => $this->fromArray[$this->payAttach]['body'],
  129. 'amount' => [
  130. 'total' => (int) bcmul($order['order_amount'], 100),
  131. 'currency' => 'CNY'
  132. ],
  133. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  134. 'scene_info' => [
  135. 'payer_client_ip' => request()->ip(),
  136. 'h5_info' => [
  137. 'type' => 'Wap',
  138. ]
  139. ],
  140. ]);
  141. if (! isset($result['h5_url'])) {
  142. throw new \Exception($result['message'] ?? '发起支付失败');
  143. }
  144. $redirect_url = request()->domain() . '/mobile/bundle/pages/h5_pay_query/h5_pay_query?pay_way='.PayEnum::WECHAT_PAY;
  145. $redirect_url = urlencode($redirect_url);
  146. return $result['h5_url'] . '&redirect_url=' . $redirect_url;
  147. }
  148. function payPc()
  149. {
  150. $order = $this->payOrder;
  151. $suffix = mb_substr(time(), -4);
  152. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  153. $result = Pay::wechat()->scan([
  154. 'out_trade_no' => $out_trade_no,
  155. 'description' => $this->fromArray[$this->payAttach]['body'],
  156. 'amount' => [
  157. 'total' => (int) bcmul($order['order_amount'], 100),
  158. 'currency' => 'CNY'
  159. ],
  160. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  161. ]);
  162. if (! isset($result['code_url'])) {
  163. throw new \Exception($result['message'] ?? '发起支付失败');
  164. }
  165. return [
  166. 'code_url' => $result['code_url'],
  167. 'order_amount' => $order['order_amount'],
  168. ];
  169. }
  170. function payNotify()
  171. {
  172. try {
  173. $data = Pay::wechat()->callback()->toArray()['resource']['ciphertext'] ?? [];
  174. // Log::write($data, 'payNotifyData');
  175. if (! is_array($data)) {
  176. return $data;
  177. }
  178. if ($data['trade_state'] == 'SUCCESS') {
  179. $extra = [
  180. 'transaction_id' => $data['transaction_id'],
  181. ];
  182. $this->paySuccess([
  183. 'extra' => $extra,
  184. 'from' => $data['attach'],
  185. 'out_trade_no' => mb_substr($data['out_trade_no'], 0, 18),
  186. 'transaction_id' => $data['transaction_id'],
  187. ]);
  188. return Pay::wechat()->success();
  189. }
  190. return true;
  191. } catch(\Throwable $e) {
  192. Log::write($e->__toString(), 'payNotifyError');
  193. return false;
  194. }
  195. }
  196. function queryPay()
  197. {
  198. }
  199. /**
  200. * @notes https://pay.weixin.qq.com/doc/v3/merchant/4012556453
  201. * @return
  202. * @author lbzy
  203. * @datetime 2024-11-21 09:23:49
  204. */
  205. function refund()
  206. {
  207. $data = $this->params['refund_data'];
  208. $order = [
  209. 'transaction_id' => $data['transaction_id'],
  210. 'out_refund_no' => $data['refund_sn'],
  211. 'amount' => [
  212. 'total' => (int) bcmul($data['total_fee'], 100),
  213. 'currency' => 'CNY',
  214. 'refund' => (int) bcmul($data['refund_fee'], 100),
  215. ],
  216. // '_action' => 'jsapi', // jsapi 退款,默认
  217. // '_action' => 'app', // app 退款
  218. // '_action' => 'combine', // 合单退款
  219. // '_action' => 'h5', // h5 退款
  220. // '_action' => 'mini', // 小程序退款
  221. // '_action' => 'native', // native 退款
  222. ];
  223. $result = Pay::wechat()->refund($order)->all();
  224. if (isset($result['status']) && in_array($result['status'], [ 'SUCCESS', 'PROCESSING' ]) ) {
  225. return true;
  226. }
  227. Log::write($result, 'wechatRefundError');
  228. $this->setStatus(false, '发起退款失败:' . ($result['message'] ?? ''));
  229. return false;
  230. }
  231. function queryRefund()
  232. {
  233. try {
  234. $refund = $this->params['query_refund_data'];
  235. $order = [
  236. 'out_refund_no' => $refund['sn'],
  237. '_action' => 'refund',
  238. // '_action' => 'refund_jsapi', // 查询 jsapi 退款订单,默认
  239. // '_action' => 'refund_app', // 查询 App 退款订单
  240. // '_action' => 'refund_h5', // 查询 H5 退款订单
  241. // '_action' => 'refund_mini', // 查询小程序退款订单
  242. // '_action' => 'refund_native', // 查询 Native 退款订单
  243. // '_action' => 'refund_combine', // 查询合单退款订单
  244. ];
  245. $result = Pay::wechat()->query($order)->all();
  246. if (isset($result['status']) && $result['status'] == 'SUCCESS') {
  247. return true;
  248. }
  249. if (isset($result['status']) && in_array($result['status'], [ 'CLOSED', 'ABNORMAL' ]) ) {
  250. Log::write($result, 'wechatQueryRefundError');
  251. return false;
  252. }
  253. // if (isset($result['status']) && $result['status'] == 'PROCESSING') {
  254. // return null;
  255. // }
  256. return $result;
  257. return true;
  258. } catch(\Throwable $e) {
  259. Log::write($e->__toString(), 'wechatQueryRefundError');
  260. return false;
  261. }
  262. }
  263. }