給客戶開發網站時須要用到支付寶在線付款功能,小云到thinkphp網站溜了一圈,代碼是有,但是都不怎麼全,所以這篇文章誕生了!
本篇文章講解了三個類的實現,擔保交易、即時到帳、雙功能收款(該功能支付寶已下線,可給以前已申請過的站長短期內的使用)
類文件在附件類,請自行下載文章主要講解如何使用本類
文件下載後解壓獲得 PayBuy 將本文件夾放到 Think\Library\Vendor 目錄下
php
而後在 config.PHP中配置一下信息thinkphp
- 'ALIPAY_PARTNER' => '8888888888888888',
- 'ALIPAY_SELLER_EMAIL' => 'xxxxxxxxxx@163.com',
- 'ALIPAY_KEY' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
在 function.php 構造付款函數
第一個付款訂單生成函數:
express
- function pay($data){
- $data['expenses'] = isset($data['expenses']) && $data['expenses'] ? $data['expenses'] : "0.00";
- $data['express'] = isset($data['express']) && $data['express'] ? $data['express'] : 'EXPRESS';
- $data['payment'] = isset($data['payment']) && $data['payment'] ? $data['payment'] : 'BUYER_PAY';
- Vendor('PayBuy.AliPay');
- $pay = new AliPay();
- $pay->partner = C('ALIPAY_PARTNER');
- $pay->seller_email = C('ALIPAY_SELLER_EMAIL');
- $pay->paykey = C('ALIPAY_KEY');
- $pay->notify_url = $data['notify_url'];
- $pay->return_url = $data['return_url'];
- $pay->orderid = $data['orderid'];
- $pay->subject = $data['subject'];
- $pay->price = $data['price'];
- $pay->expenses = $data['expenses'];
- $pay->express = $data['express'];
- $pay->payment = $data['payment'];
- $pay->message = $data['message'];
- $pay->show_url = $data['show_url'];
- $pay->receive_name = $data['receive_name'];
- $pay->receive_address = $data['receive_address'];
- $pay->receive_zip = $data['receive_zip'];
- $pay->receive_phone = $data['receive_phone'];
- $pay->receive_mobile = $data['receive_mobile'];
- $pay->waitword = $data['waitword'];
- return $pay->AliPay();
- }
第二個檢查訂單狀態函數:安全
- function checkpayorder($data=''){
- $alipay_config['partner'] = C('ALIPAY_PARTNER');
- $alipay_config['seller_email'] = C('ALIPAY_SELLER_EMAIL');
- $alipay_config['key'] = C('ALIPAY_KEY');
- $alipay_config['sign_type'] = strtoupper('MD5');
- $alipay_config['input_charset']= strtolower('utf-8');
- $alipay_config['transport'] = 'http';
- Vendor('PayBuy.AliPay');
- $alipayNotify = new AlipayNotify($alipay_config);
- $verify_result = $alipayNotify->verifyReturn();
- if($verify_result) {
- $out_trade_no = $data['out_trade_no'];
- $trade_no = $data['trade_no'];
- $trade_status = $data['trade_status'];
- return $data['trade_status'];
- }else {
- return "error";
- }
- }
第三函數集:(這幾個函數是支付寶自帶函數,請直接複製粘貼到 function.php 內)
服務器
- function createLinkstring($para) {
- $arg = "";
- while (list ($key, $val) = each ($para)) {
- $arg.=$key."=".$val."&";
- }
-
- $arg = substr($arg,0,count($arg)-2);
-
-
- if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
-
- return $arg;
- }
-
- function createLinkstringUrlencode($para) {
- $arg = "";
- while (list ($key, $val) = each ($para)) {
- $arg.=$key."=".urlencode($val)."&";
- }
-
- $arg = substr($arg,0,count($arg)-2);
-
-
- if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
-
- return $arg;
- }
-
- function paraFilter($para) {
- $para_filter = array();
- while (list ($key, $val) = each ($para)) {
- if($key == "sign" || $key == "sign_type" || $val == "")continue;
- else $para_filter[$key] = $para[$key];
- }
- return $para_filter;
- }
-
- function argSort($para) {
- ksort($para);
- reset($para);
- return $para;
- }
-
- function logResult($word='') {
- $fp = fopen("log.txt","a");
- flock($fp, LOCK_EX) ;
- fwrite($fp,"執行日期:".strftime("%Y%m%d%H%M%S",time())."\n".$word."\n");
- flock($fp, LOCK_UN);
- fclose($fp);
- }
-
- function getHttpResponsePOST($url, $cacert_url, $para, $input_charset = '') {
-
- if (trim($input_charset) != '') {
- $url = $url."_input_charset=".$input_charset;
- }
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($curl, CURLOPT_CAINFO,$cacert_url);
- curl_setopt($curl, CURLOPT_HEADER, 0 );
- curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl,CURLOPT_POST,true);
- curl_setopt($curl,CURLOPT_POSTFIELDS,$para);
- $responseText = curl_exec($curl);
-
- curl_close($curl);
-
- return $responseText;
- }
-
- function getHttpResponseGET($url,$cacert_url) {
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_HEADER, 0 );
- curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($curl, CURLOPT_CAINFO,$cacert_url);
- $responseText = curl_exec($curl);
-
- curl_close($curl);
-
- return $responseText;
- }
-
- function charsetEncode($input,$_output_charset ,$_input_charset) {
- $output = "";
- if(!isset($_output_charset) )$_output_charset = $_input_charset;
- if($_input_charset == $_output_charset || $input ==null ) {
- $output = $input;
- } elseif (function_exists("mb_convert_encoding")) {
- $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
- } elseif(function_exists("iconv")) {
- $output = iconv($_input_charset,$_output_charset,$input);
- } else die("sorry, you have no libs support for charset change.");
- return $output;
- }
-
- function charsetDecode($input,$_input_charset ,$_output_charset) {
- $output = "";
- if(!isset($_input_charset) )$_input_charset = $_input_charset ;
- if($_input_charset == $_output_charset || $input ==null ) {
- $output = $input;
- } elseif (function_exists("mb_convert_encoding")) {
- $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
- } elseif(function_exists("iconv")) {
- $output = iconv($_input_charset,$_output_charset,$input);
- } else die("sorry, you have no libs support for charset changes.");
- return $output;
- }
- function md5Sign($prestr, $key) {
- $prestr = $prestr . $key;
- return md5($prestr);
- }
-
- function md5Verify($prestr, $sign, $key) {
- $prestr = $prestr . $key;
- $mysgin = md5($prestr);
-
- if($mysgin == $sign) {
- return true;
- }
- else {
- return false;
- }
- }