zoukankan      html  css  js  c++  java
  • 微信开发-微信红包实例;

    此开发结合THINKPHP;

    效果图:

    step:1 写一个红包工具类; 目录为__ROOT__ThinkPHPLibraryVendorphpPayWxPayPubHelperWxPayPubHelper.php

    /*
     * 红包工具类
     * */
    class bonus_pub extends Wxpay_client_pub{
        function __construct()
        {
            //设置接口链接
            $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
            //设置curl超时时间
            $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
        }
    
        /**
         * 生成接口参数xml
         */
        function createXml()
        {
            try
            {
    
                //检测必填参数
                if($this->parameters["mch_billno"] == null)
                {
                    throw new SDKRuntimeException("缺少红包接口必填参数mch_billno!"."<br>");
                }elseif($this->parameters["send_name"] == null){
                    throw new SDKRuntimeException("缺少红包接口必填参数send_name!"."<br>");
                }elseif ($this->parameters["act_name"] == null ) {
                    throw new SDKRuntimeException("缺少红包接口必填参数act_name!"."<br>");
                }elseif ($this->parameters["total_num"] == null) {
                    throw new SDKRuntimeException("缺少红包接口必填参数total_num!"."<br>");
                }elseif ($this->parameters["total_amount"] == null) {
                    throw new SDKRuntimeException("缺少红包接口必填参数total_amount!"."<br>");
                }elseif ( $this->parameters["re_openid"] == NULL){
                    throw new SDKRuntimeException("缺少红包接口必填参数 openid!"."<br>");
                }elseif($this->parameters["remark"] == NULL){
                    throw new SDKRuntimeException("缺少红包接口必填参数 remark!"."<br>");
                }elseif($this->parameters["wishing"] == NULL){
                    throw new SDKRuntimeException("缺少红包接口必填参数 wishing!"."<br>");
                }
                $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
                $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
                $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
                $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
                $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    
                return  $this->arrayToXml($this->parameters);
            }catch (SDKRuntimeException $e)
            {
                die($e->errorMessage());
            }
        }
    
        /**
         * 发红包 并且获取返回值
         */
        function send_bouns()
        {
            $this->postXmlSSL();
            $this->result = $this->xmlToArray($this->response);
            return $this->result;
        }
    }

    step2 写一个红包调用类;__ROOT__ThinkPHPLibraryVendorphpPaydemoBonus.PHP

      

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 15-9-8
     * Time: 下午6:07
     */
    header("Content-type:text/html;charset=utf-8");
    class bonus extends ThinkController{
    
        public function bonus(){
            //构造方法
            vendor ( 'phpPay.WxPayPubHelper.WxPayPubHelper' );
        }
    
        /*openid 和红包金额*/
        public function sendBonus($openid,$fee){
    
            $fee = $fee*100;
            $bouns = new onus_pub();
            $mch_billno = WxPayConf_pub::MCHID.time().rand(0,100);
            $bouns->setParameter("wxappid",WxPayConf_pub::APPID);//总金额
            $bouns->setParameter("mch_billno","$mch_billno");//商户订单号
            $bouns->setParameter("send_name","Gosstech");//红包发送者名称
            $bouns->setParameter("re_openid","$openid");//用户openid
            $bouns->setParameter("total_amount","$fee");//总金额
            $bouns->setParameter("total_num","1");//红包发放总人数
            $bouns->setParameter("wishing","好人一生平安,gosstech公益基金会");//附加数据 订单ID
            $bouns->setParameter("act_name","公益红包");//活动名称
            $bouns->setParameter("remark","一起做公益吧");//备注
            $res = $bouns->send_bouns();
            return $res;
        }
    
    }

    step3  测试: 写一个控制器;随意;

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 15-9-9
     * Time: 下午4:03
     */
    
    namespace PayController;
    use ThinkController;
    class BonusController extends Controller {
        public function sendBouns(){
            vendor ( 'phpPay.demo.bonus' );
            $bonus = new onus();
            $bonus->sendBonus('oc8rujitiX4ghHtvP57WvQQW4-UA',1);
        }
    } 
  • 相关阅读:
    nyoj 117 求逆序数 (归并(merge)排序)
    2018年四川理工学院软件工程考试大纲(软件需求分析)
    2018年四川理工学院软件工程考试大纲(软件计划与可行性研究)
    hdu 1166 敌兵布阵 (线段树、单点更新)
    hdu 1754 I Hate It (线段树、单点更新)(PS:ios::sync_with_stdio(false)可以加快cin、cout的读取写出速度)
    周末手撸管理系统(一)
    drf源码save以及response
    drf框架serializers中ModelSerializer类简化序列化和反序列化操作
    drf框架序列化和返序列化
    django模型中有外键关系的表删除相关设置
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4796765.html
Copyright © 2011-2022 走看看