php
//支付确认
public function pay(){
$oid=$this->request->param("oid/d");
if(empty($oid)){
$this->error("订单ID错误!");
}
$order=\app\admin\model\g\Order::where("id",$oid)->where("user_id",$this->auth->id)->find();
if(empty($order)){
$this->error("订单不存在!");
}
//支付
$params['amount'] = $order["amount"];
$params['type'] = "wechat";
$params['method'] = ""; //web、wap、app、scan、pos、mp,miniapp
$params['openid']=$this->request->param("openid");
if ( intval($params['amount']) < 1) {
$this->error("支付金额必须大于0");
}
if (!$params['type'] || !in_array($params['type'], ['alipay', 'wechat'])) {
$this->error("支付类型不能为空");
}
$isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
$isMobile = $this->request->isMobile();
//订单号
$params['orderid'] = $order["orderid"];
//订单标题
$params['title'] = $order["g_servicename"];
if ($isWechat && $isMobile) {
$params['method'] = "mp";
if(empty($params['openid'])){
$params['openid'] = \addons\epay\library\Service::getOpenid(["backurl"=>$this->request->root(true) ."/index/index/step5/oid/{$oid}"]);
}
}elseif (!$isWechat && $isMobile){
$params['method'] = "wap";
}else{
$params['method'] = "web";
}
//回调链接
$params['notifyurl'] = $this->request->root(true) . '/index/index/notifyx/paytype/' . $params['type']."/method/".$params['method'];
$params['returnurl'] = $this->request->root(true) . '/index/index/returnx/paytype/' . $params['type'] . '/orderid/' . $params['orderid'];
Log::write("拉起支付",'winfo');
Log::write($params,'winfo');
$response = \addons\epay\library\Service::submitOrder($params);
if($response){
$this->assign("zhifujson", $response);
}else{
$this->error("请求支付失败!");
}
$this->view->assign("response", $response);
$this->assign("method", $params['method']);
$this->view->assign("title", "在线支付");
return $this->view->fetch();
}
/**
* 支付成功
*/
public function notifyx()
{
Log::write('支付回调开始。','winfo');
$paytype = $this->request->param('paytype');
$method = $this->request->param('method');
$pay = \addons\epay\library\Service::checkNotify($paytype);
if (!$pay) {
echo '签名错误';
Log::write('签名错误','winfo');
return;
}
$data = $pay->verify();
try {
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
$out_trade_no = $data['out_trade_no'];
Log::write("订单号:{$out_trade_no}",'winfo');
$up["payamount"]=$payamount; //支付金额。
$up["paytype"]=$paytype; //支付类型
$up["paytime"]=time(); //支付时间
$up["method"]=$method; //支付方法
$up["ip"]=request()->ip(); //支付ip
$up["useragent"]=$_SERVER['HTTP_USER_AGENT']; //ua
$up["updatetime"]=time(); //更新时间
$up["status"]="paid"; //支付状态
$up['payid']=$data['transaction_id']; //支付ID
//你可以在此编写订单逻辑
$order=\app\admin\model\g\Order::where('orderid', $out_trade_no)->update($up);
Log::write("支付成功",'winfo');
Log::write($up,'winfo');
} 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 = \addons\epay\library\Service::checkReturn($paytype);
if (!$pay) {
$this->error('签名错误', '');
}
//你可以在这里通过out_trade_no去验证订单状态
//但是不可以在此编写订单逻辑!!!
if(db("g_order")->where("orderid",$out_trade_no)->value("status")=="ok"){
$this->success("支付成功", addon_url("index/user/index"));
}else{
$this->error("支付失败,请重试", addon_url("index/user/index"));
}
}
html
{eq name="method" value="web"}
{$zhifujson} {/* web下直接返回html */}
{else/}<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"/>
<link rel="stylesheet" href="/skin/css/style.css?{$site.version}">
<title>{$title}</title>
<script src="//res.wx.qq.com/open/js/jweixin-1.4.0.js" charset="utf-8" async="async"></script>
</head>
<body>
{include file="inc/_top" /}
<div class="main">
<div class="wrap">
</div>
</div>
{include file="inc/_foot" /}
{eq name="method" value="mp"}
<script>
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {$zhifujson},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
layer.open({content:'支付成功!。',skin: 'msg',time:3});
setTimeout(function(){
window.location.href="/index/user/index.html";
},3000)
}else if(res.err_msg == "get_brand_wcpay_request:fail"){
layer.open({content:'支付遇到错误。'+res.err_desc,skin: 'msg',time:3});
setTimeout(function(){
window.location.href="/index/user/index.html";
},3000)
}else{
layer.open({content:'您放弃支付。',skin: 'msg',time:3});
setTimeout(function(){
window.location.href="/index/user/index.html";
},3000)
}
}
);
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
</script>
{/eq}
</body>
</html>
{/eq}
addons/epay/library/service.php
/**
* 获取微信Openid
*
* @return mixed|string
*/
public static function getOpenid($params=[])
{
$config = self::getConfig('wechat');
$openid = '';
$auth = Auth::instance();
if ($auth->isLogin()) {
$third = get_addon_info('third');
if ($third && $third['state']) {
$thirdInfo = Third::where('user_id', $auth->id)->where('platform', 'wechat')->where('apptype', 'mp')->find();
$openid = $thirdInfo ? $thirdInfo['openid'] : '';
}
}
if (!$openid) {
$openid = Session::get("openid");
//如果未传openid,则去读取openid
if (!$openid) {
$wechat = new Wechat($config['app_id'], $config['app_secret']);
if(!isset($params["backurl"])){
$openid = $wechat->getOpenid();
}else{
$openid = $wechat->getOpenid($params);
}
}
}
return $openid;
}
addons/epay/library/Wechat.php
/**
* 获取微信授权链接
*
* @return string
*/
public function getAuthorizeUrl($url="")
{
$redirect_uri = $url ? $url : addon_url('epay/api/wechat', [], true, true);
$redirect_uri = urlencode($redirect_uri);
$state = \fast\Random::alnum();
Session::set('state', $state);
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope={$this->scope}&state={$state}#wechat_redirect";
}
/**
* 获取微信openid
*
* @return mixed|string
*/
public function getOpenid($params=[])
{
$openid = Session::get('openid');
if (!$openid) {
if (!isset($_GET['code'])) {
if(!isset($params["backurl"])){
$url = $this->getAuthorizeUrl();
}else{
$url = $this->getAuthorizeUrl($params["backurl"]);
}
Header("Location: $url");
exit();
} else {
$state = Session::get('state');
if ($state == $_GET['state']) {
$code = $_GET['code'];
$token = $this->getAccessToken($code);
if (!isset($token['openid']) && isset($token['errmsg'])) {
exception($token['errmsg']);
}
$openid = isset($token['openid']) ? $token['openid'] : '';
if ($openid) {
Session::set("openid", $openid);
}
}
}
}
return $openid;
}
发表评论 取消回复