微信JSAPI支付V2版本(PHP)
生活随笔
收集整理的這篇文章主要介紹了
微信JSAPI支付V2版本(PHP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??1、支付 (member_id 用戶id)
public function unifyPay(){$param = (I('get.'));if(!$param['member_id'] || !$param['order_sn']){return json_encode(['code'=>201,'message'=>'缺少參數']);}$data = [];$paydata = [];$unifydata = [];$orderinfo = Db::name('order')->where(['order_sn'=>$param['order_sn'],'user_id'=>$param['member_id']])->find();if(empty($orderinfo)){return json_encode(['code'=>201,'message'=>'訂單不存在']);}$userinfo = Db::name('users')->where(['user_id'=>$param['member_id']])->find();if(empty($userinfo)){return json_encode(['code'=>201,'message'=>'用戶不存在']);}$config = Db::name('config')->where('name','site_url')->find();$wechatconf = config('wechat'); //這個數組里所有的數據都是必填的$unifydata = ['appid' => $wechatconf['appid'],//appid'mch_id' => $wechatconf['mchid'],//商戶id'nonce_str' => md5(rand(100000,999999)),//隨機字符串'body' => '商品','out_trade_no' => $param['order_sn'],//訂單號 // 'total_fee' => bcmul((string)$orderinfo['order_amount'], '100',0),'total_fee' => bcmul(0.01, '100',0),//金額'spbill_create_ip' => $_SERVER["REMOTE_ADDR"],'notify_url' => $config['value'].'/index.php/mobile/Pay/notify',//回調地址'trade_type' => 'JSAPI','openid' => $userinfo['openid'],];$this->closeorder($unifydata);//關閉以前的訂單,當前項目需要,可以不加 // 'total_fee' => bcmul((string)$orderinfo['order_amount'], '100'), // 'key' => $wechatconf['pay_key'], // API 密鑰// 如需使用敏感接口(如退款、發送紅包等)需要配置 API 證書路徑(登錄商戶平臺下載 API 證書) // 'cert_path' => '', // XXX: 絕對路徑!!!! // 'key_path' => '', // XXX: 絕對路徑!!!!$this->pay_key = $wechatconf['sign'];//32位簽名$orderResult = $this->create_pay_order($unifydata);創建訂單$timeStamp = time();$paydata = ['appId' => $wechatconf['appid'],'timeStamp' => "$timeStamp",'nonceStr' => md5(rand(100000,999999)),'package' => 'prepay_id=' . $orderResult['prepay_id'],'signType' => 'MD5'];$sign = $this->getSign($paydata);//給小程序生成驗證簽名$paydata['paySign'] = $sign;$data = ['code'=>0,'message'=>'統一下單成功','data'=>$paydata];return json_encode($data);}2、/** * POST 調用API * @param $url * @param array $data * @return bool|string */
public function post_data($url,$data=[]){$ch = curl_init ();// print_r($ch);curl_setopt ( $ch, CURLOPT_URL, $url );curl_setopt ( $ch, CURLOPT_POST, 1 );curl_setopt ( $ch, CURLOPT_HEADER, 0 );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );$return = curl_exec ( $ch );curl_close ( $ch );return $return;}3、/** * 獲取簽名 * @param $data * @return string */
protected function getSign($data) {ksort($data);$tmpstr = $this->arrToTempStr($data);$tmpSign = $tmpstr . '&key=' . $this->pay_key;$sign = strtoupper(md5($tmpSign));return $sign;}4、/** * 數組轉簽名字符串 * @param $arr * @return false|string */
private function arrToTempStr($arr) {$str = '';foreach ($arr as $key => $value) {$str .= $key . '=' . $value . '&';}return substr($str,0,-1);}5、/** * 數組轉微信XML * @param $arr * @return string */
protected function arrayToXml($arr) {$xml = "<xml>";foreach ($arr as $key=>$val) {if ($key == 'detail') {$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";} else {$xml.="<".$key.">".$val."</".$key.">";}}$xml.="</xml>";return $xml;}6、/** * 解析XML * @param $xml * @return bool */
protected function analyze_xml($xml) {$dom = new DOMDocument();$dom->loadXML($xml);$node = $dom->documentElement;$array = $this->getArray($node);return $array;}7、解析XML 轉數組
private function getArray($node) {$array = false;if ($node->hasAttributes()) {foreach ($node->attributes as $attr) {$array[$attr->nodeName] = $attr->nodeValue;}}if ($node->hasChildNodes()) {if ($node->childNodes->length == 1) {$array[$node->firstChild->nodeName] = $this->getArray($node->firstChild);} else {foreach ($node->childNodes as $childNode) {if ($childNode->nodeType != XML_TEXT_NODE) {$array[$childNode->nodeName][] = $this->getArray($childNode);}}}} else {return $node->nodeValue;}return $array;}8、/**關閉訂單*/
public function closeorder($order){$apiparams=['appid'=>$order['appid'],'mch_id'=>$order['mch_id'],'out_trade_no'=>$order['out_trade_no'],'nonce_str'=>$order['nonce_str'],];$apiparams['sign']=$this->getSign($apiparams);$apiparams = $this->arrayToXml($apiparams);$this->post_data('https://api.mch.weixin.qq.com/pay/closeorder',$apiparams);return true;}9、/** * 創建微信支付訂單 數據(下單)? * @param $inputData * @return array */
private function create_pay_order($inputData) {$return = array();$inputData['sign'] = $this->getSign($inputData);$inputData = $this->arrayToXml($inputData);$result = $this->post_data('https://api.mch.weixin.qq.com/pay/unifiedorder',$inputData);$result = $this->analyze_xml($result);$return['return_code'] = isset($result['return_code']['0']['#cdata-section'])?$result['return_code']['0']['#cdata-section']:'';$return['return_msg'] = isset($result['return_msg']['0']['#cdata-section'])?$result['return_msg']['0']['#cdata-section']:'';$return['appid'] = isset($result['app_id']['0']['#cdata-section'])?$result['app_id']['0']['#cdata-section']:'';$return['mchid'] = isset($result['mch_id']['0']['#cdata-section'])?$result['mch_id']['0']['#cdata-section']:'';$return['nonce_str'] = isset($result['nonce_str']['0']['#cdata-section'])?$result['nonce_str']['0']['#cdata-section']:'';$return['sign'] = isset($result['sign']['0']['#cdata-section'])?$result['sign']['0']['#cdata-section']:'';$return['result_code'] = isset($result['result_code']['0']['#cdata-section'])?$result['result_code']['0']['#cdata-section']:'';$return['prepay_id'] = isset($result['prepay_id']['0']['#cdata-section'])?$result['prepay_id']['0']['#cdata-section']:'';$return['trade_type'] = isset($result['trade_type']['0']['#cdata-section'])?$result['trade_type']['0']['#cdata-section']:'';;return $return;}10、支付回調
public function notify(){$testxml = file_get_contents("php://input");$jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));$result = json_decode($jsonxml, true);//轉成數組,$wechatconf = config('wechat');if($result){//如果成功返回了if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){if($result['sign'] == $wechatconf['sign'] && $result['appid'] == $wechatconf['appid']){Db::name('service_order')->where('orderno',$result['out_trade_no'])->update(['pay_state'=>1,'trade_no'=>$result['transaction_id'],'pay_time'=>time()]);}}}}總結
以上是生活随笔為你收集整理的微信JSAPI支付V2版本(PHP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (16)UiBot:智能化软件机器人(以
- 下一篇: 计算机毕业设计springboot家教管