php对接支付宝支付学习笔记

 

order/pay&orderid=2&paymethod=alipay

支付SDK类库

AlipayCore.php
AlipayMD5.php
AlipayNotify.php
AlipayPay.php
AlipaySubmit.php
cacert.pem
log.txt
readme.txt

异步通知地址
$notify_url=

回调地址,页面跳转同步通知地址
$return_url=

 

订单控制器类
class OrderController extends CommonController{
	//订单控制器的其他方法
	public function actionPay(){
		try{
			$orderid=Yii::$app->request->get('orderid');
			$paymethod=Yii::$app->request->get('paymethod');
			if(empty($orderid)||empty($paymethod)){
				throw new \Exception();
			}	
			if($paymethod=='alipay'){
				return Pay::alipay($orderid);
			}
		}catch(\Exception $e){
			return $this->redirect(['order/index']);
		}
	}
}

支付模型类

namespace app\models;
use app\models\Order;
use app\models\OrderDetail;
use app\models\Product;
class Pay{
	public static function alipay($orderid){
		//叮当总价
		$amount=Order::find()->where('orderid=:oid',[':oid'=>$orderid])->one()->amount;
		if(!empty($amount)){
			$alipay=new \AlipayPay();
			$giftname="慕课商城";
			$data=OrderDetail::find()->where('orderid=:oid',[':oid'=>$orderid])->all();
			$body="";
			foreach($data as $pro){
				$body.=Product::find()->where('productid=:pid',[':pid'=>$pro['productid'])->one()->title."-";
			}
			$body.="等商品";
			$showUrl="http://shop.mr-jason.com";
			$html=$alipay->requestPay($orderid,$giftname,$amount,$body,$showUrl);
			echo $html;
		}
	}
	public staitc function notify($data){
		$alipay=new \AlipayPay();
		$verify_result=$alipay->verifyNotify();
		if($verify_result){
			$out_trade_no=$data['extra_common_param'];
			$trade_no=$data['trade_no'];
			$trade_status=$data['trade_status'];
			$status=Order::PAYFAILED;
			//异步通知消息到达后,更新订单状态为已经支付,然后把订单的订单号,订单的其他扩展字段更新到订单表的tradeext字段中
			if($trade_status=='TRADE_FINISHED' || $trade_status=='TRADE_SUCCESS'){
				$status=Order::PAYSUCCESS;
				$order_info=Order::find()->where('orderid=:oid',[':oid'=>$out_trade_no])->one();
				if(!$order_info){
					return false;
				}
				if($order_info->status==Order::CHECKORDER){
					Order::updateAll(['status'=>$status, 'tradeno'=>$trade_no,'tradeext'=>json_encode($data)    ],'orderid=:oid',[':oid'=>$order_info->orderid]);
				}else{
					return false;
				}
			}
			return true;
		}else{
			return false;
		}
			
	}
	
}

支付控制器

PayController.php

namespace app\controllers;
use app\controllers\CommonController;
use app\models\Pay;
use Yii;

class PayController extends CommonController{
	public $enableCsrfValidation=false;
	public function actionNotify(){
		if(Yii::$app->request->isPost){
			$post=Yii::$app->request->post();
			if(Pay::notify($post)){
				echo "success";
				exit;
			}
			echo "fail";
			exit;
		}
	}
	public function actionReturn(){
		$this->layout='layout1';
		$status=Yii::$app->request->get('trade_status');
		if($status=="TRADE_SUCCESS"){
			$s='ok';
		}else{
			$s='no';
		}
		return $this->render("status",['status'=>$s]);
	}
}

异步通知消息转发的php文件
web/notify.php

<?php

$url="http://shop.mr-jason.com/index.php?r=pay/notify";
$post_data=$_POST;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$out=curl_exec($ch);
curl_close($ch);
echo $out;

?>

清空数据库命令
truncate shop_order;

支付宝异步通知优化

在订单表中更新status(支付状态),tradeno(订单号),tradeext(扩展的其他异步通知返回消息)

alter table shop_order add tradeno varchar(100) not null default '' after expressno;
alter table shop_order add tradeext text after tradeno;

订单表结构

create table `shop_order`{
    `orderid` bigint(20) unsigned not null auto_increment,
    `userid` bigint(20) unsigned not null default '0',
    `addressid` bigint(20) unsigned not null default '0', 
    `amount` decimal(10,2) not null default '0.00',
    `status` int(10) unsigned not null default '0',
    `expressid` int(10) unsigned not null default '0',
    `expressno` varchar(50) not null default '',
    `tradeno` varchar(50) not null default '',
    `tradeext` text,
    `createtime` int(10) unsigned not null default '0',
    `updatetime` timestamp not null default current_timestamp on update current_timestamp,
    primary key('orderid'),
    key `shop_order_userid` (`userid`),
    key `shop_order_addressid` (`addressid`),
    key `shop_roder_expressid` (`expressid`),
}engine=InnoDB AUTO_INCREMENT=1 default charset=utr8;

 

异步通知 

支付宝网关=》通知notify.php=>通知payController.php 的actionNotify() 方法 =》请求Model 类中的 Pay::notify($data) 方法

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值