微信支付(公众号支付)总结

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信支付(公众号支付)总结相关的知识,希望对你有一定的参考价值。

1.首先我们下载好sdk包,这个时候首先要配置config文件

2.然后在example文件夹下打开jsapi.php 和 WxPay.JsApiPay.php

注:其实jsapi.php里面写的就是支付需要的步骤,里面的方法在 WxPay.JsApiPay.php里面慢慢的找都可以找到

3.需要注意的是如果是在yii框架中那么这个支付下载下来的文件包需要重构一遍代码,因为require等这种的包含方法有时候似乎不管用甚至是报错!

主要就是将控制器里面的class单独拿出来写成新的.php文件,要注意注意注意格外注意重新写的时候命名空间的规则(文件名和class名称要一致,此外namespace也要可以访问到,尽量写全文件路径)

如图:

技术分享

 

 

 

不管是哪个文件必须都是全部的名称,改完了之后就可以参照着写了!在新的控制器必须引用所有用到的方法的控制器,不然会提示找不到

<?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);
    }
}

 

以上到最后的返回其实就已经给到前端支付需要用到的所有的参数了

 

那么还是要再说一下$input->SetNotify_url("http://www.peita.net/api/register/notify");的问题

之前我写的是http://www.peita.net?ak=notify

这样来看虽然可以访问不报错,但是实际是不可以的,链接中不可以带参数!!!!!!!!切记!

以上是关于微信支付(公众号支付)总结的主要内容,如果未能解决你的问题,请参考以下文章

微信公众号支付|微信H5支付|微信扫码支付|小程序支付|APP微信支付解决方案总结

微信支付之扫码支付公众号支付H5支付小程序支付相关业务流程分析总结

vue项目使用微信公众号支付总结

H5在微信公众号里调用微信支付总结(前端)

vue项目使用微信公众号支付总结及遇到的坑

微信支付-小程序H5 公众号 Payment SDK