Notify.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service;
  13. use app\api\model\Payment as PaymentModel;
  14. use app\api\model\PaymentTrade as PaymentTradeModel;
  15. use app\api\model\PaymentTemplate as PaymentTemplateModel;
  16. use app\api\service\order\PaySuccess as OrderPaySuccesService;
  17. use app\api\service\recharge\PaySuccess as RechargePaySuccesService;
  18. use app\common\enum\OrderType as OrderTypeEnum;
  19. use app\common\enum\payment\Method as PaymentMethodEnum;
  20. use app\common\library\Log;
  21. use app\common\library\helper;
  22. use app\common\library\payment\gateway\Driver;
  23. use app\common\library\payment\Facade as PaymentFacade;
  24. use app\common\library\payment\gateway\driver\wechat\V3 as WechatPaymentV3;
  25. use cores\exception\BaseException;
  26. /**
  27. * 服务类:第三方支付异步通知
  28. * Class Notify
  29. * @package app\api\service
  30. */
  31. class Notify
  32. {
  33. /**
  34. * 支付成功异步通知 (微信支付V2)
  35. * @return string
  36. */
  37. public function wechatV2(): string
  38. {
  39. try {
  40. // 获取第三方交易记录
  41. $tradeInfo = $this->getTradeInfo(PaymentMethodEnum::WECHAT, 'v2');
  42. // 构建支付模块
  43. $Payment = $this->getPayment($tradeInfo);
  44. // 验证异步通知参数是否合法
  45. if (!$Payment->notify()) {
  46. throwError($Payment->getError() ?: '异步通知验证未通过');
  47. }
  48. // 订单支付成功事件
  49. $this->orderPaySucces($tradeInfo, $Payment->getNotifyParams());
  50. } catch (\Throwable $e) {
  51. // 记录错误日志
  52. Log::append('Notify-wechat', ['errMessage' => $e->getMessage()]);
  53. }
  54. return isset($Payment) ? $Payment->getNotifyResponse() : 'FAIL';
  55. }
  56. /**
  57. * 支付成功异步通知 (微信支付V3)
  58. * @return string
  59. * @throws BaseException
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function wechatV3(): string
  64. {
  65. try {
  66. // 通过微信支付v3平台证书序号 获取支付模板
  67. $wechatpaySerial = \request()->header('wechatpay-serial');
  68. $templateInfo = PaymentTemplateModel::findByWechatpaySerial($wechatpaySerial);
  69. empty($templateInfo) && throwError("未找到该平台证书序号:$wechatpaySerial");
  70. // 从支付模板中取出v3apikey 用于解密异步通知的密文
  71. $apiv3Key = $templateInfo['config']['wechat']['mchType'] === 'provider'
  72. ? $templateInfo['config']['wechat']['provider']['spApiKey']
  73. : $templateInfo['config']['wechat']['normal']['apiKey'];
  74. // 从支付模板中取出微信支付平台证书文件 用于验证API签名
  75. $fileName = $templateInfo['config']['wechat'][$templateInfo['config']['wechat']['mchType']]['platformCert'];
  76. $platformCertificateFilePath = PaymentTemplateModel::realPathCertFile(
  77. PaymentMethodEnum::WECHAT, $fileName, $templateInfo['store_id']
  78. );
  79. // 验证异步通知是否合法并获取第三方支付交易订单号
  80. $V3 = new WechatPaymentV3();
  81. $outTradeNo = $V3->notify($apiv3Key, $platformCertificateFilePath);
  82. empty($outTradeNo) && throwError('异步通知验证未通过');
  83. // 获取第三方交易记录
  84. $tradeInfo = PaymentTradeModel::detailByOutTradeNo($outTradeNo);
  85. // 订单支付成功事件
  86. $this->orderPaySucces($tradeInfo, $V3->getNotifyParams());
  87. } catch (\Throwable $e) {
  88. // 记录错误日志
  89. Log::append('Notify-wechat', ['errMessage' => $e->getMessage()]);
  90. }
  91. return '';
  92. }
  93. /**
  94. * 支付成功异步通知 (支付宝)
  95. * @return string
  96. */
  97. public function alipay(): string
  98. {
  99. try {
  100. // 获取第三方交易记录
  101. $tradeInfo = $this->getTradeInfo(PaymentMethodEnum::ALIPAY);
  102. // 构建支付模块
  103. $Payment = $this->getPayment($tradeInfo);
  104. // 验证异步通知参数是否合法
  105. if (!$Payment->notify()) {
  106. throwError($Payment->getError() ?: '异步通知验证未通过');
  107. }
  108. // 订单支付成功事件
  109. $this->orderPaySucces($tradeInfo, $Payment->getNotifyParams());
  110. } catch (\Throwable $e) {
  111. // 记录错误日志
  112. Log::append('Notify-alipay', ['errMessage' => $e->getMessage()]);
  113. }
  114. return isset($Payment) ? $Payment->getNotifyResponse() : 'FAIL';
  115. }
  116. /**
  117. * 订单支付成功事件
  118. * @param PaymentTradeModel $tradeInfo
  119. * @param array $paymentData 第三方支付异步回调的
  120. */
  121. private function orderPaySucces(PaymentTradeModel $tradeInfo, array $paymentData)
  122. {
  123. // 记录日志
  124. Log::append('Notify-orderPaySucces', [
  125. 'orderType' => OrderTypeEnum::data()[$tradeInfo['order_type']]['name'],
  126. 'tradeInfo' => $tradeInfo->toArray(),
  127. ]);
  128. try {
  129. // 订单支付成功业务处理 (商城订单)
  130. if ($tradeInfo['order_type'] == OrderTypeEnum::ORDER) {
  131. $service = new OrderPaySuccesService;
  132. $service->setOrderNo($tradeInfo['order_no'])
  133. ->setMethod($tradeInfo['pay_method'])
  134. ->setTradeId($tradeInfo['trade_id'])
  135. ->setPaymentData($paymentData)
  136. ->handle();
  137. }
  138. // 订单支付成功业务处理 (余额充值订单)
  139. if ($tradeInfo['order_type'] == OrderTypeEnum::RECHARGE) {
  140. $service = new RechargePaySuccesService;
  141. $service->setOrderNo($tradeInfo['order_no'])
  142. ->setMethod($tradeInfo['pay_method'])
  143. ->setTradeId($tradeInfo['trade_id'])
  144. ->setPaymentData($paymentData)
  145. ->handle();
  146. }
  147. Log::append('Notify-orderPaySucces', ['message' => '订单支付成功']);
  148. } catch (\Throwable $e) {
  149. // 记录错误日志
  150. Log::append('Notify-orderPaySucces', ['errMessage' => $e->getMessage()]);
  151. }
  152. }
  153. /**
  154. * 获取当前异步请求参数中的交易订单号
  155. * @param string $method 支付方式
  156. * @param string $wxapyVersion 微信支付版本号 v2或v3
  157. * @return string|null
  158. */
  159. private function getOutTradeNo(string $method, string $wxapyVersion = 'v2'): ?string
  160. {
  161. if ($method === PaymentMethodEnum::WECHAT) {
  162. if ($wxapyVersion === 'v2') {
  163. $xml = \file_get_contents('php://input');
  164. $data = helper::xmlToArray($xml);
  165. return $data['out_trade_no'];
  166. }
  167. if ($wxapyVersion === 'v3') {
  168. }
  169. }
  170. if ($method === PaymentMethodEnum::ALIPAY) {
  171. return \request()->post('out_trade_no');
  172. }
  173. return null;
  174. }
  175. /**
  176. * 获取第三方交易记录
  177. * @param string $method 支付方式
  178. * @param string $wxapyVersion 微信支付版本号 v2或v3
  179. * @return PaymentTradeModel|null
  180. * @throws BaseException
  181. */
  182. private function getTradeInfo(string $method, string $wxapyVersion = 'v2'): ?PaymentTradeModel
  183. {
  184. // 获取第三方交易记录
  185. $outTradeNo = $this->getOutTradeNo($method, $wxapyVersion);
  186. return PaymentTradeModel::detailByOutTradeNo($outTradeNo);
  187. }
  188. /**
  189. * 获取支付模块
  190. * @param PaymentTradeModel $tradeInfo 第三方交易记录
  191. * @return Driver|null
  192. * @throws BaseException
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. */
  197. private function getPayment(PaymentTradeModel $tradeInfo): ?Driver
  198. {
  199. // 获取支付方式的配置信息
  200. $options = $this->getPaymentConfig($tradeInfo['pay_method'], $tradeInfo['client'], $tradeInfo['store_id']);
  201. // 构建支付模块
  202. return PaymentFacade::store($tradeInfo['pay_method'])->setOptions($options, $tradeInfo['client']);
  203. }
  204. /**
  205. * 获取支付方式的配置信息
  206. * @param string $method 支付方式
  207. * @param string $client 下单客户端
  208. * @return mixed
  209. * @throws BaseException
  210. * @throws \think\db\exception\DataNotFoundException
  211. * @throws \think\db\exception\DbException
  212. * @throws \think\db\exception\ModelNotFoundException
  213. */
  214. private function getPaymentConfig(string $method, string $client, int $storeId = null)
  215. {
  216. $PaymentModel = new PaymentModel;
  217. $templateInfo = $PaymentModel->getPaymentInfo($method, $client, $storeId);
  218. return $templateInfo['template']['config'][$method];
  219. }
  220. }