Files

105 lines
3.2 KiB
PHP

<?php
namespace app\index\controller;
use app\common\controller\Frontend;
use app\common\model\order\Order;
class Alipay extends Frontend
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
protected $layout = '';
/**
* 支付宝支付页面
* @return void
*/
public function pay()
{
$orderNo = $this->request->param('order_no', '');
if (empty($orderNo)) {
return $this->errorPage('订单号不能为空');
}
$order = Order::where('order_no', $orderNo)->find();
if (!$order) {
return $this->errorPage('订单不存在');
}
if ($order->status != \app\common\constant\order\Status::STATUS_UNPAID) {
return $this->errorPage('当前订单状态不支持付款');
}
try {
$alipayConfig = \app\common\model\config\System::getConfig('alipay');
if (!$alipayConfig || empty($alipayConfig['app_id'])) {
return $this->errorPage('支付宝支付未配置');
}
$notify_url = MODULE_URL . '/api/pay/notifyx/pay_type/alipay/platform/url/open_id//uniacid/' . UNIACID;
$userAgent = strtolower($this->request->server('HTTP_USER_AGENT', ''));
$isMobile = preg_match('/(mobile|android|iphone|ipad|ipod|windows phone)/i', $userAgent);
$platform = $isMobile ? 'url' : 'pc';
$pay = new \app\common\library\pay\PayService('alipay', $platform, $notify_url);
$domain = $this->request->domain();
$uniacid = UNIACID;
$order_data = [
'out_trade_no' => $order->order_no,
'total_amount' => $order->real_price,
'subject' => '商城订单支付',
'return_url' => $domain . '/index/alipay/return?order_no=' . $orderNo . '&i=' . $uniacid,
];
if ($isMobile) {
$order_data['quit_url'] = $domain . '/index.php?route=index&i=' . $uniacid;
}
$result = $pay->create($order_data);
$html = $result->getContent();
$pay_url = $this->request->root(true) . '/index/alipay/pay?order_no=' . $orderNo;
$this->view->assign('form', $html);
$this->view->assign('pay_url', $pay_url);
return $this->view->fetch();
} catch (\Exception $e) {
return $this->errorPage('支付创建失败:' . $e->getMessage());
}
}
/**
* 支付宝支付返回
* @return void
*/
public function return()
{
$orderNo = $this->request->param('order_no', '');
$uniacid = $this->request->param('i', UNIACID);
$domain = $this->request->domain();
if (!empty($orderNo)) {
$this->redirect($domain . '/index.php?route=index&i=' . $uniacid . '#/pages/order/detail/detail?order_no=' . $orderNo);
} else {
$this->redirect($domain . '/index.php?route=index&i=' . $uniacid);
}
}
/**
* 错误页面
* @param string $msg 错误信息
* @return mixed
*/
protected function errorPage($msg)
{
$this->view->assign('msg', $msg);
return $this->view->fetch('error_page');
}
}