tp框架中集成支付宝的功能,将支付宝的demo例子存在到下图位置\Extend\Vendor\Alipay
生成支付订单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
/** * 支付订单 */ public
function pay() { 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
= "订单不存在" ; } else
if ( $order [ ‘PaymentStatus‘ ] == 1){ $error
= "此订单已经完成,无需再次支付!" ; } else
if ( $order [ ‘PaymentStatus‘ ] == 2){ $error
= "此订单已经取消,无法支付,请重新下单!" ; } if ( $error
!= "" ){ $this ->_FAIL( "系统错误" , $error , $this ->getErrorLinks()); return
; } $payType
= I( ‘post.payType‘ , ‘‘ , ‘htmlspecialchars‘ ); #支付宝 if ( $payType
== ‘alipay‘ ){ $this ->payWithAlipay( $order ); } } |
支付订单提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 |
/** * 以支付宝形式支付 * @param unknown_type $order */ private
function payWithAlipay( $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
= new AlipaySubmit( $alipay_config ); $html_text
= $alipaySubmit ->buildRequestForm( $parameter , "get" , "去支付" ); echo
$html_text ; } |
支付宝回调接口
<?php /** * 支付宝回调接口 */ class AlipayAction extends Action { /** * 支付宝异步通知 */ public function notifyOnAlipay(){ 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 = new AlipayNotify($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 = "notify from Alipay, trade_status=".$trade_status." alipay sign=".$_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‘] = "notify from Alipay, 但是验证不通过,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");
如果还有乱码查看日志信息 是否出现
1 |
NOTIC: [2] Cannot modify header information - headers already sent by (output started at |
上面错误,删除错误文件开始的空格
1 |
<em id= "__mceDel" > </em> |
tp框架集成支付宝,中转页变成gbk编码,布布扣,bubuko.com
原文:http://www.cnblogs.com/klj123wan/p/3572131.html