/** *以支付寶形式支付 *@paramunknown_type$order */ privatefunctionpayWithAlipay($order){ //引入支付寶相關(guān)的文件 require_once(VENDOR_PATH."Alipay/alipay.config.php"); require_once(VENDOR_PATH."Alipay/lib/alipay_submit.class.php"); //支付類型 $payment_type="1"; //必填,不能修改 //服務(wù)器異步通知頁面路徑 $notify_url=C("HOST")."index.php/Alipay/notifyOnAlipay"; //頁面跳轉(zhuǎn)同步通知頁面路徑 $return_url=C("HOST")."index.php/Pay/ok"; //賣家支付寶帳戶 $seller_email=$alipay_config['seller_email']; //必填 //商戶訂單號,從訂單對象中獲取 $out_trade_no=$order['OrderNum']; //商戶網(wǎng)站訂單系統(tǒng)中唯一訂單號,必填 //訂單名稱 $subject="物流服務(wù)"; //必填 //付款金額 #正常金額 $price=$order['Price']; #測試金額 #$price=0.1; //必填 $body=$subject; //商品展示地址 $show_url=C('HOST'); //構(gòu)造要請求的參數(shù)數(shù)組,無需改動 $parameter=array( "service"=>"create_direct_pay_by_user", "partner"=>trim($alipay_config['partner']), "payment_type"=>$payment_type, "notify_url"=>$notify_url, "return_url"=>$return_url, "seller_email"=>$seller_email, "out_trade_no"=>$out_trade_no, "subject"=>$subject, "total_fee"=>$price, "body"=>$body, "show_url"=>$show_url, "_input_charset"=>trim(strtolower($alipay_config['input_charset'])) ); Log::write('支付寶訂單參數(shù):'.var_export($parameter,true),Log::DEBUG); //建立請求 $alipaySubmit=newAlipaySubmit($alipay_config); $html_text=$alipaySubmit->buildRequestForm($parameter,"get","去支付"); echo$html_text; } 支付寶回調(diào)接口 <?php /** *支付寶回調(diào)接口 */ classAlipayActionextendsAction{ /** *支付寶異步通知 */ publicfunctionnotifyOnAlipay(){ Log::write("notify:".print_r($_REQUEST,true),Log::DEBUG); require_once(VENDOR_PATH."Alipay/alipay.config.php"); require_once(VENDOR_PATH."Alipay/lib/alipay_notify.class.php"); $orderLogDao=M('orderlog'); //計算得出通知驗證結(jié)果 $alipayNotify=newAlipayNotify($alipay_config); $verify_result=$alipayNotify->verifyNotify(); Log::write('verify_result:'.var_export($verify_result,true),Log::DEBUG); if($verify_result){//驗證成功 //商戶訂單號 $out_trade_no=$_POST['out_trade_no']; //支付寶交易號 $trade_no=$_POST['trade_no']; //根據(jù)訂單號獲取訂單 $DAO=M('order'); $order=$DAO->where("OrderNum='".$out_trade_no."'")->find(); //如果訂單不存在,設(shè)置為0 if(!isset($order)){ $orderId=0; } else{ $orderId=$order['id']; } //交易狀態(tài) $trade_status=$_POST['trade_status']; $log="notifyfromAlipay,trade_status=".$trade_status."alipaysign=".$_POST['sign'].'price='.$_POST['total_fee']; $orderLog['o_id']=$orderId; if($_POST['trade_status']=='TRADE_FINISHED'||$_POST['trade_status']=='TRADE_SUCCESS'){ #修改訂單狀態(tài) if((float)$order['Price']!=(float)$_POST['total_fee']){ $data['PaymentStatus']='2'; }else{ $data['PaymentStatus']='1'; } $DAO->where('id='.$orderId)->save($data); } $orderLog['pay_id']=$trade_no; $orderLog['pay_log']=$log; $orderLog['pay_type']='alipay'; $orderLog['pay_result']='success'; $orderLogDao->add($orderLog); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// echo"success";//返回成功標(biāo)記給支付寶 } else{ //驗證不通過時,也記錄下來 $orderLog['pay_log']="notifyfromAlipay,但是驗證不通過,sign=".$_POST['sign']; $orderLog['o_id']=-1; $orderLog['pay_type']='alipay'; $orderLog['pay_result']='fail'; $orderLogDao->add($orderLog); //驗證失敗 echo"fail"; } } } ?> |