【PHP】PHP服务端支付宝支付及回调

支付宝支付

(由app端自行调起支付宝/微信)

1.下载PHP版SDK 

 1 <?php
 2 
 3 define('IN_ECS', true);
 4 
 5 /*App支付 PHP服务端*/
 6 /*引入支付宝PHP SDK*/
 7 header("Content-type:text/html;charset=utf-8;");
 8 require_once('../alipay-sdk-PHP/aop/request/AlipayTradeAppPayRequest.php');
 9 require_once('../alipay-sdk-PHP/aop/AopClient.php');
10 
11 
12 $user_id  = isset($_REQUEST['user_id']) ? trim($_REQUEST['user_id']) : 0;
13 $money = isset($_REQUEST['amount']) ? trim($_REQUEST['amount']) : 0;
14 
15 if($user_id == 0 || $money ==0){
16     $result  = array('code' => 1,'data'=>'缺少参数' );
17     die(json_encode($result));
18 }
19 
20 
21 $aop = new AopClient;
22 $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
23 $aop->appId = "appid";
24 $aop->format = "json";
25 $aop->charset = "utf-8";
26 $aop->signType = "RSA2";
27 $aop->rsaPrivateKey = '开发者私钥';
28 
29 $aop->alipayrsaPublicKey = '支付宝公钥';
30 
31 
32 
33 //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
34 
35 $request = new AlipayTradeAppPayRequest();
36 //SDK已经封装掉了公共参数,这里只需要传入业务参数
37 $trade_no = chongzhi_sn();
38 //入库信息
39 require(dirname(__FILE__) . '../../includes/init.php');
40 include_once(ROOT_PATH .'includes/lib_clips.php');
41 
42 $user_m = get_user_info($user_id);
43 
44 $dbcontent = array(
45     'user_id'    => $user_id,
46     'amount'  => $money,
47     'user_name' => $user_m['user_name'],
48     'payment' => "支付宝",
49     'cz_sn'  => $trade_no,
50     'add_time'  => gmtime()
51 );
52 //传参信息
53 $bizcontent = array(
54     'body' =>(string)$trade_no,
55     'subject'  =>'充值',
56     'total_amount'  => $money,
57     'out_trade_no'  => (string)$trade_no,
58     'product_code' => 'QUICK_MSECURITY_PAY',
59     'timeout_express'=> '30m'
60 );
61 
62 $bizcontent = json_encode($bizcontent);
63 $request->setNotifyUrl("支付回调地址");
64 $request->setBizContent($bizcontent);
65 //这里和普通的接口调用不同,使用的是sdkExecute
66 $response = $aop->sdkExecute($request);
67 //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
68 //echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
69 
70 if($response){
71     if($GLOBALS['db']->autoExecute($ecs->table('user_chongzhi'), $dbcontent, 'INSERT'))
72     {
73         $result = array('code'=>0,'data'=>$response);
74     }else{
75         $result = array('code'=>1,'data'=>'意外出错,请联系客服。');
76     }
77     die(json_encode($result));
78 }
79 
80 function chongzhi_sn()
81 {
82     /* 选择一个随机的方案 */
83     mt_srand((double) microtime() * 1000000);
84     return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
85 }
86 
87 ?> 

回调

<?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

posted @ 2018-01-15 10:00  rohua  阅读(6857)  评论(0编辑  收藏  举报