您还未登录! 登录 | 注册 | 帮助  

您的位置: 首页 > 软件测试工具 > 测试管理工具 > 正文

TP框架集成支付宝,中转页变成gbk编码

发表于:2017-01-09 作者:网络转载 来源:

  tp框架中集成支付宝的功能,将支付宝的demo例子存在到下图位置ExtendVendorAlipay

  生成支付订单
/**
*支付订单
*/
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;
}
支付宝回调接口
/**
*支付宝回调接口
*/
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
  上面错误,删除错误文件开始的空格
  

 相关文章