自定义下单调用支付接口
2024-06-19 14:25:13
下单,首先写日志:
//下单 public function xiadan() { global $H; $softid = $H['softid']; //查询软件 $sql = "select `title`, `yuefu`, `price` from `de_1_form_soft` where id = '$softid'" ; $soft = DB()->query($sql)->getRowArray(); //用户选择的套餐 $tc = Post('tc'); //计算价格 $price = 0; if($tc == 30){ $price = $soft['yuefu']; //月付 }else{ $price = $soft['price']; //年付 } //buy-1_book-992-185-1-null $mark = array(); $mark['rname'] = "my"; //自定义固定my $mark['rid'] = "form_mabuy";//模块名_模型名 //下面三个是传给get_row参数用来计算价格的 $mark['fid'] = $H['softid']; //软件ID $mark['num'] = 1; //购买数量 $mark['sku'] = $_POST['tc']; //套餐 月/年 $mid = implode("-",$mark); //组合成支付标记 //写订单日志de_member_paylog $ip = IP(); $type = Post('typ'); $stime = time() - 3600;//1小时前 $title = $soft['title']; //软件名称 $money = 0; //余额 $status = 0;//状态 $time = time(); //获取一个小时前,未支付的订单,或者是当前用户未支付的订单 $paylogId = 0 ;//支付日志ID $P = DB()->query("select id from `de_member_paylog` where status = '0' and ( inputtime < $stime or `url` = '$ip' ) order by id ")->getRowArray(); if(!empty($P['id'])){ $paylogId = $P['id']; $sql = "update `de_member_paylog` set mid='$mid',title='$title', `url`='$ip', `value`='$price',`money`='$money', type='$type', inputtime='$time', status='$status' where id = '$paylogId' "; }else{ $sql = "insert into `de_member_paylog` (`site`,`mid`,`uid`,`touid`,`title`, `url`, `value`, `money`, `type`, `status` , `inputtime` ) values ( '1','$mid', '0', '0', '$title', '$ip', '$price', '$money', '$type', '$status' , '$time' ) "; } $db = DB(); $db->query($sql); if($paylogId == 0){ $paylogId = $db->insertID(); } //缓存下用户输入的留言信息 $maCache = array(); $maCache['ly'] = Post("ly"); $maCache['H'] = $H; \Phpcmf\Service::L('cache')->set_data("ma".$paylogId, $maCache , 3600); //缓存1小时 if($_POST['typ'] == "weixin"){ //微信跳转到扫码界面 $url = PAY_URL.'index.php?s=pay&id='.$paylogId; }else{ //支付宝会跳转到支付宝电脑版页面,我们生成一个二维码给他扫 $url = PAY_URL.'/-ma/alipay/'.$paylogId; } //跳转到扫码界面 $this->_json(1, "下单成功" , $url); exit; }
然后直接访问 $url = PAY_URL.'/-ma/alipay/'.$paylogId; 就能调用支付接口完成付款流程
其中mid字段需要指定模块名称,和模型名称 比如 form_mabuy 会直接调用 /dayrui/App/Form/ModelsMabuy.php
代码如下:
<?php namespace Phpcmf\Model\Form; global $H; // 模型类 class Mabuy extends \Phpcmf\Model { // 付款数据 // $id 记录id; $num 数量; $sku 自定义属性 public function get_row($fid, $num, $sku, $siteid = SITE_ID) { //查询软件 $sql = "select `title`, `yuefu`, `price` from `de_1_form_soft` where id = '$fid'" ; $A = DB()->query($sql)->getRowArray(); //计算价格 $price = 0; if($sku == 30){ $price = $A['yuefu']; //月付 }else{ $price = $A['price']; //年付 } $rt = [ 'price' => $price, 'title' => $A['title'], 'sell_uid' => 1, // 商家uid 'sell_username' => "de", // 商家账号 ]; return $rt; } // 付款成功后的回调 // $id 记录id; $paylog 支付表记录数组; $num 数量; $sku 自定义属性 public function success($fid, $paylog, $num, $sku) { list($rname, $rid, $fid, $num, $sku) = explode('-',$paylog['mid']); $paylogid = $paylog['id']; $maCache = \Phpcmf\Service::L('cache')->get_data("ma".$paylogid); $H = $maCache['H']; /** $paylog = array ( 'id' => '24', 'site' => '1', 'mid' => 'my-soft_buy-231018uyhgvlqfbg', 'uid' => '3', 'touid' => '0', 'title' => '软件内充值', 'url' => 'https://de.jjdede.com/index.php?s=member&app=pay&c=paylog&m=show&id=24', 'value' => '-1.00', 'money' => '2053.91', 'type' => 'alipay2', 'status' => 1, 'result' => '2023101822001404051416767502', 'paytime' => 1697602580, 'inputtime' => '1697602567', 'phone' => '', 'email' => '', ), */ //写入激活码数据库 $uid = 0; $title = $paylog['title']; $inputip = IP(); $status = 1; $displayorder = 0; $tableid = 0; $jiqi = $H['cpuid']; $softid = $H['softid']; $time = time(); $stoptime = $time + 86400 * $sku; $liuyan = $maCache['ly']; //创建一个激活码16位数的 $md5 = md5($softid.$jiqi.time().rand(1000,9999)); $ma = strtoupper(substr($md5, 8, 16)); //获取16位的大写MD5 //执行SQL $sql = "insert into de_1_form_ma ( `uid`, `title`, `inputip`, `inputtime`, `status`, `displayorder`, `tableid`, `jiqi`, `paylog`, `stoptime`, `softid`, `liuyan`, `ma`) values ( '$uid', '$title', '$inputip','$time', '$status', '$displayorder', '$tableid', '$jiqi', '$paylogid', '$stoptime', '$softid', '$liuyan', '$ma' ) "; DB()->query($sql); //微信通知 weixinNotice( "激活《".$title."》", $ma, $paylog["type"], $paylog['value']); } // 付款成功跳转URL // $id 记录id; $paylog 支付表记录数组 public function call_url($fid, $paylog) { Header("Location: /-ma/paysuccess/". aes($paylog['id'],"goui.pay.calback")); exit; } //构造函数 public function __construct(){ //父类的构造函数 parent::__construct(); global $H; //GET参数变量拆分 $argsArr = explode("/",$_GET["can"]); foreach($argsArr as $k=>$v){ $brr = explode("-",$v); if(count($brr) > 1){ $k = $brr[0]; array_shift( $brr); //删掉第一个值 $H[$k] = implode("-",$brr); }else if(count($brr) == 1){ $H["can"] = $brr[0]; } } //获取HTTP请求的东西 if(isset($_SERVER["HTTP_CACHE_TIME"]) ){ $CacheTime = Base62De($_SERVER["HTTP_CACHE_TIME"]) ; $arr = explode("|",$CacheTime); $mi = $arr[0]; $sj = $arr[1]; $data = aesDe( $mi,$sj); $brr = explode("|",$data); $H["softid"] = $brr[0]; $H["softver"] = $brr[1]; $H["cpuid"] = $brr[2]; }else{ $H["softid"] = $H["id"]; $H["softver"] = $H["v"]; } } }