WechatPayV3Default.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 ($this->auth->isEmpty()) {
  72. if (!empty($this->payOrder['openid'])) {
  73. $this->auth = ['openid' => $this->payOrder['openid']];
  74. } else {
  75. throw new \Exception('获取授权信息失败');
  76. }
  77. }
  78. $order = $this->payOrder;
  79. $suffix = mb_substr(time(), -4);
  80. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  81. $result = Pay::wechat()->mini([
  82. 'out_trade_no' => $out_trade_no,
  83. 'description' => $this->fromArray[$this->payAttach]['body'],
  84. 'amount' => [
  85. 'total' => (int) bcmul($order['order_amount'], 100),
  86. 'currency' => 'CNY',
  87. ],
  88. 'payer' => [
  89. 'openid' => $this->auth['openid'],
  90. ],
  91. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  92. ]);
  93. if (! isset($result['paySign'])) {
  94. throw new \Exception($result['message'] ?? '发起支付失败');
  95. }
  96. return $result->toArray();
  97. }
  98. function payOa()
  99. {
  100. return $this->payMnp();
  101. }
  102. function payIos()
  103. {
  104. $order = $this->payOrder;
  105. $suffix = mb_substr(time(), -4);
  106. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  107. $result = Pay::wechat()->app([
  108. 'out_trade_no' => $out_trade_no,
  109. 'description' => $this->fromArray[$this->payAttach]['body'],
  110. 'amount' => [
  111. 'total' => (int) bcmul($order['order_amount'], 100),
  112. 'currency' => 'CNY'
  113. ],
  114. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  115. ]);
  116. if (! isset($result['paySign'])) {
  117. throw new \Exception($result['message'] ?? '发起支付失败');
  118. }
  119. return $result->toArray();
  120. }
  121. function payAndroid()
  122. {
  123. return $this->payIos();
  124. }
  125. function payH5()
  126. {
  127. $order = $this->payOrder;
  128. $suffix = mb_substr(time(), -4);
  129. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  130. $result = Pay::wechat()->h5([
  131. 'out_trade_no' => $out_trade_no,
  132. 'description' => $this->fromArray[$this->payAttach]['body'],
  133. 'amount' => [
  134. 'total' => (int) bcmul($order['order_amount'], 100),
  135. 'currency' => 'CNY'
  136. ],
  137. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  138. 'scene_info' => [
  139. 'payer_client_ip' => request()->ip(),
  140. 'h5_info' => [
  141. 'type' => 'Wap',
  142. ]
  143. ],
  144. ]);
  145. if (! isset($result['h5_url'])) {
  146. throw new \Exception($result['message'] ?? '发起支付失败');
  147. }
  148. $redirect_url = request()->domain() . '/mobile/bundle/pages/h5_pay_query/h5_pay_query?pay_way='.PayEnum::WECHAT_PAY;
  149. $redirect_url = urlencode($redirect_url);
  150. return $result['h5_url'] . '&redirect_url=' . $redirect_url;
  151. }
  152. function payPc()
  153. {
  154. $order = $this->payOrder;
  155. $suffix = mb_substr(time(), -4);
  156. $out_trade_no = $order['sn'] . 'JSAPI' . $this->terminal . $suffix;
  157. $result = Pay::wechat()->scan([
  158. 'out_trade_no' => $out_trade_no,
  159. 'description' => $this->fromArray[$this->payAttach]['body'],
  160. 'amount' => [
  161. 'total' => (int) bcmul($order['order_amount'], 100),
  162. 'currency' => 'CNY'
  163. ],
  164. 'attach' => $this->fromArray[$this->payAttach]['attach'],
  165. ]);
  166. if (! isset($result['code_url'])) {
  167. throw new \Exception($result['message'] ?? '发起支付失败');
  168. }
  169. return [
  170. 'code_url' => $result['code_url'],
  171. 'order_amount' => $order['order_amount'],
  172. ];
  173. }
  174. function payNotify()
  175. {
  176. try {
  177. $data = Pay::wechat()->callback()->toArray()['resource']['ciphertext'] ?? [];
  178. // Log::write($data, 'payNotifyData');
  179. if (! is_array($data)) {
  180. return $data;
  181. }
  182. if ($data['trade_state'] == 'SUCCESS') {
  183. $extra = [
  184. 'transaction_id' => $data['transaction_id'],
  185. ];
  186. $this->paySuccess([
  187. 'extra' => $extra,
  188. 'from' => $data['attach'],
  189. 'out_trade_no' => mb_substr($data['out_trade_no'], 0, 18),
  190. 'transaction_id' => $data['transaction_id'],
  191. ]);
  192. return Pay::wechat()->success();
  193. }
  194. return true;
  195. } catch(\Throwable $e) {
  196. Log::write($e->__toString(), 'payNotifyError');
  197. return false;
  198. }
  199. }
  200. function queryPay()
  201. {
  202. }
  203. /**
  204. * @notes https://pay.weixin.qq.com/doc/v3/merchant/4012556453
  205. * @return
  206. * @author lbzy
  207. * @datetime 2024-11-21 09:23:49
  208. */
  209. function refund()
  210. {
  211. $data = $this->params['refund_data'];
  212. $order = [
  213. 'transaction_id' => $data['transaction_id'],
  214. 'out_refund_no' => $data['refund_sn'],
  215. 'amount' => [
  216. 'total' => (int) bcmul($data['total_fee'], 100),
  217. 'currency' => 'CNY',
  218. 'refund' => (int) bcmul($data['refund_fee'], 100),
  219. ],
  220. // '_action' => 'jsapi', // jsapi 退款,默认
  221. // '_action' => 'app', // app 退款
  222. // '_action' => 'combine', // 合单退款
  223. // '_action' => 'h5', // h5 退款
  224. // '_action' => 'mini', // 小程序退款
  225. // '_action' => 'native', // native 退款
  226. ];
  227. $result = Pay::wechat()->refund($order)->all();
  228. if (isset($result['status']) && in_array($result['status'], [ 'SUCCESS', 'PROCESSING' ]) ) {
  229. return true;
  230. }
  231. Log::write($result, 'wechatRefundError');
  232. $this->setStatus(false, '发起退款失败:' . ($result['message'] ?? ''));
  233. return false;
  234. }
  235. function queryRefund()
  236. {
  237. try {
  238. $refund = $this->params['query_refund_data'];
  239. $order = [
  240. 'out_refund_no' => $refund['sn'],
  241. '_action' => 'refund',
  242. // '_action' => 'refund_jsapi', // 查询 jsapi 退款订单,默认
  243. // '_action' => 'refund_app', // 查询 App 退款订单
  244. // '_action' => 'refund_h5', // 查询 H5 退款订单
  245. // '_action' => 'refund_mini', // 查询小程序退款订单
  246. // '_action' => 'refund_native', // 查询 Native 退款订单
  247. // '_action' => 'refund_combine', // 查询合单退款订单
  248. ];
  249. $result = Pay::wechat()->query($order)->all();
  250. if (isset($result['status']) && $result['status'] == 'SUCCESS') {
  251. return true;
  252. }
  253. if (isset($result['status']) && in_array($result['status'], [ 'CLOSED', 'ABNORMAL' ]) ) {
  254. Log::write($result, 'wechatQueryRefundError');
  255. return false;
  256. }
  257. // if (isset($result['status']) && $result['status'] == 'PROCESSING') {
  258. // return null;
  259. // }
  260. return $result;
  261. return true;
  262. } catch(\Throwable $e) {
  263. Log::write($e->__toString(), 'wechatQueryRefundError');
  264. return false;
  265. }
  266. }
  267. }