AliH5PayService.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/8/13
  6. * Time: 上午10:01
  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\Alipay;
  13. use Yansongda\Pay\Pay;
  14. class AliH5PayService extends BasePayService implements SingletonService
  15. {
  16. /**
  17. * @var Alipay
  18. */
  19. private $alipay = null;
  20. /**
  21. * @var AliH5PayService
  22. */
  23. protected static $self = NULL;
  24. /**
  25. * @return AliH5PayService
  26. */
  27. public static function instance()
  28. {
  29. if (self::$self == NULL) {
  30. self::$self = new self();
  31. }
  32. return self::$self;
  33. }
  34. /**
  35. * 初始化配置
  36. */
  37. public function initPay()
  38. {
  39. $config = [
  40. 'app_id' => $this->oWxPay->appid,
  41. 'notify_url' => Config::get('site.scheme') . '://' . $this->oWxPay->pay_host . '/api/wechat/alih5pay',
  42. 'return_url' => $this->getRedirectUrl()->data,
  43. 'ali_public_key' => $this->oWxPay->quartet_app_public_key,
  44. 'private_key' => $this->oWxPay->quartet_app_key,
  45. 'http' => [ // optional
  46. 'timeout' => 5.0,
  47. 'connect_timeout' => 5.0,
  48. ],
  49. ];
  50. $config = array_merge(PayConstants::getH5DefaultConfig(), $config);
  51. LogService::debug(json_encode($config, JSON_UNESCAPED_UNICODE));
  52. $this->alipay = Pay::alipay($config);
  53. return $this->alipay;
  54. }
  55. /**
  56. * 创建支付宝订单
  57. * @return \app\main\model\object\ReturnObject
  58. */
  59. public function createOrder()
  60. {
  61. $url = '';
  62. try {
  63. $this->initPay();
  64. $out_trade_no = $this->getOutTradeNo($this->oOrder->user_id)->data;
  65. $order = [
  66. 'out_trade_no' => $out_trade_no,
  67. 'total_amount' => $this->oOrder->money,
  68. 'subject' => $this->oOrder->type == 2 ? '充值VIP' : '充值书币',
  69. 'http_method' => 'GET',
  70. 'time_expire' => date('Y-m-d H:i:s', time() + PayConstants::getPayTimeoutSeconds()),
  71. ];
  72. $content = $this->alipay->wap($order)->getContent();
  73. //补充订单信息
  74. $this->oOrder->payid = '';
  75. preg_match('/<a href="(.*?)">/', $content, $match);
  76. $url = htmlspecialchars_decode($match[1]);
  77. $this->oOrder->pay_json = $url;
  78. $this->oOrder->out_trade_no = $out_trade_no;
  79. $this->oOrder->pdorderid = '';
  80. } catch (\Exception $e) {
  81. LogService::exception($e);
  82. }
  83. LogService::debug('alih5payurl:' . $url);
  84. return $this->setData(['redirect' => $url])->getReturn();
  85. }
  86. }