WechatH5Payservice.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/8/13
  6. * Time: 上午11:48
  7. */
  8. namespace app\main\service;
  9. use app\main\constants\PayConstants;
  10. use app\main\InternalInterface\SingletonService;
  11. use think\Config;
  12. use Yansongda\Pay\Gateways\Wechat;
  13. use Yansongda\Pay\Pay;
  14. class WechatH5Payservice extends BasePayService implements SingletonService
  15. {
  16. /**
  17. * @var Wechat
  18. */
  19. private $weChatPay = null;
  20. /**
  21. * @var WechatH5Payservice
  22. */
  23. protected static $self = NULL;
  24. /**
  25. * @return WechatH5Payservice
  26. */
  27. public static function instance()
  28. {
  29. if (self::$self == NULL) {
  30. self::$self = new self();
  31. }
  32. return self::$self;
  33. }
  34. public function initPay()
  35. {
  36. $config = [
  37. 'app_id' => $this->oWxPay->appid,
  38. 'mch_id' => $this->oWxPay->mcid,
  39. 'key' => $this->oWxPay->apikey,
  40. 'notify_url' => Config::get('site.scheme') . '://' . $this->oWxPay->pay_host . '/api/wechat/wxh5pay',
  41. ];
  42. $config = array_merge(PayConstants::getH5DefaultConfig(), $config);
  43. $this->weChatPay = Pay::wechat($config);
  44. return $this->weChatPay;
  45. }
  46. /**
  47. * 调用第三方服务创建订单
  48. * @return \app\main\model\object\ReturnObject
  49. */
  50. public function createOrder()
  51. {
  52. try {
  53. $this->initPay();
  54. $out_trade_no = $this->getOutTradeNo($this->oOrder->user_id)->data;
  55. $order = [
  56. 'out_trade_no' => $out_trade_no,
  57. 'total_fee' => $this->oOrder->money * 100,
  58. 'body' => $this->oOrder->type == 2 ? '充值VIP' : '充值书币',
  59. 'time_start' => date('YmdHis'),
  60. 'time_expire' => date('YmdHis', time() + PayConstants::getPayTimeoutSeconds()),
  61. ];
  62. $url = $this->weChatPay->wap($order)->getTargetUrl();
  63. $this->oOrder->payid = $this->getPrePayId($url)->data;
  64. $this->oOrder->out_trade_no = $out_trade_no;
  65. $this->oOrder->pdorderid = '';
  66. $referral = parse_url($_SERVER['HTTP_REFERER']??'');
  67. $redirect = $this->getRedirectUrl()->data;
  68. if (empty($referral['query'])) {
  69. $url .= '&redirect_url=' . urlencode($redirect);
  70. } else {
  71. $url .= '?redirect_url=' . urlencode($redirect);
  72. }
  73. $this->oOrder->pay_json = $url;
  74. LogService::debug("wechath5payurl:" . $url);
  75. } catch (\Exception $e) {
  76. return $this->getExceptionReturn($e);
  77. }
  78. return $this->setData(['redirect' => $url])->getReturn();
  79. }
  80. /**
  81. * @param $target
  82. * @return \app\main\model\object\ReturnObject
  83. */
  84. private function getPrePayId($target)
  85. {
  86. $url = parse_url($target);
  87. $params = \GuzzleHttp\Psr7\parse_query($url['query']);
  88. return $this->setData($params['prepay_id'] ?? '')->getReturn();
  89. }
  90. }