支付宝充值
//下载demo
//加载Submit;
//加载Notify;
//充值 public function recharge(){ $this->before(); $adminuid = session('adminuid'); $adminname = session('adminname'); if($_POST) { //判断是否为有效金额 if(!is_numeric($_POST['WIDtotal_fee']) && empty($_POST['WIDtotal_fee']) && $_POST['WIDtotal_fee']<0.01) { $this->error('充值金额输入错误,请重新输入正确金额!',U('Admin/recharge'),5);exit; } $config['alipay_config'] = C('alipay_config'); $config['alipay'] = C('alipay'); $payment_type = "1"; //支付类型 //必填,不能修改 $notify_url = $config['alipay']['notify_url']; //服务器异步通知页面路径 $return_url = $config['alipay']['return_url']; //页面跳转同步通知页面路径 $seller_email = $config['alipay']['seller_email'];//卖家支付宝帐户必填 $out_trade_no = $_POST['WIDout_trade_no'];//商户订单号 通过支付页面的表单进行传递,注意要唯一! $subject = $_POST['WIDsubject']; //订单名称 //必填 通过支付页面的表单进行传递 $total_fee = $_POST['WIDtotal_fee']; //付款金额 //必填 通过支付页面的表单进行传递 $body = $_POST['WIDbody']; //订单描述 通过支付页面的表单进行传递 $show_url = ''; //商品展示地址 通过支付页面的表单进行传递 $_POST['ordshow_url']; $anti_phishing_key = "";//防钓鱼时间戳 //若要使用请调用类文件submit中的query_timestamp函数 $exter_invoke_ip = get_client_ip(); //客户端的IP地址 /************************************************************/ //构造要请求的参数数组,无需改动 $parameter = array( "service" => "create_direct_pay_by_user", "partner" => trim($config['alipay_config']['partner']), "payment_type" => $payment_type, "notify_url" => $notify_url, "return_url" => $return_url, "seller_email" => $seller_email, "out_trade_no" => $out_trade_no, "subject" => $subject, "total_fee" => $total_fee, "body" => $body, "show_url" => $show_url, "anti_phishing_key" => $anti_phishing_key, "exter_invoke_ip" => $exter_invoke_ip, "_input_charset" => trim(strtolower($config['alipay_config']['input_charset'])) ); $orderlist = M('orderlist'); $add['uid'] = $adminuid; $add['username'] = $adminname; $add['ordid'] = $_POST['WIDout_trade_no']; $add['ordtime'] = date("Y-m-d H:i:s", time()); $add['ordtitle'] = $_POST['WIDbody']; $add['ordfee'] = $_POST['WIDtotal_fee']; $add['trade_status'] = 'TRADE_FALSE'; $rs = $orderlist->add($add); if(!$rs){ $this->error('支付失败,请重新支付',U('Admin/recharge'),5);exit; } //建立请求 $alipaySubmit = new Submit($config['alipay_config']); $html_text = $alipaySubmit->buildRequestForm($parameter,"post", "确认"); echo $html_text; } $data['adminname'] = $adminname; $data['admintype'] = session('admintype'); $this->assign('data',$data); $this->display('recharge'); }
public function returnurl() { if($_GET) { $adminuid = session('adminuid'); $config['alipay_config'] = C('alipay_config'); $config['alipay'] = C('alipay'); $alipayNotify = new Notify($config['alipay_config']);//计算得出通知验证结果 $verify_result = $alipayNotify->verifyReturn(); if($verify_result) { //验证成功 //获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表 $out_trade_no = $_GET['out_trade_no']; //商户订单号 $trade_no = $_GET['trade_no']; //支付宝交易号 $trade_status = $_GET['trade_status']; //交易状态 $total_fee = $_GET['total_fee']; //交易金额 $notify_id = $_GET['notify_id']; //通知校验ID。 $notify_time = $_GET['notify_time']; //通知的发送时间。 $buyer_email = $_GET['buyer_email']; //买家支付宝帐号; $parameter = array( "out_trade_no" => $out_trade_no, //商户订单编号; "trade_no" => $trade_no, //支付宝交易号; "total_fee" => $total_fee, //交易金额; "trade_status" => $trade_status, //交易状态 "notify_id" => $notify_id, //通知校验ID。 "notify_time" => $notify_time, //通知的发送时间。 "buyer_email" => $buyer_email, //买家支付宝帐号 ); if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') { $orderlist = M('orderlist'); $checkwhere['uid'] = $adminuid; $checkwhere['ordid'] = $_GET['out_trade_no']; $checkwhere['payment_trade_status'] = 'TRADE_FALSE'; $checkwhere['ordfee'] = $_GET['total_fee']; $checkrs = $orderlist->where($checkwhere)->find(); if(!$checkrs) { $this->error('无效订单,请从新支付',U('Admin/recharge'),5);exit; } //更新支付宝数据, $savewhere['ordid'] = $_GET['out_trade_no']; $orderlistsave['payment_trade_status'] = $_GET['trade_status']; $orderlistsave['ordovertime'] = $_GET['notify_time']; $savedata = $orderlist->where($savewhere)->data($orderlistsave)->save(); //更新用户金额表数据 $finance = M('finance'); $yue['uid'] = $adminuid; $money = $finance->where($yue)->find(); $endmoney = bcadd($money['money'],$_GET['total_fee'],2); $moneywhere['uid'] = $adminuid; $moneysave['money'] = $endmoney; $rsmoney = $finance->where($moneywhere)->data($moneysave)->save(); if($rsmoney){ $applywithdraw = M('applywithdraw'); $applywithdrawadd['uid'] = $adminuid; $applywithdrawadd['order'] = date("YmdHis", time()).mt_rand(10000,99999);//订单号 $applywithdrawadd['type'] = 3;//1:推荐,2:人气,3:充值,4:提现,5:合成名片,6:视频名片 $applywithdrawadd['money'] = $_GET['total_fee']; $applywithdrawadd['status'] = 1; $applywithdrawadd['createtime'] = date("Y-m-d H:i:s", time()); $applywithdrawadd['remark'] = $_GET['out_trade_no']; $addrs = $applywithdraw->add($applywithdrawadd); } $this->redirect($config['alipay']['successpage']);//跳转到配置项中配置的支付成功页面; } else { echo "trade_status=".$_GET['trade_status']; $this->redirect($config['alipay']['errorpage']);//跳转到配置项中配置的支付失败页面; } } else { //验证失败 //如要调试,请看alipay_notify.php页面的verifyReturn函数 //echo "支付失败!"; $this->error('支付失败!',U('Admin/recharge'),5);exit; } } }
function notifyurl() { /* 同理去掉以下两句代码; */ //require_once("alipay.config.php"); //require_once("lib/alipay_notify.class.php"); //这里还是通过C函数来读取配置项,赋值给$alipay_config $config['alipay_config'] = C('alipay_config'); $config['alipay'] = C('alipay'); //计算得出通知验证结果 $alipayNotify = new Notify( $config['alipay_config']); $verify_result = $alipayNotify->verifyNotify(); if($verify_result) { //验证成功 //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 $out_trade_no = $_POST['out_trade_no']; //商户订单号 $trade_no = $_POST['trade_no']; //支付宝交易号 $trade_status = $_POST['trade_status']; //交易状态 $total_fee = $_POST['total_fee']; //交易金额 $notify_id = $_POST['notify_id']; //通知校验ID。 $notify_time = $_POST['notify_time']; //通知的发送时间。格式为yyyy-MM-dd HH:mm:ss。 $buyer_email = $_POST['buyer_email']; //买家支付宝帐号; $parameter = array( "out_trade_no" => $out_trade_no, //商户订单编号; "trade_no" => $trade_no, //支付宝交易号; "total_fee" => $total_fee, //交易金额; "trade_status" => $trade_status, //交易状态 "notify_id" => $notify_id, //通知校验ID。 "notify_time" => $notify_time, //通知的发送时间。 "buyer_email" => $buyer_email, //买家支付宝帐号; ); if($_POST['trade_status'] == 'TRADE_FINISHED') { // } else if($_POST['trade_status'] == 'TRADE_SUCCESS') { $orderlist = M('orderlist'); $checkwhere['uid'] = $adminuid; $checkwhere['ordid'] = $_GET['out_trade_no']; $checkwhere['payment_trade_status'] = 'TRADE_FALSE'; $checkwhere['ordfee'] = $_GET['total_fee']; $checkrs = $orderlist->where($checkwhere)->find(); if(!$checkrs) { $this->error('无效订单,请从新支付',U('Admin/recharge'),5);exit; } //更新支付宝数据, $savewhere['ordid'] = $_GET['out_trade_no']; $orderlistsave['payment_trade_status'] = $_GET['trade_status']; $orderlistsave['ordovertime'] = $_GET['notify_time']; $savedata = $orderlist->where($savewhere)->data($orderlistsave)->save(); //更新用户金额表数据 $finance = M('finance'); $yue['uid'] = $adminuid; $money = $finance->where($yue)->find(); $endmoney = bcadd($money['money'],$_GET['total_fee'],2); $moneywhere['uid'] = $adminuid; $moneysave['money'] = $endmoney; $rsmoney = $finance->where($moneywhere)->data($moneysave)->save(); if($rsmoney){ $applywithdraw = M('applywithdraw'); $applywithdrawadd['uid'] = $adminuid; $applywithdrawadd['order'] = date("YmdHis", time()).mt_rand(10000,99999);//订单号 $applywithdrawadd['type'] = 3;//1:推荐,2:人气,3:充值,4:提现,5:合成名片,6:视频名片 $applywithdrawadd['money'] = $_GET['total_fee']; $applywithdrawadd['status'] = 1; $applywithdrawadd['createtime'] = date("Y-m-d H:i:s", time()); $applywithdrawadd['remark'] = $_GET['out_trade_no']; $addrs = $applywithdraw->add($applywithdrawadd); } $this->redirect($config['alipay']['successpage']);//跳转到配置项中配置的支付成功页面; } echo "success"; //请不要修改或删除 } else { //验证失败 //echo "fail"; $this->error('支付失败!',U('Admin/recharge'),5);exit; } }
//返回myorder输出信息
public function myorder(){ if($_GET['ordtype'] == 'payed'){ $this->success('支付成功!',U('Admin/myfinance'),5);exit; }else{ $this->success('支付失败!',U('Admin/recharge'),5);exit; } $this->success('未知请求!',U('Admin/admin'),5);exit; }
aliconfig.php
<?php return array( //支付宝配置参数 'alipay_config'=>array( 'partner' =>'000000000', //这里是你在成功申请支付宝接口后获取到的PID; 'key'=>'xxxxxxxxxxxxxx',//这里是你在成功申请支付宝接口后获取到的Key 'sign_type'=>strtoupper('MD5'), 'input_charset'=> strtolower('utf-8'), 'cacert'=> getcwd().'\\cacert.pem', 'transport'=> 'http', ), //以上配置项,是从接口包中alipay.config.php 文件中复制过来,进行配置; 'alipay' =>array( //这里是卖家的支付宝账号,也就是你申请接口时注册的支付宝账号 'seller_email'=>'xxx@xx.com', //这里是异步通知页面url,提交到项目的Pay控制器的notifyurl方法; 'notify_url'=>'http://test.xxxxx.cn/index.php/Home/Admin/notifyurl', //这里是页面跳转通知url,提交到项目的Pay控制器的returnurl方法; 'return_url'=>'http://test.xxxxx.cn/index.php/Home/Admin/returnurl', //支付成功跳转到的页面,我这里跳转到项目的User控制器,myorder方法,并传参payed(已支付列表) 'successpage'=>'Admin/myorder?ordtype=payed', //支付失败跳转到的页面,我这里跳转到项目的User控制器,myorder方法,并传参unpay(未支付列表) 'errorpage'=>'Admin/myorder?ordtype=unpay', ), ); ?>
集成接口 http://download.alipay.com/public/api/base/alipaydirect.zip