WechatPaymentService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 WechatPaymentService 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 = '',string $notify_url = '',array $parm = []): 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' => $notify_url,
  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. return $data['trade_type'] =='JSAPI' ? $this->payment->jsapiParams($info['prepay_id']) : $this->payment->appParams($info['prepay_id']);
  53. // 返回支付参数
  54. }
  55. throw new Exception($info['err_code_des'] ?? '获取预支付码失败!');
  56. } catch (Exception $exception) {
  57. throw $exception;
  58. } catch (\Exception $exception) {
  59. throw new Exception($exception->getMessage(), $exception->getCode());
  60. }
  61. }
  62. /**
  63. * 查询微信支付订单
  64. * @param string $orderNo 订单单号
  65. * @return array
  66. * @throws \WeChat\Exceptions\InvalidResponseException
  67. * @throws \WeChat\Exceptions\LocalCacheException
  68. */
  69. public function query(string $orderNo): array
  70. {
  71. $result = $this->payment->query(['out_trade_no' => $orderNo]);
  72. if (isset($result['return_code']) && isset($result['result_code']) && isset($result['attach'])) {
  73. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
  74. $this->updatePaymentAction($result['out_trade_no'], $result['cash_fee'] / 100, $result['transaction_id']);
  75. }
  76. }
  77. return $result;
  78. }
  79. /**
  80. * 支付结果处理
  81. * @return string
  82. * @throws \WeChat\Exceptions\InvalidResponseException
  83. */
  84. public function opvip(): string
  85. {
  86. $notify = $this->payment->getNotify();
  87. if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
  88. if ($this->updateOpvipAction($notify)) {
  89. return $this->payment->getNotifySuccessReply();
  90. } else {
  91. return 'error';
  92. }
  93. } else {
  94. return $this->payment->getNotifySuccessReply();
  95. }
  96. }
  97. /**
  98. * 支付结果处理
  99. * @return string
  100. * @throws \WeChat\Exceptions\InvalidResponseException
  101. */
  102. public function payorder(): string
  103. {
  104. $notify = $this->payment->getNotify();
  105. if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
  106. if ($this->updatePayorderAction($notify)) {
  107. return $this->payment->getNotifySuccessReply();
  108. } else {
  109. return 'error';
  110. }
  111. } else {
  112. return $this->payment->getNotifySuccessReply();
  113. }
  114. }
  115. /**
  116. * 微信支付服务初始化
  117. * @return WechatPaymentService
  118. */
  119. protected function initialize(): WechatPaymentService
  120. {
  121. $this->payment = Order::instance([
  122. 'appid' => $this->params['wechat_appid'],
  123. 'mch_id' => $this->params['wechat_mch_id'],
  124. 'mch_key' => $this->params['wechat_mch_key'],
  125. 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat',
  126. ]);
  127. return $this;
  128. }
  129. }