注:至于如何申请支付宝支付以及获取相关密钥不在此范围内。
本篇文章只是针对代码层面。
第一步:从支付宝相关页面下载sdk php版本的压缩包,将其文件解压到:extend\alipaySDK 目录下,如下图:
第二步:直接上代码 并将代码放入文件 test\controller\alipay.php 中[拿走,不谢]:
<?php
namespace app\test\controller;
use think\Controller;
use think\Request;
////// 支付宝相关配置 参数 start ///////////
define("AOP_SDK_WORK_DIR", RUNTIME_PATH . "/alipaytmp/");
define("AOP_SDK_DEV_MODE", true);//如果要提高性能可以设置为false
////// 支付宝相关配置 参数 end ///////////
class Alipay extends Controller {
private $filelog = '';
public function __construct(Request $request = null)
{
parent::__construct($request);
if(!is_dir(AOP_SDK_WORK_DIR))
{
@mkdir(AOP_SDK_WORK_DIR, 0777, true);
}
$this->filelog = AOP_SDK_WORK_DIR .'alipay.log';
}
public function getAlipayConf()
{
$conf = config('config_alipay');//支付宝相关配置放在 application/extra/config_alipay.php文件中
return $conf;
}
//请求alipay支付
public function alipay_request()
{
import('.alipaySDK.aop.AopClient', '', '.php');
import('.alipaySDK.aop.request.AlipayTradeAppPayRequest', '', '.php');
$filelog = $this->filelog;
$conf = $this->getAlipayConf();
$aop = new \AopClient();
//$aop->gatewayUrl = config('config_alipay')['gatewayUrl'];
$aop->appId = $conf['appId'];
$aop->rsaPrivateKey = $conf['rsaPrivateKey'];
//$aop->format = config('config_alipay')['format'];
//$aop->charset = config('config_alipay')['charset'];
$aop->signType = $conf['signType'];
$aop->alipayrsaPublicKey = $conf['alipayRsaPublicKey'];
$aop->debugInfo=true;
$request = new \AlipayTradeAppPayRequest();
$body = '测试生成的订单'.time();
//$arr['body'] = $body;
$arr['subject'] = $body;
$arr['out_trade_no'] = 'ALI'.time();
//$arr['out_trade_no'] = 'ALI123456';
$arr['timeout_express'] = '30m';
$arr['total_amount'] = floatval(0.01);//单位为元
$arr['product_code'] = 'QUICK_MSECURITY_PAY';
$json = json_encode($arr);
$request->setNotifyUrl('https://dev.xxx.cn/test/alipay/alipay_notify');
$request->setBizContent($json);
$response = $aop->sdkExecute($request);
filelog($filelog, '第1步:发送请求数据:'.json_encode($arr, 320), true);
filelog($filelog, '第1.1步:得到如下结果:'.json_encode($response, 320), true);
$ret = urldecode($response);
//dd($ret);
//parse_str($response, $retdata);
ajaxOutFunc(200, $response);
}
public function rsaCheckV1($data)
{
$aop = new \AopClient();
$aop->alipayrsaPublicKey = config('config_alipay')['alipayRsaPublicKey'];
$flag = $aop->rsaCheckV1($data, NULL, "RSA2");
return $flag;
}
//接收通知
public function alipay_notify()
{
import('.alipaySDK.aop.AopClient', '', '.php');
$filelog = $this->filelog;
filelog($filelog, '第==步:开始接收通知', true);
//回调回来是个数组,你们可以用var_export接受下放在文件中查看
//把生成json后,然后ksort()排序,去除sign_type,sign,拼接成body=Hello&buyer_email=13788888888&buyer_id=2088002007013600..............这样字符串
filelog($filelog, '第2.0步:得到如下原始通知:'.json_encode(input(''), 320), true);
$data = input('post.');
filelog($filelog, '第2.1步:得到如下原始通知:'.json_encode($data, 320), true);
$org_str = var_export($data, true);
filelog($filelog, '第2.2步:原始通知执行var_export后:'. json_encode($org_str, 320), true);
filelog($filelog, '第3步:拼接成连接串:'.http_build_query($data), true);
$check = $this->rsaCheckV1($data);
filelog($filelog, '第4步:验证签名结果:'.json_encode($check, 320), true);
/**
* ①验签通过后核实如下参数out_trade_no、total_amount、seller_id
* ②修改订单表
**/
//打印success,应答支付宝。必须保证本界面无错误。只打印了success,否则支付宝将重复请求回调地址。
if($check) {
echo 'success';
}else{
echo 'fail';
}
}
/**
* 支付宝退款--原路退回
*/
public function ali_refund()
{
$out_trade_no = input('out_trade_no', ''); //订单号
if(empty($out_trade_no))
{
exit('$out_trade_no为空');
}
$filelog = AOP_SDK_WORK_DIR .'alipay_refund.log';
import('.alipaySDK.aop.SignData', EXTEND_PATH, '.php');
import('.alipaySDK.aop.AopClient', '', '.php');
import('.alipaySDK.aop.request.AlipayTradeRefundRequest', EXTEND_PATH, '.php');
//import('alipaySDK.aop.AopClient', EXTEND_PATH, '.php');
$biz_content = [
'out_trade_no' => $out_trade_no,
'trade_no' => '',
'refund_amount' => '0.01',//单位为元
'out_request_no'=> '',
];
$json_biz = json_encode($biz_content);
filelog($filelog, '第0步:开始退款了,退款数据:'.json_encode($biz_content, 320), true);
$config = $this->getAlipayConf();
$aop = new \AopClient();
$aop->gatewayUrl = $config['gatewayUrl'];
$aop->appId = $config['appId'];
$aop->rsaPrivateKey = $config['rsaPrivateKey'];
$aop->alipayrsaPublicKey=$config['alipayRsaPublicKey'];
$aop->apiVersion = '1.0';
$aop->signType = $config['signType'];
$aop->postCharset=$config['charset'];
$aop->format='json';
filelog($filelog, '第1步:准备请求:AlipayTradeRefundRequest退款接口', true);
$request = new \AlipayTradeRefundRequest();
$request->setBizContent($json_biz);
$result = $aop->execute($request);
filelog($filelog, '第3步:发送数据:'.json_encode($biz_content, 320), true);
filelog($filelog, '第4步:返回数据:'.json_encode($result, 320), true);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode) && $resultCode == 10000){
echo "成功";
} else {
echo "失败";
}
}
}
注:filelog用于文件记录函数,ajaxOutFunc用于输出json字符串。
建议:各位新手,如果项目紧急,可以直接拿代码下来,加上你的业务逻辑,即可使用。如果项目不是非常赶,可以到github上找一款第三方开源的集成支付宝、微信支付的sdk。
|