WechatPaymentService2.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\data\service\payment;
  3. use app\data\service\PaymentService;
  4. use think\admin\Exception;
  5. use WePay\Order;
  6. /**
  7. * 微信官方公众号支持
  8. * Class WechatPaymentService
  9. * @package app\data\service\payment
  10. */
  11. class WechatPaymentService2 extends PaymentService
  12. {
  13. /**
  14. * 微信对象对象
  15. * @var Order
  16. */
  17. protected $payment;
  18. /**
  19. * 创建订单支付参数
  20. * @param string $openid 用户OPENID
  21. * @param string $orderNo 交易订单单号
  22. * @param string $payAmount 交易订单金额(元)
  23. * @param string $payTitle 交易订单名称
  24. * @param string $payRemark 订单订单描述
  25. * @param string $payReturn 完成回跳地址
  26. * @param string $payImage 支付凭证图片
  27. * @return array
  28. * @throws Exception
  29. */
  30. public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payRemark, string $payReturn = '', string $payImage = ''): array
  31. {
  32. try {
  33. if (isset(static::TYPES[$this->type])) {
  34. $tradeType = static::TYPES[$this->type]['type'];
  35. } else {
  36. throw new Exception(sprintf('支付类型[%s]未配置定义!', $this->type));
  37. }
  38. $body = empty($payRemark) ? $payTitle : ($payTitle . '-' . $payRemark);
  39. $data = [
  40. 'body' => $body,
  41. 'openid' => $openid,
  42. 'attach' => $this->code,
  43. 'out_trade_no' => $orderNo,
  44. 'trade_type' => $tradeType ?: '',
  45. 'total_fee' => $payAmount * 100,
  46. 'notify_url' => sysuri("@data/api.notify/wxpay/scene/order/param/{$this->code}", [], false, true),
  47. 'spbill_create_ip' => $this->app->request->ip(),
  48. ];
  49. if (empty($data['openid'])) unset($data['openid']);
  50. $info = $this->payment->create($data);
  51. if ($info['return_code'] === 'SUCCESS' && $info['result_code'] === 'SUCCESS') {
  52. // 创建支付记录
  53. $this->createPaymentAction($orderNo, $payTitle, $payAmount);
  54. // 返回支付参数
  55. return $this->payment->jsapiParams($info['prepay_id']);
  56. }
  57. throw new Exception($info['err_code_des'] ?? '获取预支付码失败!');
  58. } catch (Exception $exception) {
  59. throw $exception;
  60. } catch (\Exception $exception) {
  61. throw new Exception($exception->getMessage(), $exception->getCode());
  62. }
  63. }
  64. /**
  65. * 查询微信支付订单
  66. * @param string $orderNo 订单单号
  67. * @return array
  68. * @throws \WeChat\Exceptions\InvalidResponseException
  69. * @throws \WeChat\Exceptions\LocalCacheException
  70. */
  71. public function query(string $orderNo): array
  72. {
  73. $result = $this->payment->query(['out_trade_no' => $orderNo]);
  74. if (isset($result['return_code']) && isset($result['result_code']) && isset($result['attach'])) {
  75. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  76. $this->updatePaymentAction($result['out_trade_no'], $result['cash_fee'] / 100, $result['transaction_id']);
  77. }
  78. }
  79. return $result;
  80. }
  81. /**
  82. * 支付结果处理
  83. * @return string
  84. * @throws \WeChat\Exceptions\InvalidResponseException
  85. */
  86. public function notify(): string
  87. {
  88. $notify = $this->payment->getNotify();
  89. if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
  90. if ($this->updatePaymentAction($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100)) {
  91. return $this->payment->getNotifySuccessReply();
  92. } else {
  93. return 'error';
  94. }
  95. } else {
  96. return $this->payment->getNotifySuccessReply();
  97. }
  98. }
  99. /**
  100. * 微信支付服务初始化
  101. * @return WechatPaymentService
  102. */
  103. protected function initialize(): WechatPaymentService
  104. {
  105. $this->payment = Order::instance([
  106. 'appid' => $this->params['wechat_appid'],
  107. 'mch_id' => $this->params['wechat_mch_id'],
  108. 'mch_key' => $this->params['wechat_mch_key'],
  109. 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat',
  110. ]);
  111. return $this;
  112. }
  113. }