首先在tp5.0中的/extend中放人支付宝的sdk
然后在/application/index/controller 新建立一个php文件为Alipay.php
里面放支付宝支付和验签
<?php
namespace app\index\controller;
use think\Config;
class Alipay
{
/*
* 支付宝支付
* $body 名称
* $total_amount 价格
* $product_code 订单号
* $notify_url 异步回调地址
*/
public function alipay($body, $total_amount, $product_code, $notify_url)
{
/**
* 调用支付宝接口。
*/
import('.Alipay.aop.AopClient', '', '.php');
import('.Alipay.aop.request.AlipayTradeAppPayRequest', '', '.php');
$aop = new \AopClient();
$aop->gatewayUrl = Config::get('alipay')['gatewayUrl'];
$aop->appId = Config::get('alipay')['appId'];
$aop->rsaPrivateKey = Config::get('alipay')['rsaPrivateKey'];
$aop->format = Config::get('alipay')['format'];
$aop->charset = Config::get('alipay')['charset'];
$aop->signType = Config::get('alipay')['signType'];
$aop->alipayrsaPublicKey = Config::get('alipay')['alipayrsaPublicKey'];
$request = new \AlipayTradeAppPayRequest();
$arr['body'] = $body;
$arr['subject'] = $body;
$arr['out_trade_no'] = $product_code;
$arr['timeout_express'] = '30m';
$arr['total_amount'] = floatval($total_amount);
$arr['product_code'] = 'QUICK_MSECURITY_PAY';
$json = json_encode($arr);
$request->setNotifyUrl($notify_url);
$request->setBizContent($json);
$response = $aop->sdkExecute($request);
return $response;
}
//验签
public function rsaCheckV1($data)
{
$aop = new \AopClient();
$aop->alipayrsaPublicKey = "支付宝公钥";
$flag = $aop->rsaCheckV1($data, NULL, "RSA2");
return $flag;
}
}
在/application/config.php 里面放支付宝参数
没有公钥和私钥的可以在支付宝中https://docs.open.alipay.com/291/105971/中申请,在支付宝中只放公钥,私钥要好好保留。
'alipay'=>[
'appId' => 'appId',
'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
'rsaPrivateKey' => '私钥',
'rsaPublicKey' => '公钥',
'seller' => '',//邮箱
'format' => 'json',
'charset' => 'UTF-8',
'signType' => 'RSA2',
'transport' => 'http',
]
都准备好以后可以在/application/index/controller建立一个Payment.php 文件写逻辑了。
<?php
namespace app\index\controller;
use think\Config;
use think\Request;
use think\Db;
class Payment extends Controller
{
public function payorder()
{
if (input('post.pay_type') == 'alipay') {
$orderId=$this->order_number();
$name="商品名称";
$order_price=0.01;
$alipay = new Alipay();
$url ='域名'.'/index/Payment/alipay_notify.html';
$array['aliapy'] = $alipay->alipay($name,$order_price,orderId, $url);
if ($array) {
$result = array('code' => 1, 'msg' => '成功', 'data' => $array);
return json($result);
} else {
$result = array('code' => 0, 'msg' => '对不起请检查相关参数!');
return json($result);
}
}else{
$result = array('code' => 0, 'msg' => '缺少pay_type');
return json($result);
}
}
/*
*回调
*/
public function alipay_notify()
{
//回调回来是个数组,你们可以用var_export接受下放在文件中查看
//把生成json后,然后ksort()排序,去除sign_type,sign,拼接成body=Hello&buyer_email=13788888888&buyer_id=2088002007013600..............这样字符串
$alipay = new Alipay();
//data就是字符串
$alipay->rsaCheckV1($data);
/**
* ①验签通过后核实如下参数out_trade_no、total_amount、seller_id
* ②修改订单表
**/
//打印success,应答支付宝。必须保证本界面无错误。只打印了success,否则支付宝将重复请求回调地址。
echo 'success';
}
public function order_number()
{
static $ORDERSN = array();
$ors = date('ymd') . substr(time(), -5) . substr(microtime(), 2, 5);
if (isset($ORDERSN[$ors])) {
$ORDERSN[$ors]++;
} else {
$ORDERSN[$ors] = 1;
}
return $ors . str_pad($ORDERSN[$ors], 2, '0', STR_PAD_LEFT);
}
}