支付宝支付
1:首先去支付宝的官网下载第三方sdk,如下图放在vendor下面
然后在公共方法里面封装一个pay.class.php的方法,来处理第三方支付
贴上源码
//支付宝
public function alipay($out_trade_no,$total_amount,$body,$subject){
$config = C('payment.alipay');//读取配置
//超时时间
$timeout_express="1m";
/*pc支付类*/
Vendor('Alipay.wappay.service.AlipayTradeService');//导入包
Vendor('Alipay.wappay.buildermodel.AlipayTradePagePayContentBuilder');//导入包
$payRequestBuilder = new \AlipayTradePagePayContentBuilder();
Vendor('Alipay.wappay.service.AlipayService');
$aliPay = new \AlipayService();
$aliPay->setAppid($config['app_id']);
$aliPay->setNotifyUrl($config['notify_url']);
$aliPay->setRsaPrivateKey($config['merchant_private_key']);
$aliPay->setTotalFee($total_amount);
$aliPay->setOutTradeNo($out_trade_no);
$aliPay->setOrderName($body);
$result = $aliPay->doPay();
$result = $result['alipay_trade_precreate_response'];
if(intval($result['code'])===10000 && $result['msg']==='Success'){
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setTimeExpress($timeout_express);
$payResponse = new \AlipayTradeService($config);
$result = $payResponse->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);
//输出表单
var_dump($result);die;
}
}
然后扫码之后还存在一个回调问题
1:同步回调跳转地址
/**
* 支付宝同步回调处理方法
*/
public function return_url(){
$config = C('payment.alipay');
Vendor('Alipay.wappay.service.AlipayTradeService');
$arr = $_GET;
$alipaySevice = new \AlipayTradeService($config);
$result = $alipaySevice->check($arr);
if($result) {
$url = U('');
Header("Location:$url");
}else {
$url = U('');
Header("Location:$url");
}
}
2:异步回调处理数据
/**
* 支付宝异步回调处理方法
*/
public function notify_url(){
$config = C('payment.alipay');
Vendor('Alipay.wappay.service.AlipayTradeService');
$arr = $_POST;
$alipaySevice = new \AlipayTradeService($config);
$result = $alipaySevice->check($arr);
// \Think\Log::record(json_encode($result));exit;
if($result) {
/*app_id是否为该商户本身*/
if($arr['app_id'] != $config['app_id']){
echo "fail";exit;
}
//验证成功
//获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表
// 获取相关数据
$out_trade_no = $arr['out_trade_no']; //商户订单号
$deal_no = $arr['trade_no']; //支付宝交易号
$trade_status = $arr['trade_status']; //交易状态
$money_real = $arr['total_amount']; //交易金额
$seller_id = $arr['buyer_id']; //卖家支付宝账号id
if($trade_status == 'TRADE_FINISHED' OR $trade_status == 'TRADE_SUCCESS') {
//订单处理的地方
}else{
//失败了之后
echo 'fail';
}
}else {
//验证失败
echo "fail";
}
2:微信支付
(目录结构)
//微信支付
public function wxpay($out_trade_no,$order){
Vendor('Wxpay.Weixinpay');
$weixinpay = new \Weixinpay();
$result = $weixinpay->unifiedOrder($order);
if($result['return_code'] = 'SUCCESS'){
$return['code_url'] = $result['code_url'];
$return['out_trade_no'] = $out_trade_no;
$return['status'] = 1;
$return['time'] = time();
$return['total_fee'] = $order['total_fee']/100;
$url = '需要跳转的扫码地址?out_trade_no='.$out_trade_no.'&total_fee='.$return['total_fee'].'&url='.$return['code_url'].'&time='.$return['time'];
header('Location: '.$url);
}else{
$return['status'] = 0;
}
}
异步回调
//微信回调
/**
* 微信异步回调处理方法
*/
public function wx_notify_url(){
vendor('Wxpay.Weixinpay');
$wx_config = C('payment.wxpay');
$wxpay = new \Weixinpay($wx_config);
$result = $wxpay->notify();
if ($result === false) {
exit;
}else{
/*app_id是否为该商户本身*/
if($result['appid'] != $wx_config['appid']){
echo "fail";exit;
}
//商户订单号
if($result['result_code'] == "SUCCESS" OR $result['return_code'] == "SUCCESS"){
//处理订单 成功之后要返回success
echo 'success';
}else{
//失败了打印fail
echo 'fail';
}
}
}
}