>  기사  >  백엔드 개발  >  thinkphp는 WeChat 결제 코드 Sharing_php 예제를 통합합니다.

thinkphp는 WeChat 결제 코드 Sharing_php 예제를 통합합니다.

WBOY
WBOY원래의
2016-12-05 13:28:281118검색

본 글의 예시는 참고용으로 thinkphp 통합 위챗 결제 코드를 공유하고 있습니다

다운로드: 결제 SDK

WeChat Payment SDK를 타사 라이브러리 공급업체에 넣으세요. 자금 손실을 방지하려면 WxPay.Config.php의 판매자 정보를 공식 계정 정보로 변경하세요.

php 사이드 코드

public function pay(){ 
  //商户基本信息,可以写死在WxPay.Config.php里面,其他详细参考WxPayConfig.php     
  vendor('Pay.JSAPI'); 
  $tools = new \JsApiPay(); 
  $openid = $tools->GetOpenid(); 
  $Out_trade_no=date('YHis').rand(100,1000); 
  $Total_fee='测试'; 
  $Body='啥也不说'; 
  $Total_fee=1; 
  $input = new \WxPayUnifiedOrder(); 
  $input->SetBody($Body); 
  $input->SetOut_trade_no($Out_trade_no); 
  $input->SetTotal_fee($Total_fee); 
  $input->SetNotify_url("http://xx.xxx.com/pay/notify.php"); 
  $input->SetTrade_type("JSAPI"); 
  $input->SetOpenid($openId); 
  $order = \WxPayApi::unifiedOrder($input); 
  $this->jsApiParameters = $tools->GetJsApiParameters($order); 
  $this->display(); 
} 

HTML 사이드 코드

<!doctype html> 
<head> 
  <meta charset="UTF-8"> 
  <title>微信安全支付</title> 
  <meta name="keywords" content="" /> 
  <meta name="HandheldFriendly" content="True"> 
  <meta name="MobileOptimized" content="320"> 
  <meta name="format-detection" content="telephone=no"> 
  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> 
  <meta http-equiv="Pragma" content="no-cache" /> 
  <meta http-equiv="Expires" content="0" /> 
  <meta http-equiv="cleartype" content="on"> 
  <meta id="viewport" name="viewport" content="width=320, initial-scale=1.0,maximum-scale=1.0, user-scalable=0," /> 
  <script type="text/javascript"> 
    //调用微信JS api 支付 
    function jsApiCall() 
    { 
      WeixinJSBridge.invoke( 
        'getBrandWCPayRequest', 
        {$jsApiParameters}, 
        function(res){ 
          WeixinJSBridge.log(res.err_msg); 
          if(res.err_msg == 'get_brand_wcpay_request:cancel') { 
            alert("您已取消了此次支付"); 
            return; 
          } else if(res.err_msg == 'get_brand_wcpay_request:fail') { 
            alert("支付失败"); 
            return; 
          } else if(res.err_msg == 'get_brand_wcpay_request:ok') { 
          alert("支付成功!");//跳转到订单页面 
           
        } else { 
          alert("未知错误"+res.error_msg); 
          return; 
        } 
      } 
      ); 
    } 
    function callpay() 
    { 
      if (typeof WeixinJSBridge == "undefined"){ 
        if( document.addEventListener ){ 
          document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); 
        }else if (document.attachEvent){ 
          document.attachEvent('WeixinJSBridgeReady', jsApiCall);  
          document.attachEvent('onWeixinJSBridgeReady', jsApiCall); 
        } 
      }else{ 
        jsApiCall(); 
      } 
    } 
  </script> 
</head> 
<body> 
  <button type="button" class="btn btn-danger btn-lg btn-block" onclick="callpay()"> 
    确认支付 
  </button> 
</body> 
</html> 

그렇게 간단합니다.

위 글의 내용은 모두의 공부에 도움이 되길 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.