Files
evus.acvisa.com/app/home/controller/Payesta.php

252 lines
8.6 KiB
PHP

<?php
namespace app\home\controller;
use api\Httpcurl;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Log;
use think\facade\Request;
use think\facade\View;
use think\exception\HttpResponseException;
use think\Response;
class Payesta extends Base
{
use \app\home\controller\concern\SelectedPayment;
public function checkout()
{
return $this->selectedCheckout(ESTA_MODEL, PAY_ESTA_MARK, ESTA_COUNTRY, (string) config('app.get_order_detail_url'), 'payesta');
}
private $order_sn = '';
public function initialize()
{
parent::initialize();
$this->order_sn = Request::instance()->param('order_sn', '');
if (empty($this->order_sn)) {
throw new HttpResponseException(Response::create($this->error('缺少订单号,请重新填写', url('esta/index'))));
}
}
public function index()
{
if (isMobile()) {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return redirect(url('payesta/wxpay', ['order_sn' => $this->order_sn])->build());
}
return redirect(url('payesta/h5pay', ['order_sn' => $this->order_sn])->build());
}
return redirect(url('payesta/pcpay', ['order_sn' => $this->order_sn])->build());
}
/**
* 跳到微信支付
*/
public function wxpay()
{
$wechaturl = config('app.wechatpay');
$wechaturl .= '?order_sn=' . $this->order_sn . '&mark=' . PAY_ESTA_MARK . '&body=' . urlencode(ESTA_COUNTRY . '服务费');
header('location:' . $wechaturl); // 跳转到 user/profile
exit;
}
/**
* 跳转到pc支付
*/
public function pcpay()
{
$order_sn = $this->order_sn;
//判断支付是否已完成
$order_url = config('app.get_order_detail_url');
$response = Httpcurl::request($order_url, 'post', ['order_sn' => $order_sn, 'model' => ESTA_MODEL]);
if ($response[3]) {
return $this->error("异常错误,请重新提交", url('detail/index'));
}
$pay_result = json_decode($response[0], true);
Log::info(json_encode($pay_result));
if (empty($pay_result)) {
return $this->error("异常错误,请重新提交", url('detail/index'));
}
if (!$pay_result['code']) {
return $this->error($pay_result['msg'], url('detail/index'));
}
$pay_result = $pay_result['data'];
if ($pay_result['pay_status']) {
return redirect(url('home/payesta/succ', ['order_sn' => $order_sn])->build());
}
//微信pc
$cfg = config('app.wechatpcpay');
$res = Httpcurl::request($cfg, 'get', array('order_sn' => $order_sn, 'mark' => PAY_ESTA_MARK, 'body' => ESTA_COUNTRY . '服务费'));
$wxurl = "";
if (!$res[3]) {
$result = json_decode($res[0], true);
$wxurl = $result['url'];
}
Log::info(json_encode($res));
//支付宝pc
$cfg = config('app.alipcpay');
$res = Httpcurl::request($cfg, 'get', array('order_sn' => $order_sn, 'mark' => PAY_ESTA_MARK, 'body' => urlencode(ESTA_COUNTRY . '服务费')));
$aliurl = "";
if (!$res[3]) {
$result = json_decode($res[0], true);
$aliurl = $result['url'];
}
Log::info(json_encode($res));
View::assign('url', $wxurl);
View::assign('aliurl', $aliurl);
View::assign('order_sn', $order_sn);
View::assign('total_fee', number_format($pay_result['total_price'] / 100, 2));
return View::fetch();
}
/**
* 跳转到H5支付
*/
public function h5pay()
{
$wechaturl = config('app.wechath5pay');
$wechaturl .= '?order_sn=' . $this->order_sn . '&mark=' . PAY_ESTA_MARK . '&body=' . urlencode(ESTA_COUNTRY . '服务费');
header('location:' . $wechaturl); // 跳转到 user/profile
exit;
}
public function succ()
{
$order_sn = $this->order_sn;
View::assign('order_sn', $order_sn);
return View::fetch();
}
/**
* h5支付结果页面
*/
public function h5result()
{
if (Request::instance()->isAjax()) {
$order_sn = $this->order_sn;
if (!$order_sn) {
return json([
'status' => 0,
'msg' => '订单号不存在,请重新支付',
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
$order_url = config('app.get_order_detail_url');
$response = Httpcurl::request($order_url, 'post', ['order_sn' => $order_sn, 'model' => ESTA_MODEL]);
if ($response[3]) {
return json([
'status' => 0,
'msg' => '异常错误,请重新提交',
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
$pay_result = json_decode($response[0], true);
Log::info(json_encode($pay_result));
if (empty($pay_result)) {
return json([
'status' => 0,
'msg' => '异常错误,请重新提交',
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
if (!$pay_result['code']) {
return json([
'status' => 0,
'msg' => $pay_result['msg'],
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
$pay_result = $pay_result['data'];
if (in_array($pay_result['pay_status'] ?? null, [1, '1'], true)) {
return json([
'status' => 1,
'msg' => '支付成功',
'url' => url('payesta/succ', ['order_sn' => $order_sn])->build()
]);
}
return json([
'status' => 0,
'msg' => '您还未支付,请重新支付',
'url' => url('payesta/h5pay', ['order_sn' => $order_sn])->build()
]);
}
View::assign('order_sn', $this->order_sn);
View::assign('payment_label', 'ESTA');
View::assign('payment_check_url', url('home/payesta/h5result', ['order_sn' => $this->order_sn])->build());
View::assign('payment_success_url', url('home/payesta/succ', ['order_sn' => $this->order_sn])->build());
return View::fetch();
}
/**
* pc支付结果页面
*/
public function pcresult()
{
if (Request::instance()->isAjax()) {
$order_sn = $this->order_sn;
if (!$order_sn) {
return json([
'status' => 0,
'msg' => '订单号不存在,请重新支付',
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
$order_url = config('app.get_order_detail_url');
$response = Httpcurl::request($order_url, 'post', ['order_sn' => $order_sn, 'model' => ESTA_MODEL]);
if ($response[3]) {
return json([
'status' => 0,
'msg' => '异常错误,请重新提交',
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
$pay_result = json_decode($response[0], true);
Log::info(json_encode($pay_result));
if (empty($pay_result)) {
return json([
'status' => 0,
'msg' => '异常错误,请重新提交',
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
if (!$pay_result['code']) {
return json([
'status' => 0,
'msg' => $pay_result['msg'],
'url' => url('payesta/index', ['order_sn' => $order_sn])->build()
]);
}
$pay_result = $pay_result['data'];
if ($pay_result['pay_status']) {
return json([
'status' => 1,
'msg' => '支付成功',
'url' => url('payesta/succ', ['order_sn' => $order_sn])->build()
]);
}
return json([
'status' => 0,
'msg' => '您还未支付,请重新支付',
'url' => url('payesta/pcpay', ['order_sn' => $order_sn])->build()
]);
}
}
}