首先是在支付宝的蚂蚁金服开放平台下载PHP的demo:
1 https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1
下载地址如上,里面有个MD5加密方式UTF8的PHP的demo可以拿来用,可以直接在localhost上跑,传入合适的参数即使体验支付一次,这点和微信支付接口调用的demo相同。
现在我们尝试将其中demo的lib类文件整合到thinkphp之中去。
将四个lib改名放入Vendor文件夹:(文件在我的文件之中),相比官方demo仅仅改动里面的PHP文件include
点击支付并传入订单ID:
1 public function pay_request() 2 { 3 $id = intval($_GET[‘order_id‘]);//传入订单ID 4 if($id){ 5 $o = D(‘order_info‘); 6 $order_info = $o->where(‘order_id = %d‘,$id)->find();//查询出所有订单信息 7 8 if(empty($order_info[‘paycode‘])){ 9 $order_info[‘paycode‘] = ‘Alipay‘; 10 } 11 $class = ‘Com\\Pay\\‘.$order_info[‘paycode‘]; 12 // import($path); 13 $pay = new $class;//实例化支付宝类 14 15 $pay->getCode($order_info); 16 } 17 }
类文件:
Alipay.class.php:
1 ‘YUMING‘ => ‘https://www.homewide.cn‘,
1 <?php 2 namespace Com\Pay; 3 4 class Alipay 5 { 6 7 private $config; 8 9 public function __construct($config = null) 10 { 11 if($config){ 12 $this->config = unserialize($config); 13 }else{ 14 $this->config = array( 15 ‘pid‘=>‘**卖家申请成功后得到的PID****‘, 16 ‘key‘=>‘**卖家申请成功后获取的key**‘, 17 ‘seller_email‘=>‘***卖家支付宝账号**‘, 18 ‘notify_url‘=>(C(‘YUMING‘).U(‘Home/Login/pay_respone‘)),//C(‘yunyin’)这个常量是网址www.haveadream.win 19 ‘return_url‘=>(C(‘YUMING‘).U(‘Home/User/my_order‘)), 20 ); 21 } 22 //这里我们通过TP的C函数把配置项参数读出,赋给$alipay_config; 23 $this->config[‘alipay_config‘] = array( 24 ‘partner‘ =>$this->config[‘pid‘], //这里是你在成功申请支付宝接口后获取到的PID; 25 ‘key‘=>$this->config[‘key‘],//这里是你在成功申请支付宝接口后获取到的Key 26 ‘sign_type‘=>strtoupper(‘MD5‘), 27 ‘input_charset‘=> strtolower(‘utf-8‘), 28 // ‘cacert‘=> getcwd().‘\\domain.crt‘, 29 ‘cacert‘=> getcwd().‘\\cacert.pem‘, 30 ‘transport‘=> ‘http‘, 31 ); 32 33 /** 34 * 引入支付宝 35 */ 36 vendor(‘Alipay.Corefunction‘); 37 vendor(‘Alipay.Md5function‘); 38 vendor(‘Alipay.Notify‘); 39 vendor(‘Alipay.Submit‘); 40 41 } 42 43 /*生成支付按钮*/ 44 public function getCode($order_info)//跑这个方法的时候就已经在construct函数里面将config数据传递进去了 45 { 46 /********************************************************* 47 把alipayapi.php中复制过来的如下两段代码去掉, 48 第一段是引入配置项, 49 第二段是引入submit.class.php这个类。 50 为什么要去掉?? 51 第一,配置项的内容已经在项目的Config.php文件中进行了配置,我们只需用C函数进行调用即可; 52 第二,这里调用的submit.class.php类库我们已经在PayAction的_initialize()中已经引入;所以这里不再需要; 53 *****************************************************/ 54 55 /**************************请求参数**************************/ 56 $payment_type = "1"; //支付类型 //必填,不能修改 57 $notify_url = $this->config[‘notify_url‘]; //服务器异步通知页面路径 58 $return_url = $this->config[‘return_url‘]; //页面跳转同步通知页面路径 59 $seller_email = $this->config[‘seller_email‘];//卖家支付宝帐户必填 60 $out_trade_no = $order_info[‘order_sn‘];//商户订单号 通过支付页面的表单进行传递,注意要唯一! 61 $subject = ‘家安采购订单‘; //订单名称 //必填 通过支付页面的表单进行传递 62 $total_fee = $order_info[‘pay_money‘]; //付款金额 //必填 通过支付页面的表单进行传递 63 //$total_fee = 0.01; //付款金额 //必填 通过支付页面的表单进行传递 64 $body = ‘采购上家安‘; //订单描述 通过支付页面的表单进行传递 65 $show_url = ‘‘; //商品展示地址 通过支付页面的表单进行传递 66 $anti_phishing_key = "";//防钓鱼时间戳 //若要使用请调用类文件submit中的query_timestamp函数 67 $exter_invoke_ip = get_client_ip(); //客户端的IP地址,这个方法是ThinkPHP之中自带的方法的 68 69 //商品数量 70 $quantity = "1"; 71 //必填,建议默认为1,不改变值,把一次交易看成是一次下订单而非购买一件商品 72 //物流费用 73 $logistics_fee = "0.00"; 74 //必填,即运费 75 //物流类型 76 $logistics_type = "EXPRESS"; 77 //必填,三个值可选:EXPRESS(快递)、POST(平邮)、EMS(EMS) 78 //物流支付方式 79 $logistics_payment = "SELLER_PAY"; 80 //必填,两个值可选:SELLER_PAY(卖家承担运费)、BUYER_PAY(买家承担运费) 81 //订单描述 82 //收货人姓名 83 $receive_name = $order_info[‘consignee‘]; 84 //如:张三 85 //收货人地址 86 $receive_address = ‘XX省XXX市XXX区XXX路XXX小区XXX栋XXX单元XXX号‘; 87 //如:XX省XXX市XXX区XXX路XXX小区XXX栋XXX单元XXX号 88 //收货人邮编 89 $receive_zip = ‘123456‘; 90 //如:123456 91 92 //收货人电话号码 93 $receive_phone = ‘0000-0000000‘; 94 //如:0571-88158090 95 96 //收货人手机号码 97 $receive_mobile = ‘13***21‘; 98 //如:13312341234 99 100 101 /************************************************************/ 102 103 //构造要请求的参数数组,无需改动 104 $parameter = array( 105 "service" => "create_direct_pay_by_user", 106 "partner" => trim($this->config[‘pid‘]), 107 "payment_type" => $payment_type, 108 "notify_url" => $notify_url, 109 "return_url" => $return_url, 110 "seller_email" => $seller_email, 111 "out_trade_no" => $out_trade_no, 112 "subject" => $subject, 113 "price" => $total_fee, 114 "quantity" => $quantity, 115 "logistics_fee" => $logistics_fee, 116 "logistics_type" => $logistics_type, 117 "logistics_payment" => $logistics_payment, 118 "body" => $body, 119 "show_url" => $show_url, 120 "receive_name" => $receive_name, 121 "receive_address" => $receive_address, 122 "receive_zip" => $receive_zip, 123 "receive_phone" => $receive_phone, 124 "receive_mobile" => $receive_mobile, 125 "_input_charset" => trim(strtolower($this->config[‘alipay_config‘][‘input_charset‘])) 126 ); 127 // var_dump($parameter);die; 128 129 // var_dump($parameter);die; 130 131 132 //建立请求 133 $alipaySubmit = new \AlipaySubmit($this->config[‘alipay_config‘]); 134 $html_text = $alipaySubmit->buildRequestForm($parameter,"post", ""); 135 echo $html_text;die; 136 } 137 138 public function respone() 139 { 140 // file_put_contents(‘./pay.txt‘,var_export($_REQUEST,true)); 141 /* 142 同理去掉以下两句代码; 143 */ 144 //require_once("alipay.config.php"); 145 //require_once("lib/alipay_notify.class.php"); 146 //计算得出通知验证结果 147 $alipayNotify = new \AlipayNotify($this->config[‘alipay_config‘]); 148 $verify_result = $alipayNotify->verifyNotify(); 149 // if(!$verify_result){ 150 // file_put_contents(‘./pay.txt‘,‘验证失败‘,FILE_APPEND); 151 // }else{ 152 // file_put_contents(‘./pay.txt‘,‘验证成功!‘,FILE_APPEND); 153 // } 154 155 if($verify_result) { 156 // file_put_contents(‘./pay.txt‘,‘进来了‘,FILE_APPEND); 157 //验证成功 158 //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 159 $out_trade_no = $_POST[‘out_trade_no‘]; //商户订单号 160 $trade_no = $_POST[‘trade_no‘]; //支付宝交易号 161 $trade_status = $_POST[‘trade_status‘]; //交易状态 162 $total_fee = $_POST[‘total_fee‘]; //交易金额 163 $notify_id = $_POST[‘notify_id‘]; //通知校验ID。 164 $notify_time = $_POST[‘notify_time‘]; //通知的发送时间。格式为yyyy-MM-dd HH:mm:ss。 165 $buyer_email = $_POST[‘buyer_email‘]; //买家支付宝帐号; 166 $parameter = array( 167 "out_trade_no" => $out_trade_no, //商户订单编号; 168 "trade_no" => $trade_no, //支付宝交易号; 169 "total_fee" => $total_fee, //交易金额; 170 "trade_status" => $trade_status, //交易状态 171 "notify_id" => $notify_id, //通知校验ID。 172 "notify_time" => $notify_time, //通知的发送时间。 173 "buyer_email" => $buyer_email, //买家支付宝帐号; 174 ); 175 if($_POST[‘trade_status‘] == ‘TRADE_FINISHED‘) { 176 177 }else if ($_POST[‘trade_status‘] == ‘TRADE_SUCCESS‘) { 178 179 if(!check_order_status($out_trade_no)){ 180 // file_put_contents(‘./pay.txt‘,‘1进来了‘,FILE_APPEND); 181 $data = array( 182 ‘order_sn‘=>$out_trade_no, 183 ‘des‘=>(‘订单交易:‘.$out_trade_no), 184 ‘money‘=>$total_fee, 185 ); 186 orderhandle($data); 187 //进行订单处理,并传送从支付宝返回的参数; 188 } 189 } 190 echo "success"; //请不要修改或删除 191 }else { 192 //验证失败 193 echo "fail"; 194 // file_put_contents(‘./pay.txt‘,var_export($verify_result,true),FILE_APPEND); 195 // file_put_contents(‘./pay.txt‘,var_export($this->config,true),FILE_APPEND); 196 file_put_contents(‘./pay.txt‘,‘验证失败‘,FILE_APPEND); 197 } 198 } 199 200 } 201 ?>
U(‘Home/Login/pay_respone‘)://服务器异步通知页面路径
1 public function pay_respone() 2 { 3 // file_put_contents(‘./pay.txt‘,var_export($_REQUEST,true)); 4 if(IS_POST){ 5 $o = D(‘order_info‘); 6 $order_info = $o->where(‘order_sn = "%s"‘,$_POST[‘out_trade_no‘])->find(); 7 if(empty($order_info[‘paycode‘])){ 8 $order_info[‘paycode‘] = ‘Alipay‘; 9 } 10 $class = ‘Com\\Pay\\‘.$order_info[‘paycode‘]; 11 // import($path); 12 $pay = new $class; 13 $pay->respone(); 14 } 15 }
U(‘Home/User/my_order‘)://页面跳转同步通知页面路径
public function my_order(){ /* 取出左边分类栏目start */ $c = D(‘category‘); $left_menu = $c->get_category(); $this->assign(‘category_menu‘,$left_menu); /* 取出左边分类栏目end */ //取出该采购商所有订单信息 $o = D(‘order_info‘); $list = $o->getList(); $page = $o->getPage(); $countNum = $o->getCount(); // var_dump($res); $this->assign(‘list‘,$list); $this->assign(‘page‘,$page); $this->assign(‘countNum‘,$countNum); //获取总金额 $this->assign(‘countMoney‘,sprintf(‘%.2f‘,$o->get_order_count_num())); $other = array( ‘is_submit‘=>$o->get_order_num(‘is_submit‘), ‘is_send‘=>$o->get_order_num(‘is_send‘), ‘is_ok‘=>$o->get_order_num(‘is_ok‘), ‘is_cancel‘=>$o->get_order_num(‘is_cancel‘), ); $this->assign($other); $this->display(); }
上面就是所有的alipay的接口调用,以及回调数据处理了
更详细参考文档:
1 http://www.thinkphp.cn/code/240.html
时间: 2024-08-07 21:16:28