1. 程式人生 > >PHP 微信掃一掃支付(TP5)

PHP 微信掃一掃支付(TP5)

PHP交流群:294088839

1.支付呼叫

require_once "./payment/wxpay/php/lib/WxPay.Api.php";
require_once "./payment/wxpay/php/example/WxPay.NativePay.php";
$notify = new \NativePay();
$input = new \WxPayUnifiedOrder();
$input->SetBody("訂單內容");
$input->SetOut_trade_no($order_data['order_number']);
$input->SetTotal_fee($zongMoney);
$input->SetTime_start(date("YmdHis"));
$input->SetNotify_url(config('u_wx_notify_url'));
$input->SetTrade_type("NATIVE");
$input->SetProduct_id(rand(10000,99999));

$result = $notify->GetPayUrl($input);

$url2 =config('prcode'). $result["code_url"];
2.微信支付識別二維碼地址 這個在他的demo裡有個qrcode 檔案 
'prcode'=>'http://'.$_SERVER['HTTP_HOST'].'/payment/wxpay/php/example/qrcode.php?data='

3.引數配置 APPID等引數  在demo裡的 WxPay.Config.php 進行配置 可以寫一個建構函式 進行引數填寫

private $appid;
private $mch_id;
private $key;
private $appsecret;
   public function __construct()
   {
       $pay=\think\Db::name('pay_type')->where(['pay_id'=>2])->field('pay_json')->find();
       $wx=json_decode($pay['pay_json'],true);
       $this->appid=$wx['web_appid'];
       $this->mch_id=$wx['web_mch_id'];
       $this->key=$wx['web_key'];
       $this->appsecret=$wx['web_appsecret'];

   }

4.微信回撥地址 資料認證 

  $config = new \WxPayConfig();

  $notify = new \WxPayNotify();
  $notify->Handle($config, false);

  //儲存微信的回撥
  $objData = $GLOBALS['HTTP_RAW_POST_DATA'];
  //寫入日誌
  log_result("【接收到的notify通知】:\n".$objData."\n");
  $data=\WxPayResults::Init($config,$objData);
//  $data = $objData->GetValues();

  //TODO 1、進行引數校驗
  if(!array_key_exists("return_code", $data) || (array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
      //TODO失敗,不是支付成功的通知
      //如果有需要可以做失敗時候的一些清理處理,並且做一些監控
      $msg = "異常異常";
      log_result("【接收到的notify通知】:\n".$msg."\n");
      return false;
  }
  if(!array_key_exists("transaction_id", $data)){
      $msg = "輸入引數不正確";
      log_result("【接收到的notify通知】:\n".$msg."\n");
      return false;
  }