支付宝支付
(由app端自行调起支付宝/微信)
<?php
define('IN_ECS', true);
/*App支付 PHP服务端*/
/*引入支付宝PHP SDK*/
header("Content-type:text/html;charset=utf-8;");
require_once('../alipay-sdk-PHP/aop/request/AlipayTradeAppPayRequest.php');
require_once('../alipay-sdk-PHP/aop/AopClient.php');
$user_id = isset($_REQUEST['user_id']) ? trim($_REQUEST['user_id']) : 0;
$money = isset($_REQUEST['amount']) ? trim($_REQUEST['amount']) : 0;
if($user_id == 0 || $money ==0){
$result = array('code' => 1,'data'=>'缺少参数' );
die(json_encode($result));
}
$aop = new AopClient;
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = "appid";
$aop->format = "json";
$aop->charset = "utf-8";
$aop->signType = "RSA2";
$aop->rsaPrivateKey = '开发者私钥';
$aop->alipayrsaPublicKey = '支付宝公钥';
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
$trade_no = chongzhi_sn();
//入库信息
require(dirname(__FILE__) . '../../includes/init.php');
include_once(ROOT_PATH .'includes/lib_clips.php');
$user_m = get_user_info($user_id);
$dbcontent = array(
'user_id' => $user_id,
'amount' => $money,
'user_name' => $user_m['user_name'],
'payment' => "支付宝",
'cz_sn' => $trade_no,
'add_time' => gmtime()
);
//传参信息
$bizcontent = array(
'body' =>(string)$trade_no,
'subject' =>'充值',
'total_amount' => $money,
'out_trade_no' => (string)$trade_no,
'product_code' => 'QUICK_MSECURITY_PAY',
'timeout_express'=> '30m'
);
$bizcontent = json_encode($bizcontent);
$request->setNotifyUrl("支付回调地址");
$request->setBizContent($bizcontent);
//这里和普通的接口调用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
//echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
if($response){
if($GLOBALS['db']->autoExecute($ecs->table('user_chongzhi'), $dbcontent, 'INSERT'))
{
$result = array('code'=>0,'data'=>$response);
}else{
$result = array('code'=>1,'data'=>'意外出错,请联系客服。');
}
die(json_encode($result));
}
function chongzhi_sn()
{
/* 选择一个随机的方案 */
mt_srand((double) microtime() * 1000000);
return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}
?>
回调
<?php
define('IN_ECS', true);
require_once('../alipay-sdk-PHP/aop/request/AlipayTradeAppPayRequest.php');
require_once('../alipay-sdk-PHP/aop/AopClient.php');
$aop = new AopClient;
$aop->alipayrsaPublicKey = '支付宝公钥';
$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
$myfile = fopen("testfile.txt", "a");
fwrite($myfile, "\r\n");
fwrite($myfile, json_encode($_POST));
if($_POST['trade_status'] == 'TRADE_SUCCESS' ){
//业务处理
echo 'success';
}else{
echo 'fail';
}
fclose($myfile);
?>
所需的私钥公钥获取地址
1.私钥 (下载rsa签名验签工具)https://docs.open.alipay.com/291/105971/
2.支付宝公钥
通过上传私钥,由支付宝生成公钥 https://docs.open.alipay.com/291/105972/
3.支付宝联调日志排查 https://openmonitor.alipay.com/acceptance/cloudparse.htm