q827665774 2021-07-15 11:43 采纳率: 50%
浏览 85

PHP对接葫芦码支付/付费求解决

我博客程序emlog,已有插件,
需求:更改下支付接口
我的Q:827665774
葫芦码支付文档:huluwl.cn/doc.php

<?php
/**
 *
 * 文件说明: 前台支付处理
 *
 * Created by PhpStorm.
 * User: qs9515
 * Date: 2020/3/14
 * Time: 8:49
 *
 * $Id$
 * $LastChangedDate$
 * $LastChangedRevision$
 * $LastChangedBy$
 */
if(!defined('EMLOG_ROOT')) {exit('error!');}
if (!class_exists('pay_func', false)) {
    include dirname(__FILE__) . '/pay_func.php';
}
session_start();
$ac=pay_func::_get('ac');
$data['code']=500;
if($ac=='pay')
{
    //展示支付页面
    $paySetting=pay_func::readConfig();
    $bid=pay_func::_get('bid');
    $email=pay_func::_get('payEmail');
    $_SESSION['payUserId']=$email;
    include dirname(__FILE__) . '/static/pay_before.php';
}
elseif ($ac=='qr')
{
    //展示二维码
    $bid=pay_func::_post('bid');
    $userId=pay_func::_post('email');
    $userId=$userId!=''?$userId:'';
    $_SESSION['payUserId']=$_SESSION['payUserId']?$_SESSION['payUserId']:$userId;
    //查找用户是否已付费
    $db=Database::getInstance();
    if (pay_func::isPay($userId,$bid))
    {
        //用户已付费
        $data['msg']='您已经购买本文章,无需重复付费!';
        $data['refresh']=1;
        pay_func::json($data);
    }
    //校验用户是否已存在支付订单
    pay_func::updateOrderStatus($userId,$bid);
    //第二次校验
    if (pay_func::isPay($userId,$bid))
    {
        //用户已付费
        $data['msg']='您已经购买本文章,无需重复付费!';
        $data['refresh']=1;
        pay_func::json($data);
    }
    if ($bid)
    {
        //获取xorpay参数
        $paySetting=pay_func::readConfig();
        if (isset($paySetting['aid']) && $paySetting['aid']!='' && isset($paySetting['secret']) && $paySetting['secret']!='')
        {
            //生成二维码
            $apiUri='https://xorpay.com/api/pay/'.$paySetting['aid'];
            //获取博客信息用于生成二维码
            //查找博客信息
            $bsql="SELECT `title` FROM " . DB_PREFIX . "blog WHERE gid='".$bid."' limit 1";
            $bblog=$db->once_fetch_array($bsql);
            if (isset($bblog['title']) && $bblog['title']!='')
            {
                //获取支付金额
                $sql = "SELECT * FROM " . DB_PREFIX . "pay WHERE bid='".$bid."' limit 1";
                $row = $db->once_fetch_array($sql);
                if (isset($row['money']) && $row['money']>0)
                {
                    $params=array();
                    //商品名称
                    $params['name']='购买博客文章【'.$bblog['title'].'】';
                    //支付方式
                    $params['pay_type']='alipay';
                    //价格
                    $params['price']=$row['money'];
                    //平台订单号
                    $params['order_id']=md5(uniqid('P_',true));
                    //订单用户
                    $params['order_uid']=$userId;
                    //回调地址
                    $params['notify_url']=BLOG_URL.'/?plugin=pay&ac=callback&bid='.$bid.'&email='.$userId.'&k='.$params['order_id'];
                    //回调显示内容
                    $params['more']='您购买的内容仅限自己查看,请不要未经允许,私自传播。';
                    //将参数按name + pay_type + price + order_id + notify_url + app secret顺序拼接后MD5(纯 value 拼接,不要包含 + 号)
                    $params['sign']=md5($params['name'].$params['pay_type'].$params['price'].$params['order_id'].$params['notify_url'].$paySetting['secret']);
                    $res=pay_func::curlPost($apiUri,$params);
                    $resArr=json_decode($res,true);
                    $_SESSION['paySign']='';
                    if (isset($resArr['status']) && $resArr['status']=='ok')
                    {
                        include dirname(__FILE__) .'/phpqrcode/phpqrcode.php';
                        $data['code']=200;
                        $data['qrcode']=isset($resArr['info']['qr'])?$resArr['info']['qr']:'';
                        require dirname(__FILE__) . '/mobile/mobile.php';
                        $detect = new Mobile_Detect();
                        $imgInfo = '';
                        if (!$detect->isMobile())
                        {
                            ob_start();//开启缓冲区
                            QRcode::png($data['qrcode'], false, 'L', 10, 1);//生成二维码
                            $img = ob_get_contents();//获取缓冲区内容
                            ob_end_clean();//清除缓冲区内容
                            $imgInfo = 'data:png;base64,' . chunk_split(base64_encode($img));//转base64
                            $data['loadUri']=2;
                        }
                        else
                        {
                            $data['loadUri']=1;
                        }
                        $data['qqrcode']=$data['qrcode'];
                        $data['qrcode']=$imgInfo;
                        $data['bmoney']=$row['money'];
                        $data['btitle']=$bblog['title'];
                        $data['buri']=Url::log($bid);
                        $data['expireTime']=isset($resArr['info']['expires_in'])?$resArr['info']['expires_in']:7200;
                        $data['msg']='支付二维码获取成功!';
                        $_SESSION['paySign']=$params['sign'];
                        $_SESSION['xorPayId']=$resArr['aoid'];
                        //记录订单信息
                        $payTime=date('Y-m-d H:i:s');
                        $db->query("INSERT INTO " . DB_PREFIX . "pay_log (`bid`,`phone`,`pay_id`,`pay_time`,`user_info`,`money`,`xpay_id`,`ip`,`created`,`updated`,`status`) VALUES ('".$bid."','".$userId."','".$params['order_id']."','".$payTime."','".(isset($_SERVER['HTTP_USER_AGENT'])?base64_encode($_SERVER['HTTP_USER_AGENT']):'')."','".$params['price']."','".$resArr['aoid']."','".pay_func::getIp()."','".$payTime."','".$payTime."',2)");
                        pay_func::json($data);
                    }
                    else
                    {
                        $data['msg']='支付接口未能返回正确参数!';
                        pay_func::json($data);
                    }
                }
                else
                {
                    //支付金额错误
                    $data['msg']='文章支付金额未设置,或者支付金额设置错误!';
                    pay_func::json($data);
                }
            }
            else
            {
                //参数错误
                $data['msg']='参数错误,未能获取到付费的博客信息!';
                pay_func::json($data);
            }
        }
        else
        {
            //支付参数未设置
            $data['msg']='支付参数未设置,请联系管理员!';
            pay_func::json($data);
        }
    }
    else
    {
        $data['msg']='生成二维码失败,参数错误!';
        pay_func::json($data);
    }
}
elseif($ac=='callback')
{
    //回调地址
    $bid=pay_func::_get('bid');
    $userId=pay_func::_get('email');
    $pay_id=pay_func::_get('k');
    if ($bid && $userId)
    {
        $db=Database::getInstance();
        $row=$db->once_fetch_array("select * from `" . DB_PREFIX . "pay_log` where `bid`=$bid and `phone`='$userId' and `pay_id`='".$pay_id."'");
        if (isset($row['bid']) && $row['bid']!='')
        {
            //签名匹配
            $sign=pay_func::_post('sign');
            if (isset($_SESSION['paySign']) && $_SESSION['paySign']==$sign)
            {
                //更新订单状态
                $db->query("update `" . DB_PREFIX . "pay_log` set `status`=1 where `bid`=$bid and `phone`='$userId' and `pay_id`='".$pay_id."'");
                exit('订单【'.$row['pay_id'].'】支付成功!请关闭支付窗口!');
            }
            else
            {
                exit('支付失败,返回参数错误,请联系管理员!');
            }
        }
        else
        {
            exit('支付失败,返回参数错误,您可以刷新页面后重试!');
        }
    }
    else
    {
        header("HTTP/1.0 405 Method Not Allowed");
        exit();
    }
}
elseif($ac=='status')
{
    //监测订单付费情况
    $data['code']=500;
    $xorpayId=isset($_SESSION['xorPayId'])?$_SESSION['xorPayId']:'';
    if ($xorpayId)
    {
        $res=pay_func::curlGet('https://xorpay.com/api/query/'.$xorpayId);
        $resArr=json_decode($res,true);
        if (isset($resArr['status']))
        {
            if ($resArr['status']=='payed' || $resArr['status']=='success')
            {
                $db=Database::getInstance();
                $db->query("update `" . DB_PREFIX . "pay_log` set `status`=1 where `xpay_id`='".$xorpayId."'");
                $data['code']=200;
                $data['msg']='订单支付成功!';
            }
            elseif($resArr['status']=='not_exist')
            {
                $data['code']=300;
                $data['msg']='未监测到订单,请刷新页面重试!';
            }
            elseif($resArr['status']=='fee_error')
            {
                $data['code']=400;
                $data['msg']='订单扣费失败,请联系管理员!';
            }
            elseif($resArr['status']=='expire')
            {
                $data['code']=600;
                $data['msg']='订单已过期,请刷新页面!';
            }
        }
        else
        {
            $data['msg']='订单信息获取失败,请稍后再试,或者联系网站管理员!';
        }
        pay_func::json($data);
    }
    else
    {
        $data['code']=700;
        $data['msg']='订单信息获取失败,请稍后再试,或者联系网站管理员!';
        pay_func::json($data);
    }
}
else
{
    exit('非法访问!');
}
  • 写回答

1条回答 默认 最新

  • ityun.t 2021-07-20 11:47
    关注

    还在做吗?

    评论

报告相同问题?

问题事件

  • 修改了问题 7月15日
  • 创建了问题 7月15日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址