<Thinkphp3.2对接支付宝即时到账接口
步骤如下
下载支付宝即时到账接口,可以自己百度找,也可以直接下载我的
在Thinkphp3.2的Libs/ThinkPHP/Library/Vendor目录下创建一个alipay目录,并且把支付宝即时到账接口解压后,把lib目录复制到alipay目录,如下


把压缩包里的alipay.config.php里面的配置,复制到我们项目的App/Common/Conf/config.php里面修改并且配置
代码如下
'alipay_config'=>array(
//合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm
'partner'=> '11111111',
//收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
// 'seller_id']= 'partner'];
'seller_id'=> "1111111111111111111",
// MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm
'key'=> '2222222222222222222',
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
'notify_url' => "http://xxxxx.com/Alipay/ailpay_notify",
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
'return_url' => "http://xxxxx.com/Alipay/result",
//签名方式
'sign_type' => strtoupper('MD5'),
//字符编码格式 目前支持 gbk 或 utf-8
'input_charset'=> strtolower('utf-8'),
//ca证书路径地址,用于curl中ssl校验
//请保证cacert.pem文件在当前文件夹目录中
'cacert' => getcwd().'\\cacert.pem',
//访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
'transport' => 'http',
// 支付类型 ,无需修改
'payment_type' => "1",
// 产品类型,无需修改
'service' => "create_direct_pay_by_user",
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//↓↓↓↓↓↓↓↓↓↓ 请在这里配置防钓鱼信息,如果没开通防钓鱼功能,为空即可 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 防钓鱼时间戳 若要使用请调用类文件submit中的query_timestamp函数
'anti_phishing_key' => "",
// 客户端的IP地址 非局域网的外网IP地址,如:221.0.0.1
'exter_invoke_ip' => "",
),
3.在我们的前台home模块下的View目录下创建一个Alipay目录,并在里面创建一个支付页面,用于填写支付金额
内容如下,样式我就不贴了,看功能即可

充值中心
充值
并且在Home模块下的View目录下创建一个pay.html文件,用于确认支付信息并提交支付
代码如下:

账户充值
关于支付宝|
商家中心|
诚征英才|
联系我们|
International Business|
About Alipay
支付宝版权所有
2004-2016
ICP证:沪B2-20150087
4.在home模块下面controller里面创建一个AlipayController.class.php
代码如下
error("请输入充值金额!");
exit;
}
if ($info['order_price'] < 1) {
$this->error("充值金额不能小于1!");
exit;
}
$userinfo = $this->getuserinfo();
$order_code = "DZ" . date("YmdHis") . rand(1111, 9999);
$data = array("order_code" => $order_code, "order_num" => 1, "order_price" => $info['order_price'], "order_actual_price" => $info['order_price'], "user_id" => $userinfo['id'], "order_state" => 0, "pay_state" => 0, "pay_type" => $info["pay_type"], "addtime" => time());
$result = M("order")->add($data);
if (!$result) {
echo "支付失败,请返回重试!";
exit;
} else {
header("Location:" . $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . "/Alipay/Pay/order_code/" . $order_code);
}
} else {
$info = M("order")->field("order_code,order_price,order_actual_price")->where("order_code='" . I('get.order_code') . "'")->find();
if (!$info) {
echo "非法操作!";
exit;
}
$this->assign("info", $info);
$this->display();
}
}
//提交支付
public function Alipayapi() {
header("Content-type: text/html; charset=utf-8");
Vendor("alipay.lib.alipay_submit");
$alipay_config = C("alipay_config");
/**************************请求参数**************************/
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = $_POST['WIDout_trade_no'];
//订单名称,必填
$subject = $_POST['WIDsubject'];
//付款金额,必填
$total_fee = $_POST['WIDtotal_fee'];
//商品描述,可空
$body = $_POST['WIDbody'];
/************************************************************/
//构造要请求的参数数组,无需改动
$parameter = array("service" => $alipay_config['service'], "partner" => $alipay_config['partner'], "seller_id" => $alipay_config['seller_id'], "payment_type" => $alipay_config['payment_type'], "notify_url" => $alipay_config['notify_url'], "return_url" => $alipay_config['return_url'], "anti_phishing_key" => $alipay_config['anti_phishing_key'], "exter_invoke_ip" => $alipay_config['exter_invoke_ip'], "out_trade_no" => $out_trade_no, "subject" => $subject, "total_fee" => $total_fee, "body" => $body, "_input_charset" => trim(strtolower($alipay_config['input_charset']))
//其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1
//如"参数名"=>"参数值"
);
//建立请求
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter, "get", "确认");
echo $html_text;
}
//支付宝异步回调
public function ailpay_notify() {
Vendor("alipay.lib.alipay_notify");
$alipay_config = C("alipay_config");
//计算得出通知验证结果
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyNotify();
if ($verify_result) { //验证成功
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//请在这里加上商户的业务逻辑程序代
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
//获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表
$orderdata = $_POST;
if ($_POST['trade_status'] == 'TRADE_FINISHED') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
//调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
$odb = D("order");
$udb = D("member");
$odb->startTrans();
$orderinfo = $odb->field("order_code,order_price,order_state,pay_type,user_id,order_actual_price")->where("order_code='{$orderdata['out_trade_no']}'")->find();
if ($orderinfo) {
if ($orderinfo['order_actual_price'] == $orderdata['total_fee'] && $orderinfo['order_state'] == 0 && $orderinfo['pay_state'] == 0) {
$result = $odb->where("order_code='{$orderdata['out_trade_no']}'")->save(array("order_state" => 1, "pay_state" => 1, "pay_time" => time()));
if (!$result) {
$odb->rollback();
echo "fail";
exit;
} else {
$bl = M("setting")->where("ckey='Rechargebl'")->getField("cvalue");
if (!$bl) $bl = 1;
$result = $udb->where("id='{$orderinfo['user_id']}'")->setInc("balance", $bl * $orderinfo['order_price']);
if ($result) {
$odb->commit();
} else {
echo "fail";
exit;
}
}
} else {
echo "fail";
exit;
}
} else {
echo "fail";
exit;
}
} else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//付款完成后,支付宝系统发送该交易状态通知
//调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
$odb = D("order");
$udb = D("member");
$odb->startTrans();
$orderinfo = $odb->field("order_code,order_price,order_actual_price,order_state,pay_type,user_id")->where("order_code='{$orderdata['out_trade_no']}'")->find();
if ($orderinfo) {
if ($orderinfo['order_actual_price'] == $orderdata['total_fee'] && $orderinfo['order_state'] == 0 && $orderinfo['pay_state'] == 0) {
$result = $odb->where("order_code='{$orderdata['out_trade_no']}'")->save(array("order_state" => 1, "pay_state" => 1, "pay_time" => time()));
if (!$result) {
$odb->rollback();
echo "fail";
exit;
} else {
$bl = M("setting")->where("ckey='Rechargebl'")->getField("cvalue");
if (!$bl) $bl = 1;
$result = $udb->where("id='{$orderinfo['user_id']}'")->setInc("balance", $bl * $orderinfo['order_price']);
if ($result) {
$odb->commit();
} else {
echo "fail";
exit;
}
}
} else {
echo "fail";
exit;
}
} else {
echo "fail";
exit;
}
}
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
echo "success"; //请不要修改或删除
} else {
//验证失败
echo "fail";
//调试用,写文本函数记录程序运行情况是否正常
//logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
}
}
//支付成功跳转地址
public function result() {
$orderdata = $_GET;
$odb = D("order");
$udb = D("member");
$odb->startTrans();
$orderinfo = $odb->field("order_code,order_price,order_actual_price,order_state,pay_type,user_id")->where("order_code='{$orderdata['out_trade_no']}'")->find();
if ($orderinfo) {
if ($orderinfo['order_state'] == 1 && $orderinfo['pay_state'] == 1) {
$this->success("支付成功!", U("Member/Recharge", false));
exit;
} else {
if ($orderdata['trade_status'] == "TRADE_SUCCESS") {
if ($orderinfo['order_actual_price'] == $orderdata['total_fee']) {
if ($orderinfo['order_state'] == 0 && $orderinfo['pay_state'] == 0) {
$result = $odb->where("order_code='{$orderdata['out_trade_no']}'")->save(array("order_state" => 1, "pay_state" => 1, "pay_time" => time()));
if (!$result) {
$odb->rollback();
$this->error("支付失败!", U("Member/Recharge", false));
exit;
} else {
$bl = M("setting")->where("ckey='Rechargebl'")->getField("cvalue");
if (!$bl) $bl = 1;
$result = $udb->where("id='{$orderinfo['user_id']}'")->setInc("balance", $bl * $orderinfo['order_price']);
if ($result) {
$odb->commit();
$this->success("支付成功!", U("Member/Recharge", false));
exit;
} else {
$this->error("支付失败!", U("Member/Recharge", false));
exit;
}
}
} else {
$this->success("支付成功!", U("Member/Recharge", false));
exit;
}
} else {
$this->error("支付失败!", U("Member/Recharge", false));
exit;
}
} else {
$this->error("支付失败!", U("Member/Recharge", false));
}
}
} else {
$this->error("支付失败!", U("Member/Recharge", false));
exit;
}
}
}
?>到这里代码结束,如果觉得乱的,下载上面的压缩包,代码都在里面
>