###发起支付
public function _initialize(){Vendor('Alipay.wappay.service.AlipayTradeService');Vendor('Alipay.wappay.buildermodel.AlipayTradeWapPayContentBuilder');}//商城public function pay(){$config = config('Alipay');$order_id = intval(input("param.order_id/d",0));if(empty($order_id)){$this->error("订单有误");}$order = Db::name('order')->where(array('id'=>$order_id))->find();if(empty($order)){$this->error("订单有误");}//商户订单号,商户网站订单系统中唯一订单号,必填$out_trade_no = $order['order_sn'];//订单名称,必填// $subject = rtrim($order['good_name'],'/');//商品名称过长导致无法跳转支付宝$subject = "商城购物";//付款金额,必填$total_amount = $order['total_price'];// $total_amount = 0.01;$body = "";//构造参数$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();$payRequestBuilder->setBody($body);$payRequestBuilder->setSubject($subject);$payRequestBuilder->setTotalAmount($total_amount);$payRequestBuilder->setOutTradeNo($out_trade_no);$config['notify_url'] = "/index/Alipay/xiaofei_notify_url";$aop = new \AlipayTradeService($config);$response = $aop->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);}
###回调处理,异步跳转
public function xiaofei_notify_url(){$config = config('Alipay');$config['notify_url'] = "/index/Alipay/xiaofei_notify_url";$arr=$_POST;$alipaySevice = new \AlipayTradeService($config);$alipaySevice->writeLog(var_export($_POST,true));$result = $alipaySevice->check($arr);/* 实际验证过程建议商户添加以下校验。1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额)*/if($result) {//验证成功//请在这里加上商户的业务逻辑程序代//获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表//商户订单号$arr = input('post.');$map['out_trade_no'] = $arr['out_trade_no'];//支付宝交易号$map['transaction_id'] = trim($arr['trade_no']);//交易状态$trade_status = $arr['trade_status'];if($arr['trade_status'] == 'TRADE_FINISHED'|| $trade_status == 'TRADE_SUCCESS') {//判断该笔订单是否在商户网站中已经做过处理//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序//请务必判断请求时的total_amount与通知时获取的total_fee为一致的//如果有做过处理,不执行商户的业务程序$list = Db::name("order")->where(array("order_sn"=>$map['out_trade_no']))->update(array("pay_time"=>time(),"status"=>10));if($list){echo "success";//请不要修改或删除}}}else {//验证失败echo "fail";}}
如有不足,欢迎评论指导,转载请注明来源!