ThinkPHP调用连连支付
生活随笔
收集整理的這篇文章主要介紹了
ThinkPHP调用连连支付
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.配置環境
ip域名申請
?ip域名審核
?ip域名填寫
?
?安全中心
?商戶公鑰維護
?
2.編寫代碼
*注意,一定要驗簽
*注意,一定要驗簽
文章中有的地方沒有驗簽,但是自己根據需要一定要加上(安全第一)
<?php namespace Service\Controller;use Base\Webbase;/***漢字轉變為utf8編碼*/ function chinesetoutf8($a=null){$a = iconv("gb2312","utf-8",$a);return $a; } /** 驗證 webhooks 簽名方法*/ function verify_signature($raw_data, $signature, $pub_key_path) {$pub_key_contents = file_get_contents($pub_key_path);// php 5.4.8 以上,第四個參數可用常量 OPENSSL_ALGO_SHA256return openssl_verify($raw_data, base64_decode($signature), $pub_key_contents, 'sha256'); }class PayController extends Webbase {public function _initialize(){parent::_initialize();Vendor('Llpay.lib.llpay_core');Vendor('Llpay.lib.llpay_md5');Vendor('Llpay.lib.llpay_rsa');Vendor('Llpay.lib.llpay_cls_json');Vendor('Llpay.lib.llpay_notify');Vendor('Llpay.lib.llpay_submit');Vendor('Llpay.lib.tixian.llpay_security');Vendor('Llpay.lib.tixian.llpay_apipost_submit');}public function dollpay($data){//商戶用戶唯一編號$user_id = $data['uid'];$bank = M('bankcard')->where(array('uid'=>$this->uid))->find();$userinfo = M('user_info')->where(array('uid'=>$this->uid))->find();//支付類型 虛擬商品:101001 實物商品:109001 賬戶充值:108001//$busi_partner = $data['busi_partner'];$busi_partner = 101001;//商戶訂單號$no_order = $data['order_id']; //商戶網站訂單系統中唯一訂單號,必填//付款金額$money_order = $data['amount']/100;//商品名稱$name_goods = $data['name_goods'];//訂單描述$info_order = $data['info_order'];/**********************************************///卡號$card_no = $bank['card_number'];//姓名$acct_name = $userinfo['name'];//身份證號$id_no = $userinfo['idcard'];//協議號$no_agree = $data['no_agree'];/*************************************************///風險控制參數 // $risk_item = $data['risk_item'];// $risk_item = '{\"user_info_bind_phone\":\"17358695911\",\"user_info_dt_register\":\"2013100122130\",\"risk_state\":\"1\",\"frms_ware_category\":\"1019\"}';$user = M('user')->where(array('uid'=>$user_id))->find();$userinfo = M('user_info')->where(array('uid'=>$user_id))->find();$risk_item = '{\"frms_ware_category\":\"1018\",\"user_info_mercht_userno\":\"2018109002237008\",\"user_info_bind_phone\":\"'.$user['mobile'].'\",\"user_info_dt_register\":\"'.date('YmdHis', $user['reg_time']).'\",\"user_info_full_name\":\"'.$userinfo['name'].'\",\"user_info_id_no\":\"'.$userinfo['idcard'].'\",\"user_info_identify_state\":\"1\",\"user_info_identify_type\":\"3\",}';//訂單有效期 分鐘為單位,默認為10080分鐘(7天)$valid_order = 10080;//服務器異步通知頁面路徑$notify_url = "http://www.***.com/index.php/**/**/notifyurl";//需http://格式的完整路徑,不能加?id=123這類自定義參數//頁面跳轉同步通知頁面路徑$return_url = "http://www.***.com/index.php/**/**/returnurl";//需http://格式的完整路徑,不能加?id=123這類自定義參數,不能寫成http://localhost//************************************************************///構造要請求的參數數組,無需改動$parameter = array ("oid_partner" => C('llpay_config.oid_partner'),"app_request" => C('llpay_config.app_request'),"sign_type" => C('llpay_config.sign_type'),"valid_order" => C('llpay_config.valid_order'),"user_id" => $user_id,"busi_partner" => $busi_partner,"no_order" => $no_order,"dt_order" => local_date('YmdHis', time()),//"dt_order" =>time(),"name_goods" => $name_goods,"info_order" => $info_order,"money_order" => $money_order,"notify_url" => $notify_url,"url_return" => $return_url,"card_no" => $card_no,"acct_name" => $acct_name,"id_no" => $id_no,"no_agree" => $no_agree,"risk_item" => $risk_item,"valid_order" => $valid_order);$llpay_config = C('llpay_config');//建立請求$llpaySubmit = new \LLpaySubmit($llpay_config);$html_text = $llpaySubmit->buildRequestForm($parameter, "post", "確認");echo $html_text;}/******************************服務器異步通知頁面方法其實這里就是將notify_url.php文件中的代碼復制過來進行處理*******************************/public function notifyurl(){$llpay_config = C('llpay_config');//計算得出通知驗證結果$llpayNotify = new \LLpayNotify($llpay_config);$llpayNotify->verifyNotify();if ($llpayNotify->result) { //驗證成功//獲取連連支付的通知返回參數,可參考技術文檔中服務器異步通知參數列表$no_order = $llpayNotify->notifyResp['no_order'];//商戶訂單號$oid_paybill = $llpayNotify->notifyResp['oid_paybill'];//連連支付單號$result_pay = $llpayNotify->notifyResp['result_pay'];//支付結果,SUCCESS:為支付成功$money_order = $llpayNotify->notifyResp['money_order'];// 支付金額if($result_pay == "SUCCESS"){//請在這里加上商戶的業務邏輯程序代(更新訂單狀態、入賬業務)//——請根據您的業務邏輯來編寫程序——if($result_pay == "SUCCESS"){//請在這里加上商戶的業務邏輯程序代(更新訂單狀態、入賬業務)//——請根據您的業務邏輯來編寫程序——//payAfter($llpayNotify->notifyResp);}//payAfter($llpayNotify->notifyResp);}file_put_contents("log.txt", "異步通知 驗證成功\n", FILE_APPEND);die("{'ret_code':'0000','ret_msg':'交易成功'}"); //請不要修改或刪除/} else {file_put_contents("log.txt", "異步通知 驗證失敗\n", FILE_APPEND);//驗證失敗die("{'ret_code':'9999','ret_msg':'驗簽失敗'}");//調試用,寫文本函數記錄程序運行情況是否正常//logResult("這里寫入想要調試的代碼變量值,或其他運行的結果記錄");file_put_contents("log.txt", "異步通知 驗證失敗1\n", FILE_APPEND);}}/*頁面跳轉處理方法;這里其實就是將return_url.php這個文件中的代碼復制過來,進行處理;*/public function returnurl(){$llpay_config = C('llpay_config');//計算得出通知驗證結果$llpayNotify = new \LLpayNotify($llpay_config);$verify_result = $llpayNotify->verifyReturn();if($verify_result) {//驗證成功///請在這里加上商戶的業務邏輯程序代碼//——請根據您的業務邏輯來編寫程序(以下代碼僅作參考)——//獲取連連支付的通知返回參數,可參考技術文檔中頁面跳轉同步通知參數列表$json = new \JSON;$res_data = $_POST["res_data"];//商戶編號$oid_partner = $json->decode($res_data)-> {'oid_partner' };//商戶訂單號$no_order = $json->decode($res_data)-> {'no_order' };//支付結果$result_pay = $json->decode($res_data)-> {'result_pay' };if($result_pay == 'SUCCESS') {header("location:http://www.yixianglife.com/yxlife_new/wx_web/index.html#/my") ;//判斷該筆訂單是否在商戶網站中已經做過處理//如果沒有做過處理,根據訂單號(no_order)在商戶網站的訂單系統中查到該筆訂單的詳細,并執行商戶的業務程序//如果有做過處理,不執行商戶的業務程序}else {echo "result_pay=".$result_pay;}echo "驗證成功<br />";//——請根據您的業務邏輯來編寫程序(以上代碼僅作參考)——/}else {//驗證失敗//如要調試,請看llpay_notify.php頁面的verifyReturn函數echo "驗證失敗";}header("location:http://www.**.com") ;}/*** 付款接口* @param $data*/public function payment($data){//商戶付款流水號$no_order = $data['orderid'];//商戶時間$dt_order = date("YmdHis",time());//金額$money_order = $data['amount']/100;//收款人姓名 $acct_name= $data['acct_name'];//銀行賬號$card_no = $data['card_number'];//訂單描述$info_order = $data['info'];//對私標記$flag_card = '0';//服務器異步通知地址$notify_url = 'http://www.**.com/index.php/**/**/tx_notifyurl';//平臺來源$platform = '**.com';//版本號$api_version = '1.0';//實時付款交易接口地址$llpay_payment_url = 'https://instantpay.lianlianpay.com/paymentapi/payment.htm';//需http://格式的完整路徑,不能加?id=123這類自定義參數/************************************************************/$llpay_config = C('llpay_tx_config');//構造要請求的參數數組,無需改動$parameter = array ("oid_partner" => trim($llpay_config['oid_partner']),"sign_type" => trim($llpay_config['sign_type']),"no_order" => $no_order,"dt_order" => $dt_order,"money_order" => $money_order,"acct_name" => $acct_name,"card_no" => $card_no,"info_order" => $info_order,"flag_card" => $flag_card,"notify_url" => $notify_url,"platform" => $platform,"api_version" => $api_version);//建立請求$llpaySubmit = new \LLpaytxSubmit($llpay_config);//對參數排序加簽名$sortPara = $llpaySubmit->buildRequestPara($parameter);//傳json字符串$json = json_encode($sortPara);$parameterRequest = array ("oid_partner" => trim($llpay_config['oid_partner']),"pay_load" => ll_encrypt($json,$llpay_config['LIANLIAN_PUBLICK_KEY']) //請求參數加密);$html_text = $llpaySubmit->buildRequestJSON($parameterRequest,$llpay_payment_url); //調用付款申請接口,同步返回0000,是指創建連連支付單成功,訂單處于付款處理中狀態,最終的付款狀態由異步通知告知 //出現1002,2005,4006,4007,4009,9999這6個返回碼時或者沒返回碼, //拋exception(或者對除了0000之后的code都查詢一遍查詢接口)調用付款結果查詢接口,明確訂單狀態,不能私自設置訂單為失敗狀態,以免造成這筆訂單在連連付款成功了, //而商戶設置為失敗,用戶重新發起付款請求,造成重復付款,商戶資金損失//對連連響應報文內容需要用連連公鑰驗簽$arr = json_decode($html_text,true);return $arr; /*********************************************************************************//*if ($res['ret_code'] =="0000"){ $this->websuccess(self::OUTPUT_SUCCESS,'提現成功'); }else{ file_put_contents("./tx.log",json_encode($res));$this->weberror(self::THIS_ACTION_ERROR,'提現失敗'.$res['ret_msg']);} /*********************************************************************************/}/*** 付款結果查詢* @param $data*/public function queryPayment($data) {$data = $_POST; $url ="https://instantpay.lianlianpay.com/paymentapi/queryPayment.htm";$parameterRequest = array ('oid_partner' =>$data['oid_partner'],'sign'=>$data['sign'],'sign_type'=>$data['sign_type'],'no_order'=>$data['no_order'],'api_version'=>'1.0');$result = getHttpResponseJSON($url,$parameterRequest);//$result = json_decode($result);var_dump($result);if ($result['result_pay'] =='success'){ return true;}else{return false; }}}?
總結
以上是生活随笔為你收集整理的ThinkPHP调用连连支付的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入理解计算机操作系统(一)
- 下一篇: 面向对象的PHP初学者