微信支付(Yii)

1、下載官方demo

下載路徑:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1

將此文件夾放入extensions目錄下


2、extensions目錄下創建Wechatpay.php 文件  內容如下

<?php 
/**
 * 微信支付
*/
namespace app\extensions;

require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Api.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Data.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Notify.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Config.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/example/WxPay.NativePay.php';


class Wechatpay
{
   /**
    * TODO: 修改這裏配置爲您自己申請的商戶信息
    * 微信公衆號信息配置
    * 
    * APPID:綁定支付的APPID(必須配置,開戶郵件中可查看)
    * 
    * MCHID:商戶號(必須配置,開戶郵件中可查看)
    * 
    * KEY:商戶支付密鑰,參考開戶郵件設置(必須配置,登錄商戶平臺自行設置)
    * 設置地址:https://pay.weixin.qq.com/index.php/account/api_cert
    * 
    * APPSECRET:公衆帳號secert(僅JSAPI支付的時候需要配置, 登錄公衆平臺,進入開發者中心可設置),
    * 獲取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
    * @var string
    */
   const APPID = 'wx426b3015555a46be';
   const MCHID = '1900009851';
   const KEY = '8934e7d15453e97507ef794cf7b0519d';
   const APPSECRET = '7813490da6f1265e4901ffb80afaa36f';
   const NOTIFY_URL = 'http://younijia.fenglianda.cn/site/wechatpay';

   private $APPID;
   private $MCHID;
   private $KEY;
   private $APPSECRET;
   function __construct()
    {
        $this->APPID = Wechatpay::APPID;
        $this->MCHID = Wechatpay::MCHID;
        $this->KEY = Wechatpay::KEY;
        $this->APPSECRET = Wechatpay::APPSECRET;
    }

    /*
     * 微信支付方法
     * @param $body 商品描述內容
     * @param $attach     訂單名稱
     * @param $out_trade_no     訂單編號
     * @param $total_fee 支付金額,
     * @param $goods_tag 商品標記
     * @param $product_id 商品id
     *
     * @return 跳轉到微信二維碼
     *
     */

    function native($out_trade_no,$total_fee,$goods_tag,$product_id,$body='',$attach='')
    {
       $notify = new \NativePay();
       $input = new \WxPayUnifiedOrder();
      $input->SetBody($body);
      $input->SetAttach($attach);
      //訂單號
      $input->SetOut_trade_no($out_trade_no); // 訂單號
      $input->SetTotal_fee($total_fee);  //分爲單位(訂單金額)
      $input->SetTime_start(date("YmdHis"));
      $input->SetTime_expire(date("YmdHis", time() + 600));
      $input->SetGoods_tag($goods_tag);

      // $input->SetNotify_url("http://www.nxq.com/test/weixinPay/example/notify_url.php");
      $input->SetNotify_url(Wechatpay::NOTIFY_URL);
      //原生掃碼支付
      $input->SetTrade_type("NATIVE");
       //商品ID
      $input->SetProduct_id($product_id);
      // print_r($input);die;
      //生成二維碼的短鏈接地址
      $result = $notify->GetPayUrl($input);
      // print_r($result);die;
      $url2 = $result["code_url"];
      // print_r($url2);die;
      $url = "http://127.0.0.1/XXX/basic/extensions/weixinPay/example/qrcode.php?data=".urldecode($url2);
      return $url;
    }


    /**
    * 微信退款
    * @param $weixin_trade_no    微信交易號
    * @param $total_fee          訂單總金額
    * @param $refund_fee         退款金額
    */
    function refund($weixin_trade_no,$total_fee,$refund_fee)
    {
       if(isset($weixin_trade_no) && $weixin_trade_no != ""){
         $transaction_id = $_REQUEST["transaction_id"];
         $total_fee = $_REQUEST["total_fee"];
         $refund_fee = $_REQUEST["refund_fee"];
         $input = new \WxPayRefund();
         $input->SetTransaction_id($transaction_id);
         $input->SetTotal_fee($total_fee);
         $input->SetRefund_fee($refund_fee);
          $input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
          $input->SetOp_user_id(WxPayConfig::MCHID);
         printf_info(WxPayApi::refund($input));
         exit();
      }
    }
}

 ?>


demo中所有的引入文件格式改爲如圖所示

Demo中的文件不用添加命名空間,new \類名()時前面加\


前端用ajax請求支付時控制器調用extensions/Wechatpay類中native方法



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章