1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/8/13
- * Time: 上午10:01
- */
- namespace app\main\service;
- use app\main\constants\PayConstants;
- use app\main\InternalInterface\SingletonService;
- use think\Config;
- use Yansongda\Pay\Gateways\Alipay;
- use Yansongda\Pay\Pay;
- class AliH5PayService extends BasePayService implements SingletonService
- {
- /**
- * @var Alipay
- */
- private $alipay = null;
- /**
- * @var AliH5PayService
- */
- protected static $self = NULL;
- /**
- * @return AliH5PayService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 初始化配置
- */
- public function initPay()
- {
- $config = [
- 'app_id' => $this->oWxPay->appid,
- 'notify_url' => Config::get('site.scheme') . '://' . $this->oWxPay->pay_host . '/api/wechat/alih5pay',
- 'return_url' => $this->getRedirectUrl()->data,
- 'ali_public_key' => $this->oWxPay->quartet_app_public_key,
- 'private_key' => $this->oWxPay->quartet_app_key,
- 'http' => [ // optional
- 'timeout' => 5.0,
- 'connect_timeout' => 5.0,
- ],
- ];
- $config = array_merge(PayConstants::getH5DefaultConfig(), $config);
- LogService::debug(json_encode($config, JSON_UNESCAPED_UNICODE));
- $this->alipay = Pay::alipay($config);
- return $this->alipay;
- }
- /**
- * 创建支付宝订单
- * @return \app\main\model\object\ReturnObject
- */
- public function createOrder()
- {
- $url = '';
- try {
- $this->initPay();
- $out_trade_no = $this->getOutTradeNo($this->oOrder->user_id)->data;
- $order = [
- 'out_trade_no' => $out_trade_no,
- 'total_amount' => $this->oOrder->money,
- 'subject' => $this->oOrder->type == 2 ? '充值VIP' : '充值书币',
- 'http_method' => 'GET',
- 'time_expire' => date('Y-m-d H:i:s', time() + PayConstants::getPayTimeoutSeconds()),
- ];
- $content = $this->alipay->wap($order)->getContent();
- //补充订单信息
- $this->oOrder->payid = '';
- preg_match('/<a href="(.*?)">/', $content, $match);
- $url = htmlspecialchars_decode($match[1]);
- $this->oOrder->pay_json = $url;
- $this->oOrder->out_trade_no = $out_trade_no;
- $this->oOrder->pdorderid = '';
- } catch (\Exception $e) {
- LogService::exception($e);
- }
- LogService::debug('alih5payurl:' . $url);
- return $this->setData(['redirect' => $url])->getReturn();
- }
- }
|