<?php
namespace app\api\controller;
use app\common\controller\Api;
use addons\epay\library\Service;
use fast\Random;
use think\addons\Controller;
use Exception;
/**
* 支付
*
* Class Index
* @package addons\epay\controller
*/
class Pay extends Controller
{
protected $noNeedLogin = "*";
protected $noNeedRight = "*";
public function _initialize()
{
parent::_initialize();
$this->OrderModel = new \app\admin\model\nb\Order;
$this->site=config("site");
}
/**
* 开始支付
*/
public function pay()
{
$id=$this->request->param('id/d');
$method = $this->request->param('method',"web");
$type= $this->request->param('paytype');
$returnurl= $this->request->param('returnurl',"");
if(empty($id)){
$this->error("参数错误!");
}
$order=$this->OrderModel->get($id);
$amount = $order["money"];
if (!$amount || $amount < 0) {
$this->error("支付金额必须大于0");
}
if (!$type || !in_array($type, ['alipay', 'wechat'])) {
$this->error("支付类型不能为空");
}
$isMobile = $this->request->isMobile();
$isWechat = strpos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
//web、wap、app、scan、pos、mp,miniapp,此判断好入库
if($isMobile && !$isWechat){
$method = "wap";
}elseif($isWechat){
$method = "mp";
}
//订单号
$out_trade_no = $order["orderno"];
//订单标题
$title = $order["title"];
//回调链接
$notifyurl = $this->request->root(true) . '/api/pay/notifyx/paytype/' . $type;
$returnurl = empty($returnurl) ? $this->request->root(true) . '/api/pay/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no : $returnurl;
db("nb_order")->where("id",$id)->update(["paytype"=>$type,"method"=>$method]);
//使用微信支付 且 在手机端 且 不是在微信浏览器里 那么 调用第三方支付
$isMobile = $this->request->isMobile();
$isWechat = strpos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
if($type=="wechat" && $isMobile && !$isWechat){
$h5payurl="https://168.qudoi.cn/pay/WeChatH5Go.php?out_trade_no={$out_trade_no}&appid={$this->site["appid"]}&sub_notify_url={$notifyurl}&total=".ceil($amount*100)."&title={$title}&redirect_url=".str_replace('#','%23',$returnurl);
$this->redirect($h5payurl);
//$this->success("h5支付",["url"=>$h5payurl,'method'=>"h5"]);
}
//提交的时候传method为web可以自动判断是mp还是扫码还是h5,且自动获取openid
$response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $isWechat ? "web" : $method ,"");
return $response;
}
/**
* 支付成功
*/
public function notifyx()
{
//trace($this->request->param(),"zhifuhuidiao");
//如果是第三方h5的回调
$h5data=$this->request->param();
if(isset($h5data["appkey"]) && $h5data["appkey"]==config("site.appkey")){
$up["transactionid"]=$h5data["trade_no"];
$up["paytime"]=time();
$up["paystatus"]="20";
db("nb_order")->where("orderno",$h5data["out_trade_no"])->update($up);
exit("SUCCESS");
}
$paytype = $this->request->param('paytype');
$pay = Service::checkNotify($paytype);
if (!$pay) {
echo '签名错误';
return;
}
$data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
try {
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
$out_trade_no = $data['out_trade_no'];
$up["transactionid"]= $paytype == 'alipay' ? $data['trade_no'] : $data["transaction_id"];
$up["paytime"]=time();
$up["paystatus"]="20";
db("nb_order")->where("orderno",$out_trade_no)->update($up);
//你可以在此编写订单逻辑
} catch (Exception $e) {
}
//下面这句必须要执行,且在此之前不能有任何输出
return $pay->success()->send();
}
/**
* 支付返回
*/
public function returnx()
{
$paytype = $this->request->param('paytype');
$out_trade_no = $this->request->param('out_trade_no');
$pay = Service::checkReturn($paytype);
if (!$pay) {
$this->error('签名错误', '');
}
//你可以在这里通过out_trade_no去验证订单状态
//但是不可以在此编写订单逻辑!!!
$this->redirect("/h5/pages/progress/progress?&orderno=".$out_trade_no);
//$this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
}
}
快捷键:pay.php
源:PHP
sublime_text格式
<snippet>
<content><![CDATA[
<?php
namespace app\api\controller;
use app\common\controller\Api;
use addons\epay\library\Service;
use fast\Random;
use think\addons\Controller;
use Exception;
/**
* 支付
*
* Class Index
* @package addons\epay\controller
*/
class Pay extends Controller
{
protected \$noNeedLogin = "*";
protected \$noNeedRight = "*";
public function _initialize()
{
parent::_initialize();
\$this->OrderModel = new \app\admin\model\nb\Order;
\$this->site=config("site");
}
/**
* 开始支付
*/
public function pay()
{
\$id=\$this->request->param('id/d');
\$method = \$this->request->param('method',"web");
\$type= \$this->request->param('paytype');
\$returnurl= \$this->request->param('returnurl',"");
if(empty(\$id)){
\$this->error("参数错误!");
}
\$order=\$this->OrderModel->get(\$id);
\$amount = \$order["money"];
if (!\$amount || \$amount < 0) {
\$this->error("支付金额必须大于0");
}
if (!\$type || !in_array(\$type, ['alipay', 'wechat'])) {
\$this->error("支付类型不能为空");
}
\$isMobile = \$this->request->isMobile();
\$isWechat = strpos(\$this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
//web、wap、app、scan、pos、mp,miniapp,此判断好入库
if(\$isMobile && !\$isWechat){
\$method = "wap";
}elseif(\$isWechat){
\$method = "mp";
}
//订单号
\$out_trade_no = \$order["orderno"];
//订单标题
\$title = \$order["title"];
//回调链接
\$notifyurl = \$this->request->root(true) . '/api/pay/notifyx/paytype/' . \$type;
\$returnurl = empty(\$returnurl) ? \$this->request->root(true) . '/api/pay/returnx/paytype/' . \$type . '/out_trade_no/' . \$out_trade_no : \$returnurl;
db("nb_order")->where("id",\$id)->update(["paytype"=>\$type,"method"=>\$method]);
//使用微信支付 且 在手机端 且 不是在微信浏览器里 那么 调用第三方支付
\$isMobile = \$this->request->isMobile();
\$isWechat = strpos(\$this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
if(\$type=="wechat" && \$isMobile && !\$isWechat){
\$h5payurl="https://168.qudoi.cn/pay/WeChatH5Go.php?out_trade_no={\$out_trade_no}&appid={\$this->site["appid"]}&sub_notify_url={\$notifyurl}&total=".ceil(\$amount*100)."&title={\$title}&redirect_url=".str_replace('#','%23',\$returnurl);
\$this->redirect(\$h5payurl);
//\$this->success("h5支付",["url"=>\$h5payurl,'method'=>"h5"]);
}
//提交的时候传method为web可以自动判断是mp还是扫码还是h5,且自动获取openid
\$response = Service::submitOrder(\$amount, \$out_trade_no, \$type, \$title, \$notifyurl, \$returnurl, \$isWechat ? "web" : \$method ,"");
return \$response;
}
/**
* 支付成功
*/
public function notifyx()
{
//trace(\$this->request->param(),"zhifuhuidiao");
//如果是第三方h5的回调
\$h5data=\$this->request->param();
if(isset(\$h5data["appkey"]) && \$h5data["appkey"]==config("site.appkey")){
\$up["transactionid"]=\$h5data["trade_no"];
\$up["paytime"]=time();
\$up["paystatus"]="20";
db("nb_order")->where("orderno",\$h5data["out_trade_no"])->update(\$up);
exit("SUCCESS");
}
\$paytype = \$this->request->param('paytype');
\$pay = Service::checkNotify(\$paytype);
if (!\$pay) {
echo '签名错误';
return;
}
\$data = Service::isVersionV3() ? \$pay->callback() : \$pay->verify();
try {
\$payamount = \$paytype == 'alipay' ? \$data['total_amount'] : \$data['total_fee'] / 100;
\$out_trade_no = \$data['out_trade_no'];
\$up["transactionid"]= \$paytype == 'alipay' ? \$data['trade_no'] : \$data["transaction_id"];
\$up["paytime"]=time();
\$up["paystatus"]="20";
db("nb_order")->where("orderno",\$out_trade_no)->update(\$up);
//你可以在此编写订单逻辑
} catch (Exception \$e) {
}
//下面这句必须要执行,且在此之前不能有任何输出
return \$pay->success()->send();
}
/**
* 支付返回
*/
public function returnx()
{
\$paytype = \$this->request->param('paytype');
\$out_trade_no = \$this->request->param('out_trade_no');
\$pay = Service::checkReturn(\$paytype);
if (!\$pay) {
\$this->error('签名错误', '');
}
//你可以在这里通过out_trade_no去验证订单状态
//但是不可以在此编写订单逻辑!!!
\$this->redirect("/h5/pages/progress/progress?&orderno=".\$out_trade_no);
//\$this->success("请返回网站查看支付结果", addon_url("epay/index/index"));
}
}
]]></content>
<tabTrigger>pay.php(fastadmin后端支付示例)</tabTrigger>
<scope></scope>
</snippet>
发表评论 取消回复