Wechat.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace app\common\Service\Pay\Provider;
  3. use think\Log;
  4. use think\Exception;
  5. use think\exception\HttpResponseException;
  6. use Yansongda\Pay\Pay;
  7. use app\common\Enum\ChannelEnum;
  8. /**
  9. * 微信支付提供器
  10. */
  11. class Wechat extends Base
  12. {
  13. /**
  14. * 构造函数
  15. * @param mixed $payService
  16. * @param string $platform
  17. * @param string $channel
  18. */
  19. public function __construct($payService, $platform = null, $channel = null)
  20. {
  21. parent::__construct($payService, $platform, $channel);
  22. }
  23. public function pay($order, $config = [])
  24. {
  25. $this->init('wechat', $config);
  26. if (isset($this->config['wechat']['default']['mode']) && $this->config['wechat']['default']['mode'] === 2) {
  27. if (in_array($this->platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {
  28. $order['payer']['sub_openid'] = $order['payer']['openid'] ?? '';
  29. unset($order['payer']['openid']);
  30. }
  31. }
  32. $order['amount']['total'] = intval(bcmul((string)$order['total_amount'], '100')); // 按分 为单位
  33. if ($this->platform == 'H5') {
  34. $order['_type'] = 'app'; // 使用 配置中的 app_id 字段
  35. $order['scene_info'] = [
  36. 'payer_client_ip' => request()->ip(),
  37. 'h5_info' => [
  38. 'type' => 'Wap',
  39. ]
  40. ];
  41. }
  42. unset($order['order_id'], $order['total_amount']);
  43. $method = $this->getMethod('wechat');
  44. $result = Pay::wechat()->$method($order);
  45. return $result;
  46. }
  47. public function transfer($payload, $config = [])
  48. {
  49. $this->init('wechat', $config, 'sub_mch');
  50. $code = 0;
  51. $payload['total_amount'] = intval(bcmul((string)$payload['total_amount'], '100'));
  52. foreach ($payload['transfer_detail_list'] as $key => &$detail) {
  53. $detail['transfer_amount'] = intval(bcmul((string)$detail['transfer_amount'], '100'));
  54. }
  55. if (isset($this->config['wechat']['default']['_type'])) {
  56. // 为了能正常获取 appid
  57. $payload['_type'] = $this->config['wechat']['default']['_type'];
  58. }
  59. // $payload['authorization_type'] = 'INFORMATION_AUTHORIZATION_TYPE';
  60. $payload['authorization_type'] = 'FUND_AUTHORIZATION_TYPE';
  61. // $payload['authorization_type'] = 'INFORMATION_AND_FUND_AUTHORIZATION_TYPE';
  62. $response = Pay::wechat()->transfer($payload);
  63. if (isset($response['batch_id']) && $response['batch_id']) {
  64. $code = 1;
  65. }
  66. return [$code, $response];
  67. }
  68. public function notify($callback, $config = [])
  69. {
  70. $this->init('wechat', $config);
  71. try {
  72. $originData = Pay::wechat()->callback(); // 是的,验签就这么简单!
  73. // {
  74. // "id": "a5c68a7c-5474-5151-825d-88b4143f8642",
  75. // "create_time": "2022-06-20T16:16:12+08:00",
  76. // "resource_type": "encrypt-resource",
  77. // "event_type": "TRANSACTION.SUCCESS",
  78. // "summary": "支付成功",
  79. // "resource": {
  80. // "original_type": "transaction",
  81. // "algorithm": "AEAD_AES_256_GCM",
  82. // "ciphertext": {
  83. // "mchid": "1623831039",
  84. // "appid": "wx43********d3d0",
  85. // "out_trade_no": "P202204155176122100021000",
  86. // "transaction_id": "4200001433202206201698588194",
  87. // "trade_type": "JSAPI",
  88. // "trade_state": "SUCCESS",
  89. // "trade_state_desc": "支付成功",
  90. // "bank_type": "OTHERS",
  91. // "attach": "",
  92. // "success_time": "2022-06-20T16:16:12+08:00",
  93. // "payer": {
  94. // "openid": "oRj5A44G6lgCVENzVMxZtoMfNeww"
  95. // },
  96. // "amount": {
  97. // "total": 1,
  98. // "payer_total": 1,
  99. // "currency": "CNY",
  100. // "payer_currency": "CNY"
  101. // }
  102. // },
  103. // "associated_data": "transaction",
  104. // "nonce": "qoJzoS9MCNgu"
  105. // }
  106. // }
  107. Log::write('pay-notify-origin-data:' . json_encode($originData));
  108. if ($originData['event_type'] == 'TRANSACTION.SUCCESS') {
  109. // 支付成功回调
  110. $data = $originData['resource']['ciphertext'] ?? [];
  111. if (isset($data['trade_state']) && $data['trade_state'] == 'SUCCESS') {
  112. // 交易成功
  113. $data['pay_fee'] = ($data['amount']['total'] / 100);
  114. $data['notify_time'] = date('Y-m-d H:i:s', strtotime((string)$data['success_time']));
  115. $data['buyer_info'] = $data['payer']['openid'] ?? ($data['payer']['sub_openid'] ?? '');
  116. $result = $callback($data, $originData);
  117. return $result;
  118. }
  119. return 'fail';
  120. } else {
  121. // 微信交易未成功,返回 false,让微信再次通知
  122. Log::error('notify-error:交易未成功:' . $originData['event_type']);
  123. return 'fail';
  124. }
  125. } catch (HttpResponseException $e) {
  126. $data = $e->getResponse()->getData();
  127. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  128. $this->logPayment('微信支付回调HttpResponseException', ['error' => $message], 'error');
  129. return 'fail';
  130. } catch (\Exception $e) {
  131. $this->logPayment('微信支付回调异常', ['error' => $e->getMessage()], 'error');
  132. return 'fail';
  133. }
  134. }
  135. /**
  136. * 退款
  137. *
  138. * @param array $order_data
  139. * @param array $config
  140. * @return array
  141. */
  142. public function refund($order_data, $config = [])
  143. {
  144. $config['notify_url'] = $config['notify_url'] ?? request()->domain() . '/addons/shopro/pay/notifyRefund/payment/wechat/platform/' . $this->platform;
  145. $order_data['notify_url'] = $config['notify_url'];
  146. $this->init('wechat', $config);
  147. $order_data['amount']['total'] = intval(bcmul((string)$order_data['amount']['total'], '100'));
  148. $order_data['amount']['refund'] = intval(bcmul((string)$order_data['amount']['refund'], '100'));
  149. $result = Pay::wechat()->refund($order_data);
  150. Log::write('pay-refund-origin-data:' . json_encode($result, JSON_UNESCAPED_UNICODE));
  151. // { 返回数据字段
  152. // "amount": {
  153. // "currency": "CNY",
  154. // "discount_refund": 0,
  155. // "from": [],
  156. // "payer_refund": 1,
  157. // "payer_total": 1,
  158. // "refund": 1,
  159. // "settlement_refund": 1,
  160. // "settlement_total": 1,
  161. // "total": 1
  162. // },
  163. // "channel": "ORIGINAL",
  164. // "create_time": "2022-06-20T19:06:36+08:00",
  165. // "funds_account": "AVAILABLE",
  166. // "out_refund_no": "R202207063668479227002100",
  167. // "out_trade_no": "P202205155977315528002100",
  168. // "promotion_detail": [],
  169. // "refund_id": "50301802252022062021833667769",
  170. // "status": "PROCESSING",
  171. // "transaction_id": "4200001521202206207964248014",
  172. // "user_received_account": "\u652f\u4ed8\u7528\u6237\u96f6\u94b1"
  173. // }
  174. return $result;
  175. }
  176. /**
  177. * 微信退款回调
  178. *
  179. * @param array $callback
  180. * @param array $config
  181. * @return array
  182. */
  183. public function notifyRefund($callback, $config = [])
  184. {
  185. $this->init('wechat', $config);
  186. try {
  187. $originData = Pay::wechat()->callback(); // 是的,验签就这么简单!
  188. Log::write('pay-notifyrefund-callback-data:' . json_encode($originData));
  189. // {
  190. // "id": "4a553265-1f28-53a3-9395-8d902b902462",
  191. // "create_time": "2022-06-21T11:25:33+08:00",
  192. // "resource_type": "encrypt-resource",
  193. // "event_type": "REFUND.SUCCESS",
  194. // "summary": "\u9000\u6b3e\u6210\u529f",
  195. // "resource": {
  196. // "original_type": "refund",
  197. // "algorithm": "AEAD_AES_256_GCM",
  198. // "ciphertext": {
  199. // "mchid": "1623831039",
  200. // "out_trade_no": "P202211233042122753002100",
  201. // "transaction_id": "4200001417202206214219765470",
  202. // "out_refund_no": "R202211252676008994002100",
  203. // "refund_id": "50300002272022062121864292533",
  204. // "refund_status": "SUCCESS",
  205. // "success_time": "2022-06-21T11:25:33+08:00",
  206. // "amount": {
  207. // "total": 1,
  208. // "refund": 1,
  209. // "payer_total": 1,
  210. // "payer_refund": 1
  211. // },
  212. // "user_received_account": "\u652f\u4ed8\u7528\u6237\u96f6\u94b1"
  213. // },
  214. // "associated_data": "refund",
  215. // "nonce": "8xfQknYyLVop"
  216. // }
  217. // }
  218. if ($originData['event_type'] == 'REFUND.SUCCESS') {
  219. // 支付成功回调
  220. $data = $originData['resource']['ciphertext'] ?? [];
  221. if (isset($data['refund_status']) && $data['refund_status'] == 'SUCCESS') {
  222. // 退款成功
  223. $result = $callback($data, $originData);
  224. return $result;
  225. }
  226. return 'fail';
  227. } else {
  228. // 微信交易未成功,返回 false,让微信再次通知
  229. Log::error('notify-error:退款未成功:' . $originData['event_type']);
  230. return 'fail';
  231. }
  232. } catch (HttpResponseException $e) {
  233. $data = $e->getResponse()->getData();
  234. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  235. $this->logPayment('微信退款回调HttpResponseException', ['error' => $message], 'error');
  236. return 'fail';
  237. } catch (\Exception $e) {
  238. $this->logPayment('微信退款回调异常', ['error' => $e->getMessage()], 'error');
  239. return 'fail';
  240. }
  241. }
  242. /**
  243. * 格式化支付参数
  244. *
  245. * @param [type] $params
  246. * @return void
  247. */
  248. protected function formatConfig($config, $data = [], $type = 'normal')
  249. {
  250. if ($config['mode'] == 2 && $type == 'sub_mch') {
  251. // 服务商模式,但需要子商户直连 ,重新定义 config(商家转账到零钱)
  252. $config = [
  253. 'mch_id' => $config['sub_mch_id'],
  254. 'mch_secret_key' => $config['sub_mch_secret_key'],
  255. 'mch_secret_cert' => $config['sub_mch_secret_cert'],
  256. 'mch_public_cert_path' => $config['sub_mch_public_cert_path'],
  257. ];
  258. $config['mode'] = 0; // 临时改为普通商户
  259. }
  260. if ($config['mode'] === 2) {
  261. // 首先将平台配置的 app_id 初始化到配置中
  262. $config['mp_app_id'] = $config['app_id']; // 服务商关联的公众号的 appid
  263. $config['sub_app_id'] = $data['app_id']; // 服务商特约子商户
  264. } else {
  265. $config['app_id'] = $data['app_id'];
  266. }
  267. switch ($this->platform) {
  268. case 'WechatMiniProgram':
  269. $config['_type'] = 'mini'; // 小程序提现,需要传 _type = mini 才能正确获取到 appid
  270. if ($config['mode'] === 2) {
  271. $config['sub_mini_app_id'] = $config['sub_app_id'];
  272. unset($config['sub_app_id']);
  273. } else {
  274. $config['mini_app_id'] = $config['app_id'];
  275. unset($config['app_id']);
  276. }
  277. break;
  278. case 'WechatOfficialAccount':
  279. $config['_type'] = 'mp'; // 小程序提现,需要传 _type = mp 才能正确获取到 appid
  280. if ($config['mode'] === 2) {
  281. $config['sub_mp_app_id'] = $config['sub_app_id'];
  282. unset($config['sub_app_id']);
  283. } else {
  284. $config['mp_app_id'] = $config['app_id'];
  285. unset($config['app_id']);
  286. }
  287. break;
  288. case 'App':
  289. case 'H5':
  290. default:
  291. break;
  292. }
  293. $config['notify_url'] = request()->domain() . '/api/pay/notify/payment/wechat/platform/' . $this->platform;
  294. // $config['mch_secret_cert'] = cdnurl($config['mch_secret_cert'], true);
  295. // $config['mch_public_cert_path'] = cdnurl($config['mch_public_cert_path'], true);
  296. // // $config['mch_secret_cert'] = ROOT_PATH . 'public/apiclient_cert.pem';
  297. // // $config['mch_public_cert_path'] = ROOT_PATH . 'public/apiclient_key.pem';
  298. $config['mch_secret_cert'] = ROOT_PATH . 'public' . $config['mch_secret_cert'];
  299. $config['mch_public_cert_path'] = ROOT_PATH . 'public' . $config['mch_public_cert_path'];
  300. // 可手动配置微信支付公钥证书
  301. $config['wechat_public_cert_id'] = $config['wechat_public_cert_id'] ?? '';
  302. $config['wechat_public_cert'] = $config['wechat_public_cert'] ?? '';
  303. if ($config['wechat_public_cert_id'] && $config['wechat_public_cert']) {
  304. $config['wechat_public_cert_path'] = [
  305. $config['wechat_public_cert_id'] => cdnurl($config['wechat_public_cert'], true)
  306. ];
  307. }
  308. unset($config['wechat_public_cert_id'], $config['wechat_public_cert']);
  309. return $config;
  310. }
  311. }