ThinkPHP之集成微信支付的操作步骤
YPE html>
微信æ¯ä»å¨æ们ççæ´»ä¸å·²ç»å¸ç©ºè§æ¯äºï¼ä½æ¯å¨åå±è¿ç¨ä¸ï¼å¾®ä¿¡å°èç®ä¹å¾å欢è¿ï¼ç°å¨ç±ç«ææ¯é¢éå°ç¼å°±ä¸ºå¤§å®¶ä»ç»çThinkPHPä¹éæ微信æ¯ä»çæä½æ¥éª¤ï¼ä¸èµ·åèççå§ï¼
å ä¸ææå¾ï¼æè¦åè¯ä½ æè¿ä¸ç¯æç« åçæ¯å¾®ä¿¡æ¯ä»ä¹ä¸çï¼æ®éåæ·èéæå¡ååæ·çç»ä¸ä¸åJSPIï¼å¾®ä¿¡æ¯ä»ï¼
å ¶å®èªå·±æ´åSDK失败äºï¼ç¨äºä¸ä¸ªå客å主æ´åç代ç ï¼å¨è¿éåä¸ä¸ç¬è®°ï¼
åé¢åå¤ï¼
1ãå¾®ä¿¡å ¬ä¼å·ï¼
ããããç¬ç¹çappidãappscrectãæ¥å£æéä¹ä¸è®¾ç½®å¯ä»¥è·åç¨æ·IDä¿¡æ¯æéçååï¼æ¯ä¸ªç¨æ·å¯¹äºä¸åå ¬ä¼é½ä¼æä¸ä¸ªç¹æIDï¼éè¿è¿ä¸ªIDè·åç¨æ·å¾®ä¿¡è´¦å·åºæ¬ä¿¡æ¯ã详æ ç微信å¼åè ææ¡£ï¼ãå¨å¾®ä¿¡æ¯ä»æé®åºè®¾ç½®å¾®ä¿¡æ¯ä»ææç®å½ï¼åå°å起请æ±çæ§å¶å¨é£ä¸å±ï¼ã设置å¼åè 微信账å·ä¸ºæµè¯ç½ååï¼ç¨å¾®ä¿¡å¼åè å·¥å ·çæ¶åéè¦ï¼
2ã微信æ¯ä»å¹³å°ï¼
ããåæ·å¹³å°ç»éè´¦å·ãæ¯ä»å¯é¥ï¼éæ¶å¯ä»¥èªè¡è®¾ç½®ï¼åªè½æä¸ä¸ªï¼ã
3ãæ´åè¿å»thinkphpä¹ä¸é»è¾ï¼
ããå端微信æ¯ä»æé®è®¾ç½®ç¹å»è°ç¨æ¯ä»åèµ·æ§å¶å¨æ¹æ³ã
ããæ§å¶å¨è¿è¡ï¼å¼ç¨å¾®ä¿¡æ¯ä»ç±»ãè·åç¨æ·openidãè·å订åæ°æ®ãæ¼æ¥åºæææ®éåæ·é¢æ¯ä»jspéè¦çæ°æ®ï¼displayåºé£ä¸ªèªå®ä¹çæ¯ä»é¡µé¢ã
ããå¨æ¯ä»é¡µé¢ç¹å»æ¯ä»ãè°ç¨å¾®ä¿¡æä¾çjspiåèµ·æ¯ä»çscripetå½æ°åèµ·æ¯ä»ã
ããæ¯ä»å®æ以å页é¢ä¼éå®åå°ï¼å¨èªå®ä¹æ¯ä»é¡µé¢çscriptå½æ°é设置ç跳转ç®å½{:U('controller/function)}ï¼,并ä¸å¼æ¥ï¼éé»ï¼è®¾ç½®çå¼æ¥å¤ç订åé»è¾ï¼è®°å½æ¯ä»æ¶é´å¦ãæ 记为已ç»æ¯ä»å¦ãæ è®°æ¯å¾®ä¿¡æ¯ä»å¦ï¼ä¹ç±»çã
代ç ï¼
ããæç订å页é¢ç微信æ¯ä»æé®ï¼
åèµ·æ¯ä»æ§å¶å¨Wxpay:
$_GET['order_key_num'],'expire'=>3600)); S('pay_now_id',$_GET['order_key_num'],3600); } vendor('Weixinpay.WxPayPubHelper'); //使ç¨jsapiæ¥å£ $jsApi = new \JsApi_pub(); //=========æ¥éª¤1ï¼ç½é¡µææè·åç¨æ·openid============ //éè¿codeè·å¾openid if($_GET['code'] == ''){ //跳转 $redirect_uri = 'https://å½ååå+模å+æ§å¶å¨+æ¹æ³'; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize ?appid=å ¬ä¼å·ç¹æIDredirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'; header("Location: $url"); exit(); }else{ //è·åopenid $url = 'https://api.weixin.qq.com/sns/oauth2/access_token ?appid=å ¬ä¼å·ID&secret=å ¬ä¼å·scrept&code='.$_GET['code'].'&grant_type=authorization_code'; $openid_arr = json_decode(file_get_contents($url),true); } $openid=$openid_arr['openid']; $pay_now_id = S('pay_now_id'); if($pay_now_id){ $id=$pay_now_id; $o = D('order_info'); $order_info = $o->where('order_id = %d',$id)->find(); if(empty($order_info['paycode'])){ $order_info['paycode'] = 'weixin'; } if($order_info['is_pay']){ $this->error('å½å订åå·²ç»æ¯ä»'); } }else{ $this->error("ä¸åå¨å½å订åç¼å·!"); } $res = array( 'order_sn' => $order_info['order_sn'], 'order_amount' => $order_info['pay_money'] ); //=========æ¥éª¤2ï¼ä½¿ç¨ç»ä¸æ¯ä»æ¥å£ï¼è·åprepay_id============ //使ç¨ç»ä¸æ¯ä»æ¥å£ $unifiedOrder = new \UnifiedOrder_pub(); //设置ç»ä¸æ¯ä»æ¥å£åæ° //è®¾ç½®å¿ å¡«åæ° //appid已填,åæ·æ ééå¤å¡«å //mch_id已填,åæ·æ ééå¤å¡«å //noncestr已填,åæ·æ ééå¤å¡«å //spbill_create_ip已填,åæ·æ ééå¤å¡«å //sign已填,åæ·æ ééå¤å¡«å $total_fee = $order_info['pay_money']*100; // $total_fee = $res['order_amount']; //$total_fee = 1; // var_dump($order_info['pay_money']);die; $body = "订åæ¯ä»"; $unifiedOrder->setParameter("openid", "$openid");//ç¨æ·æ è¯ $unifiedOrder->setParameter("body", 'ååéè´');//ååæè¿° //èªå®ä¹è®¢åå·ï¼æ¤å¤ä» ä½ä¸¾ä¾ $unifiedOrder->setParameter("out_trade_no", $order_info['order_sn']);//åæ·è®¢åå· $unifiedOrder->setParameter("total_fee", $total_fee);//æ»éé¢ //$unifiedOrder->setParameter("attach", "order_sn={$res['order_sn']}");//éå æ°æ® $unifiedOrder->setParameter("notify_url", \WxPayConf_pub::NOTIFY_URL);//éç¥å°å $unifiedOrder->setParameter("trade_type", "JSAPI");//交æç±»å //éå¿ å¡«åæ°ï¼åæ·å¯æ ¹æ®å®é æ åµéå¡« //$unifiedOrder->setParameter("sub_mch_id","XXXX");//ååæ·å· //$unifiedOrder->setParameter("device_info","XXXX");//设å¤å· //$unifiedOrder->setParameter("attach","XXXX");//éå æ°æ® //$unifiedOrder->setParameter("time_start","XXXX");//交æèµ·å§æ¶é´ //$unifiedOrder->setParameter("time_expire","XXXX");//交æç»ææ¶é´ //$unifiedOrder->setParameter("goods_tag","XXXX");//ååæ è®° //$unifiedOrder->setParameter("openid","XXXX");//ç¨æ·æ è¯ //$unifiedOrder->setParameter("product_id","XXXX");//ååID $prepay_id = $unifiedOrder->getPrepayId(); // var_dump($prepay_id);die; //=========æ¥éª¤3ï¼ä½¿ç¨jsapiè°èµ·æ¯ä»============ $jsApi->setPrepayId($prepay_id); $jsApiParameters = $jsApi->getParameters(); $wxconf = json_decode($jsApiParameters, true); if ($wxconf['package'] == 'prepay_id=') { $this->error('å½å订ååå¨å¼å¸¸!'); } $this->assign('res', $res); $this->assign('jsApiParameters', $jsApiParameters); $this->display('jsapi'); } //å¼æ¥éç¥urlï¼åæ·æ ¹æ®å®é å¼åè¿ç¨è®¾å® public function notify_url() { vendor('Weixinpay.WxPayPubHelper'); //使ç¨éç¨éç¥æ¥å£ $notify = new \Notify_pub(); //åå¨å¾®ä¿¡çåè° $xml = $GLOBALS['HTTP_RAW_POST_DATA']; $notify->saveData($xml); //éªè¯ç¾åï¼å¹¶ååºå¾®ä¿¡ã //对åå°éç¥äº¤äºæ¶ï¼å¦æ微信æ¶å°åæ·çåºçä¸æ¯æåæè¶ æ¶ï¼å¾®ä¿¡è®¤ä¸ºéç¥å¤±è´¥ï¼ //微信ä¼éè¿ä¸å®ççç¥ï¼å¦30åéå ±8次ï¼å®æéæ°åèµ·éç¥ï¼ //å°½å¯è½æé«éç¥çæåçï¼ä½å¾®ä¿¡ä¸ä¿è¯éç¥æç»è½æåã if($notify->checkSign() == FALSE){ $notify->setReturnParameter("return_code", "FAIL");//è¿åç¶æç $notify->setReturnParameter("return_msg", "ç¾å失败");//è¿åä¿¡æ¯ }else{ $notify->setReturnParameter("return_code", "SUCCESS");//设置è¿åç } $returnXml = $notify->returnXml(); //==åæ·æ ¹æ®å®é æ åµè®¾ç½®ç¸åºçå¤çæµç¨ï¼æ¤å¤ä» ä½ä¸¾ä¾======= //以logæ件形å¼è®°å½åè°ä¿¡æ¯ //$log_name = "notify_url.log";//logæä»¶è·¯å¾ //$this->log_result($log_name, "ãæ¥æ¶å°çnotifyéç¥ã:\n".$xml."\n"); $parameter = $notify->xmlToArray($xml); //$this->log_result($log_name, "ãæ¥æ¶å°çnotifyéç¥ã:\n".$parameter."\n"); if($notify->checkSign() == TRUE){ if ($notify->data["return_code"] == "FAIL") { //æ¤å¤åºè¯¥æ´æ°ä¸ä¸è®¢åç¶æï¼åæ·èªè¡å¢å æä½ //$this->log_result($log_name, "ãéä¿¡åºéã:\n".$xml."\n"); //æ´æ°è®¢åæ°æ®ãéä¿¡åºéã设为æ æ订å echo 'error'; } else if($notify->data["result_code"] == "FAIL"){ //æ¤å¤åºè¯¥æ´æ°ä¸ä¸è®¢åç¶æï¼åæ·èªè¡å¢å æä½ //$this->log_result($log_name, "ãä¸å¡åºéã:\n".$xml."\n"); //æ´æ°è®¢åæ°æ®ãéä¿¡åºéã设为æ æ订å echo 'error'; } else{ //$this->log_result($log_name, "ãæ¯ä»æåã:\n".$xml."\n"); //æè¿éç¨å°ä¸ä¸ªprocessæ¹æ³ï¼æåè¿åæ°æ®åå¤çï¼è¿åå°æ°æ®å ·ä½å¯ä»¥åè微信çææ¡£ if ($this->process($parameter)) { //å¤çæååè¾åºsuccessï¼å¾®ä¿¡å°±ä¸ä¼åä¸å请æ±äº echo 'success'; }else { //没æå¤çæåï¼å¾®ä¿¡ä¼é´éçåéè¯·æ± echo 'error'; } } } } //订åå¤ç private function process($parameter) { //æ¤å¤åºè¯¥æ´æ°ä¸ä¸è®¢åç¶æï¼åæ·èªè¡å¢å æä½ /* * è¿åçæ°æ®æå°æ以ä¸å 个 * $parameter = array( 'out_trade_no' => xxx,//åæ·è®¢åå· 'total_fee' => XXXX,//æ¯ä»éé¢ 'openid' => XXxxx,//ä»æ¬¾çç¨æ·ID ); */ $data = array( 'order_sn'=>$parameter['out_trade_no'], 'des'=>('订å交æï¼'.$parameter['out_trade_no']), 'money'=>$parameter['total_fee'], ); orderhandlestarysdgdss($data);//è¿æ¯ä¸ä¸ªcommonæ¹æ³ï¼ä»ä¼å°è¯¥è®¢åç¶æ设置为已æ¯ä»ä¹ç±»ç return true; } } ?>
åèµ·æ¯ä»åæ¼æ¥é¢æ¯ä»æ°æ®åæ°ï¼åæ°å表ç微信æ®éåæ·å¼åè ææ¡£ââ微信æ¯ä»ââç»ä¸ä¸åï¼displayç页é¢ï¼
ä¸
- æ¬æ¬¡è®¢åéæ¯ä»ï¼ï¿¥{$res['order_amount']} å
ç¶åå°±æ¯ç±»æ件å¦ï¼
é£ä¸ªcacertæ¯è¯ä¹¦åæ¾ç®å½ï¼è¯ä¹¦ä¸ä¸å®éè¦çï¼
vendoræ件夹å¨æçæ件éé¢æ¾æ¾å°±å¯ä»¥ã
综ä¸æè¿°ï¼å°±æ¯ç±ç«ææ¯é¢éå°ç¼ä¸ºä½ ä»ç»çThinkPHPä¹éæ微信æ¯ä»çæä½æ¥éª¤ï¼å¦æä½ æè¿æ¹é¢ççèï¼éæ©js.aizhan.comæ¯ææ£ç¡®çå³å®ã
下一篇:PHP开发中小程序中验证码的操作