网站首页 javascript技术
QUICK支付接口示例
发布时间:2019-04-30 11:14查看次数:2702
<?php namespace App\Http\Controllers; use App\Model\LogOrderModel; use App\Mysql\Yixu_logorder; use Illuminate\Http\Request; class QuickController extends Controller { private $key = "*****"; private $root_url = "http://180.******/"; private $md5_key = "****"; private $_yixu_pay_kay ="*****"; /** 翼讯支付 * @param Request $req */ public function yixupay(Request $req){ $_url = $req->all(); $appid = $_url["appid"]; $userid = $_url["userid"]; $orderid = $_url["orderid"]; $price = $_url["price"]; $payresult = $_url["payresult"]; $ext = $_url["ext"]; $sign = $_url["sign"]; $_md5Sign = "appid=".$appid."&ext=".$ext."&orderid=".$orderid."&payresult=".$payresult."&price=".$price."&userid=".$userid."&".$this->_yixu_pay_kay; $_md5Sign = md5($_md5Sign); // 订单验证通过 if ($sign == $_md5Sign && $payresult == "0"){ $_yixu_order = new Yixu_logorder(); $_yixu_order->appid = $appid; $_yixu_order->userid = $userid; $_yixu_order->orderid = $orderid; $_yixu_order->price = $price; $_yixu_order->payresult = $payresult; $_yixu_order->ext = $ext; $_yixu_order->sign = $sign; if($_yixu_order->save()){ $qudaoOrderId = $_yixu_order->id; $_tempYrl = $this->root_url.$ext."/pay?" ."ownOrder=".$orderid."&goodsOrder=".$qudaoOrderId."&amount=".$price; $this->send_to_game($_tempYrl); return "success"; }else{ return "log error"; } }else{ return "order sign error"; } } // quick 支付 public function pay(Request $request){ $input = $request->input(); $nt_data = $input["nt_data"]; $sign = $input["sign"]; $md5Sign = $input["md5Sign"]; $params = []; $params ["nt_data"] = $nt_data; $params ["sign"] = $sign; file_put_contents(app_path()."/order.txt",$nt_data."_".$sign."_".$md5Sign); // 第一步获取签名 $server_md5 = self::getSign($params,$this->md5_key); echo $server_md5; if($server_md5 == $md5Sign){ //第二步解密加密数据 $_xmlData = $this->decode($params["nt_data"],$this->key); //XML成数组 $xml = simplexml_load_string($_xmlData); $xmljson= json_encode($xml); $xml = json_decode($xmljson,true); // 提取出来消息实体 $xml = $xml["message"]; $log_order = new LogOrderModel(); $log_order->order_no = $xml["order_no"]; $log_order->game_order =$xml["game_order"]; $log_order->amount =$xml["amount"]; $log_order->channel =$xml["channel"]; $log_order->channel_uid =$xml["channel_uid"]; $log_order->ext =$xml["extras_params"]; $log_order->status =$xml["status"]; $log_order->save(); // 拼接订单字符串 $_tempYrl = $this->root_url.$xml["extras_params"]."/pay?" ."ownOrder=".$xml["game_order"]."&goodsOrder=".$xml["order_no"]."&amount=".$xml["amount"]; // 通知游戏服务器发放东西 $this->send_to_game($_tempYrl); return "SUCCESS"; }else{ return "SignError"; } } /** 发送到游戏服务器 */ private function send_to_game($gameUrl){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $gameUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($curl); curl_close($curl); return $data; } /** * 解密方法 * $strEncode 密文 * $keys 解密密钥 为游戏接入时分配的 callback_key */ public function decode($strEncode, $keys) { if(empty($strEncode)){ return $strEncode; } preg_match_all('(\d+)', $strEncode, $list); $list = $list[0]; if (count($list) > 0) { $keys = self::getBytes($keys); for ($i = 0; $i < count($list); $i++) { $keyVar = $keys[$i % count($keys)]; $data[$i] = $list[$i] - (0xff & $keyVar); } return self::toStr($data); } else { return $strEncode; } } /** * 计算游戏同步签名 */ public static function getSign($params,$callbackkey){ return md5($params['nt_data'].$params['sign'].$callbackkey); } /** * MD5签名替换 */ static private function replaceMD5($md5){ strtolower($md5); $bytes = self::getBytes($md5); $len = count($bytes); if ($len >= 23){ $change = $bytes[1]; $bytes[1] = $bytes[13]; $bytes[13] = $change; $change2 = $bytes[5]; $bytes[5] = $bytes[17]; $bytes[17] = $change2; $change3 = $bytes[7]; $bytes[7] = $bytes[23]; $bytes[23] = $change3; }else{ return $md5; } return self::toStr($bytes); } /** * 转成字符数据 */ private static function getBytes($string) { $bytes = array(); for($i = 0; $i < strlen($string); $i++){ $bytes[] = ord($string[$i]); } return $bytes; } /** * 转化字符串 */ private static function toStr($bytes) { $str = ''; foreach($bytes as $ch) { $str .= chr($ch); } return $str; } }
关键字词:APP##