微信支付(公衆號支付)總結

1.首先咱們下載好sdk包,這個時候首先要配置config文件php

2.而後在example文件夾下打開jsapi.php 和 WxPay.JsApiPay.phphtml

注:其實jsapi.php裏面寫的就是支付須要的步驟,裏面的方法在 WxPay.JsApiPay.php裏面慢慢的找均可以找到前端

3.須要注意的是若是是在yii框架中那麼這個支付下載下來的文件包須要重構一遍代碼,由於require等這種的包含方法有時候彷佛無論用甚至是報錯!json

主要就是將控制器裏面的class單獨拿出來寫成新的.php文件,要注意注意注意格外注意從新寫的時候命名空間的規則(文件名和class名稱要一致,此外namespace也要能夠訪問到,儘可能寫全文件路徑)api

如圖:app

 

 

 

無論是哪一個文件必須都是所有的名稱,改完了以後就能夠參照着寫了!在新的控制器必須引用全部用到的方法的控制器,否則會提示找不到框架

<?php

namespace backend\modules\api\controllers;

use common\models\SmsReport;
use common\models\Aes;
use common\models\Counter;
use common\models\Phone;
use common\models\Ewm;
use common\models\EwmLog;
use common\models\ParentList;
use common\models\City;
use common\models\oss\oss;

use backend\modules\api\models\Goods;
use backend\modules\api\models\lists\GoodsList;

use Yii;
use yii\rest\Controller;
use backend\modules\api\common\lib\Helper;
use backend\modules\api\models\User;
use backend\modules\api\models\Register;
use backend\modules\api\models\Orders;
use backend\modules\api\models\Wxpay;
use backend\modules\api\models\Zfbpay;
use backend\modules\api\models\Share;
use backend\modules\api\common\lib\HttpUploadFile;
use yii\base\Object;
use backend\modules\api\models\Login;
use backend\modules\api\models\Banner;
use backend\modules\api\models\operates\UserOperate;
use backend\modules\api\models\details\MomentsDetail;
use backend\modules\api\models\lists\UserList;
use backend\modules\api\models\lists\MomentsCommentsList;
use backend\modules\api\models\Journalism;
use backend\modules\api\models\Journalismbanner;
use backend\modules\api\models\Encyclopedias;
use backend\modules\api\models\EncyclopediasComments;
use backend\modules\api\models\EncyclopediasTagsLog;
use backend\modules\api\models\JournalismTagsLog;
use backend\modules\api\models\details\SosDetail;
use backend\modules\api\models\lists\SosOperateList;
use backend\modules\api\models\lists\SosList;
use backend\modules\api\models\Screen;
use backend\modules\api\models\QuestionShare;
use backend\modules\api\models\Question;
use backend\modules\api\models\Collection;
use backend\modules\api\models\Pet;
use backend\modules\api\models\PetCategory;
use backend\modules\api\models\WxUser;
use common\models\wxpay\example\JsApiPay;
use common\models\wxpay\lib\WxPayApi;
use common\models\wxpay\lib\WxPayDataBase;
use common\models\wxpay\example\Log;
use common\models\wxpay\lib\WxPayUnifiedOrder;
use common\models\wxpay\lib\WxPayConfig;

header('Access-Control-Allow-origin:*');
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers:Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With');
header('Content-Type: text/html; charset=utf-8');

class WxpayController extends Controller{

    public function behaviors() {
        $behaviors = parent::behaviors();
        unset($behaviors ['authenticator']);
        return $behaviors;
    }

    public function actionIndex(){
        //接收信息生成訂單
        $user_id = Yii::$app->request->post('user_id')?Yii::$app->request->post('user_id'):null;
        $username = Yii::$app->request->post('username')?Yii::$app->request->post('username'):null;
        $area = Yii::$app->request->post('area')?Yii::$app->request->post('area'):null;
        $address = Yii::$app->request->post('address')?Yii::$app->request->post('address'):null;
        $phone = Yii::$app->request->post('phone')?Yii::$app->request->post('phone'):null;
        $goods_img = Yii::$app->request->post('goods_img')?Yii::$app->request->post('goods_img'):null;
        $goods_name = Yii::$app->request->post('goods_name')?Yii::$app->request->post('goods_name'):null;
        $goods_color = Yii::$app->request->post('goods_color')?Yii::$app->request->post('goods_color'):null;
        $goods_num = Yii::$app->request->post('goods_num')?Yii::$app->request->post('goods_num'):null;
        $goods_price = Yii::$app->request->post('goods_price')?Yii::$app->request->post('goods_price'):null;
        $goods_brand = Yii::$app->request->post('goods_brand')?Yii::$app->request->post('goods_brand'):null;
        $goods_content = Yii::$app->request->post('goods_content')?Yii::$app->request->post('goods_content'):null;
        $remarks = Yii::$app->request->post('remarks')?Yii::$app->request->post('remarks'):null;
        $goods_id = Yii::$app->request->post('goods_id')?Yii::$app->request->post('goods_id'):null;

        //訂單號
        $order_sn = WxPayConfig::MCHID.date("YmdHis");
        //總價
        $price_count = Yii::$app->request->post('price_count')?Yii::$app->request->post('price_count'):null;

        $model = new Orders();
        $model->user_id = $user_id;
        $model->order_sn = $order_sn;
        $model->username = $username;
        $model->area = $area;
        $model->address = $address;
        $model->phone = $phone;
        $model->goods_img = $goods_img;
        $model->goods_name = $goods_name;
        $model->goods_color = $goods_color;
        $model->goods_num = $goods_num;
        $model->goods_price = $price_count;
        $model->goods_brand = $goods_brand;
        $model->create_time = time();
        $model->goods_content = $goods_content;
        $model->remarks = $remarks;
        $model->goods_id = $goods_id;
        $model->pay_status = 1;
        $model->insert();

        //支付
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();

        $input = new WxPayUnifiedOrder();
        $input->SetBody($goods_name);
        $input->SetAttach("暫無附加信息");
        $input->SetOut_trade_no($order_sn);
        $input->SetTotal_fee($price_count);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("本價");
        $input->SetNotify_url("http://www.peita.net/api/register/notify");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return json_decode($jsApiParameters);
    }
}

 

以上到最後的返回其實就已經給到前端支付須要用到的全部的參數了yii

 

那麼仍是要再說一下$input->SetNotify_url("http://www.peita.net/api/register/notify");的問題post

以前我寫的是http://www.peita.net?ak=notifyui

這樣來看雖然能夠訪問不報錯,可是實際是不能夠的,連接中不能夠帶參數!!!!!!!!切記!

相關文章
相關標籤/搜索