TP框架集成支付寶,中轉頁變成gbk編碼
tp框架中集成支付寶的功能,將支付寶的demo例子存在到下圖位置\Extend\Vendor\Alipay
生成支付訂單
/** *支付訂單 */ publicfunctionpay(){ header("Content-Type:text/html;charset=utf-8"); $id=I('post.oid','','htmlspecialchars'); $DAO=M('order'); $order=$DAO->where("id=".$id)->find(); $error=""; if(!isset($order)){ $error="訂單不存在"; }elseif($order['PaymentStatus']==1){ $error="此訂單已經完成,無需再次支付!"; }elseif($order['PaymentStatus']==2){ $error="此訂單已經取消,無法支付,請重新下單!"; } if($error!=""){ $this->_FAIL("系統錯誤",$error,$this->getErrorLinks()); return; } $payType=I('post.payType','','htmlspecialchars'); #支付寶 if($payType=='alipay'){ $this->payWithAlipay($order); } } |
支付訂單提交
/** *以支付寶形式支付 *@paramunknown_type$order */ privatefunctionpayWithAlipay($order){ //引入支付寶相關的文件 require_once(VENDOR_PATH."Alipay/alipay.config.php"); require_once(VENDOR_PATH."Alipay/lib/alipay_submit.class.php"); //支付類型 $payment_type="1"; //必填,不能修改 //服務器異步通知頁面路徑 $notify_url=C("HOST")."index.php/Alipay/notifyOnAlipay"; //頁面跳轉同步通知頁面路徑 $return_url=C("HOST")."index.php/Pay/ok"; //賣家支付寶帳戶 $seller_email=$alipay_config['seller_email']; //必填 //商戶訂單號,從訂單對象中獲取 $out_trade_no=$order['OrderNum']; //商戶網站訂單系統中唯一訂單號,必填 //訂單名稱 $subject="物流服務"; //必填 //付款金額 #正常金額 $price=$order['Price']; #測試金額 #$price=0.1; //必填 $body=$subject; //商品展示地址 $show_url=C('HOST'); //構造要請求的參數數組,無需改動 $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('支付寶訂單參數:'.var_export($parameter,true),Log::DEBUG); //建立請求 $alipaySubmit=newAlipaySubmit($alipay_config); $html_text=$alipaySubmit->buildRequestForm($parameter,"get","去支付"); echo$html_text; } 支付寶回調接口 <?php /** *支付寶回調接口 */ 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'); //計算得出通知驗證結果 $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']; //根據訂單號獲取訂單 $DAO=M('order'); $order=$DAO->where("OrderNum='".$out_trade_no."'")->find(); //如果訂單不存在,設置為0 if(!isset($order)){ $orderId=0; } else{ $orderId=$order['id']; } //交易狀態 $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'){ #修改訂單狀態 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";//返回成功標記給支付寶 } 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"; } } } ?> |
今天在tp框架中集成支付寶功能,跳轉支付寶的時候出現亂碼錯誤。
需要設定header("Content-Type:text/html;charset=utf-8");
如果還有亂碼查看日志信息是否出現
NOTIC:[2]Cannotmodifyheaderinformation-headersalreadysentby(outputstartedat
上面錯誤,刪除錯誤文件開始的空格
<emid="__mceDel"></em>
posted on 2014-11-21 10:50 順其自然EVO 閱讀(518) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄