微信新版代金券

2023-11-07

微信代金券的注意事项
1.直连商户或者服务商需要登录微信商户后台开通预充值营销插件和免充值营销插件
2.公众账号的appid 与商户号关联绑定关系
3.小程序需要小程序appid与商户号关联绑定
4.需要服务商或者直连商户的cert证书的序列号key证书的在服务器的据对路径
5.如果是子商户发放服务商创建的代金券还需要子商户的cert证书与key证书在服务器的绝对路径,
6.子商户发放服务商创建的代金券还需要向微信发送邮件申请跨商户发券权限 :
Q:商户号A制券,允许商户号B发券,如何配置?(例如服务商制券,在子商户的场景例如公众号、小程序里发券)
A:跨商户号发券的使用场景是,A商户号制券,在B商户的场景中发券,例如小程序。需要A开通跨商户发券制券商户号的白名单、B开通商户发券发券商户号的白名单。然后A开始制券,制券是要把B添加为“可发券商户”。然后再用B的商户号调用接口,在和B商户号有绑定关系的APPID对应的场景中发券,就可以了。
目前申请流程:
需要先开通跨商户号发券权限。发邮件给v_jjinglliu@tencent.com,anthonylin@tencent.com,说明申请原因、制券的商户号、发券商户号。邮件标题“申请跨商户号发券+商户名称”。配置批次的时候把可发券商户号填发券商户,然后调用发券接口即可。申请后会在一个工作日内开通并回复邮件。
注意(需要把发券商户子商户放在批次的可发券商户号中)
7.创建代金券时no_cash 为true时是免充值类型,false为预充值类型
8.如果是服务商创建代金券时available_merchants 应该是可核销代金券的子商户集合
注意商户A制券B商户发放,服务商是没有分润,服务商制券,服务商发券这个是有分润的
9.公共类

<?php

namespace Addons\Pay;
class WxPayV3
{
    protected $authorization = 'WECHATPAY2-SHA256-RSA2048';  //认证类型
    protected $method = "POST";
    protected $url; //链接
    protected $mch_id;        //商户号
    protected $nonce_str;        //随机字符串
    protected $sign;        //签名
    protected $timestamp;   //时间
    protected $serial_no;        //商户Api证书序列号
    protected $apiclient_key;   //私钥地址
    protected $apiclient_cert;   //公钥地址
    protected $token;        //Token
    protected $data;         //发送参数
    protected $stock_id;     //批次号
    protected $response;    //返回信息
    protected $image_type;    //图片类型
    protected $boundary;      //边界符
    protected $suffix;      //图片后缀
    protected $openid;      //发放的openid
    protected $file;         //图片信息


    public function __construct($param)
    {
        $this->mch_id = isset($param['mch_id']) ? $param['mch_id'] : '';
        $this->data = isset($param['data']) ? $param['data'] : '';
        $this->stock_id = isset($param['stock_id']) ? $param['stock_id'] : '';
        $this->serial_no = isset($param['serial_no']) ? $param['serial_no'] : '';
        $this->apiclient_key = isset($param['apiclient_key']) ? $param['apiclient_key'] : '';
        $this->image_type = isset($param['image_type']) ? $param['image_type'] : '';
        $this->boundary = isset($param['boundary']) ? $param['boundary'] : '';
        $this->openid = isset($param['openid']) ? $param['openid'] : '';
        $this->file = isset($param['file']) ? $param['file'] : '';
    }


    /**
     * 上传图片
     * @return bool|string
     */
    public function upload()
    {
        $this->url = 'https://api.mch.weixin.qq.com/v3/marketing/favor/media/image-upload';
        $result = $this->uploadRequestAction();
        return $result;
    }


    /**
     * 图片上传请求
     * @return bool|string
     */
    public function uploadRequestAction()
    {
        if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
            throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");
        }
        $headerParam = $this->uploadHeaderParam(); //获取头部信息
        $boundarystr = "--{$this->boundary}\r\n";// $out是post的内容
        $str = $boundarystr;
        $str .= 'Content-Disposition: form-data; name="meta"' . "\r\n";
        $str .= 'Content-Type: application/json' . "\r\n";
        $str .= "\r\n";
        $str .= json_encode($this->data) . "\r\n";
        $str .= $boundarystr;
        $str .= 'Content-Disposition: form-data; name="file"; filename="' . $this->data['filename'] . '"' . "\r\n";
        $str .= 'Content-Type: ' . $this->image_type . ";\r\n";
        $str .= "\r\n";
        $str .= $this->file . "\r\n";
        $str .= $boundarystr . "--\r\n";
//        print_r($str);exit;
        $this->response = $this->http_Request($this->url, $headerParam, $str);
        return $this->response;
    }


    /**
     * 图片上传头部参数
     * @return array
     */
    public function uploadHeaderParam()
    {
        $this->getSign();        //生成签名
        $this->getToken();        //生成Token
        $header = [
            "Content-Type: multipart/form-data;name=meta",
            "Content-Type: application/json",
            "User-Agent:" . $_SERVER['HTTP_USER_AGENT'],
            'Authorization: ' . $this->authorization . ' ' . $this->token,
            "Content-Type: multipart/form-data;boundary=" . $this->boundary
        ];
        return $header;
    }


    /**
     * 创建代金券
     * @return bool|string
     */
    public function createCoupon()
    {
        $this->url = 'https://api.mch.weixin.qq.com/v3/marketing/favor/coupon-stocks';       //创建代金券请求链接
        $result = $this->requestAction();
        return $result;
    }


    /**
     * 激活代金券
     * @return bool|string
     */
    public function activateCoupon()
    {
        $this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/{$this->stock_id}/start";
        $result = $this->requestAction();
        return $result;
    }


    /**
     * 发放代金券
     */
    public function grant()
    {
        $this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/users/{$this->openid}/coupons";
        $result = $this->requestAction();
        return $result;
    }


    /**
     * 暂停代金券
     */
    public function suspend()
    {
        $this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/{$this->stock_id}/pause";
        $result = $this->requestAction();
        return $result;
    }


    /**
     * 重启代金券
     */
    public function restart()
    {
        $this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/{$this->stock_id}/restart";
        $result = $this->requestAction();
        return $result;
    }


    /**
     * 发送请求
     * @return bool|string
     */
    protected function requestAction()
    {
        if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
            throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");
        }
        $headerParam = $this->getHeaderParam(); //获取头部信息
//       print_r($headerParam);
        $this->response = $this->http_Request($this->url, $headerParam, json_encode($this->data));
//        print_r(json_decode($this->response));
        return $this->response;
    }


    /**
     * 获取请求头部参数
     * @return array
     */
    protected function getHeaderParam()
    {
        $this->getSign();        //生成签名
        $this->getToken();        //生成Token
        $header = [
            "Content-type: application/json;charset='utf-8'",
            "Accept:application/json",
            "User-Agent:*/*",
            'Authorization: ' . $this->authorization . ' ' . $this->token,
        ];
        return $header;
    }


    /**
     * 生成签名
     */
    protected function getSign()
    {
        $url_parts = parse_url($this->url);  //链接
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $this->timestamp = time();
        $this->nonce_str = createKey(); //随机字符串
        $message = $this->method . "\n" .
            $canonical_url . "\n" .
            $this->timestamp . "\n" .
            $this->nonce_str . "\n" .
            json_encode($this->data) . "\n";
        openssl_sign($message, $raw_sign, openssl_get_privatekey(file_get_contents($this->apiclient_key)), 'sha256WithRSAEncryption');
        $this->sign = base64_encode($raw_sign);
    }


    /**
     * 生成Token
     * @return string
     */
    protected function getToken()
    {
        $this->token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
            $this->mch_id, $this->nonce_str, $this->timestamp, $this->serial_no, $this->sign);
    }


    /**
     * 数据请求
     * @param $url
     * @param array $header 获取头部
     * @param string $post_data POST数据,不填写默认以GET方式请求
     * @return bool|string
     */
    public function http_Request($url, $header = array(), $post_data = "")
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 2);
        if ($post_data != "") {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //设置post提交数据
        }
        //判断当前是不是有post数据的发
        $response = curl_exec($ch);
        if ($response === FALSE) {
            $response = "curl 错误信息: " . curl_error($ch);
        }
        curl_close($ch);
        return $response;
    }


}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

微信新版代金券 的相关文章

  • 多维数组 PHP 内爆 [重复]

    这个问题在这里已经有答案了 就我的数据结构而言 我有一个 communications 数组 每个 communications id 本身包含三部分信息 id score 和 content 我想内爆这个数组以获得逗号分隔的 id 列表
  • Laravel 5 命名约定

    我对 Laravel 约定有点困惑 因为我是这个框架的新手 我正在关注 Jeffrey Way 他使用的 Laracasts 视频Plural对于控制器名称 E g 页面控制器 卡片控制器 帖子控制器 但如果我参考官方文档Laravel g
  • 检查字符串是否编码为 UTF-8

    function seems utf8 str length strlen str for i 0 i lt length i c ord str i if c lt 0x80 n 0 0bbbbbbb elseif c 0xE0 0xC0
  • MySQL如何从多个表中获取数据

    我正在寻找 php MySQL jquery 的帮助 我有2张桌子 table1表 1 有 4 列 id 标题 desc thumb img tabel2表 2 有 3 列 id 表 id img 我只想将 2 个表与 get QS 的值进
  • 为什么 0.5 mod 0.1 在不同的编程语言中结果不同?

    我有一个关于模数的问题 模运算求一个数除以另一个数的余数 我原本期望 0 5 0 1 0 的结果 但是当我在 PHP 或 net 中运行它时 我得到 0 1 我运行的 php 代码是 var dump fmod 0 5 0 1 在 net中
  • Yii2:使用 Pjax POST 方法和分页在 Gridview 中搜索

    我是 yii2 的初学者 尝试在搜索按钮上使用 Pjax 来搜索 Gridview 中的字段 我已经使用 GET 方法完成此操作 但我想使用 POST 方法完成此操作 那么我该如何使用 Yii2 Pjax post 方法 和分页来做到这一点
  • 为什么需要将nginx中的$args重定向到index.php?

    许多 PHP 框架建议将其添加到 nginx location try files uri index php is args args 执行index php在所有 HTTP 请求上 为什么我需要 is args args 我觉得 arg
  • 表头在 php 中的 for 循环中重复

    我正在尝试从数据库创建排行榜 我将数据打印在列表中 当我尝试将此数据放入 html 表中时 标题在每次数据输入后都会重复 这是 for 循环导致的 但我不知道如何只打印一次标题 然后将数据插入到每一行中 任何帮助将不胜感激 代码和结果的屏幕
  • 在 PHP 中比较字符串的方式与 MySQL 相同

    我将 varchar 存储在 utf8 MySQL 表中并使用 utf8 general ci 排序规则 我在 varchar 上有一个唯一索引 我想在 PHP 中进行字符串比较 这相当于 MySQL 对索引所做的操作 一个具体的例子是 我
  • 使用 PHP 和 MySQL 的服务器端事件

    我正在使用 PHP 和 MySQL 构建一个 非常基本的 应用程序 该应用程序的目的是在网页上显示 实时 数据交易 这些交易来自于transactionsMySQL 数据库中的表 到目前为止 我可以在网页上检索并显示数据 不过我期待看到数据
  • 刷新页面后保留输入值

    我有一个带有输入字段的表单 该输入包含一个下拉菜单 从数据库中读取信息 如果用户输入值 并且当他到达下拉菜单时 他没有找到他想要的内容 他会转到另一个页面将此信息添加到下拉菜单 然后转到第一页继续输入信息 如果他转到另一个页面向下拉菜单添加
  • 在数据转换之前应用验证

    我想将从提交的用户数据中获得的文本字段转换为 Symfony2 中的对象 我使用 DataTransformer 来做到这一点 当我使用 NotEmpty 或 NotNull 等内置验证器或任何以标准方式内置的自定义验证器时 Symfony
  • PHP 中消息队列和工作系统的有效架构?

    我正在尝试了解我想要在 PHP 应用程序中实现的消息队列模型和作业 我的目标是卸载需要发送到多个第三方 API 的消息 数据 因此访问它们不会减慢客户端的速度 所以将数据发送到消息队列是理想的 我考虑过仅使用 Gearman 来保存 MQ
  • Elasticquent(ElasticSearch) Laravel 限制

    您好 我尝试使用 elasticSearch 查询获取所有结果 但如果 limit 值为 null 则仅返回 10 个结果 videos Video searchByQuery match gt field gt request gt fi
  • 我可以在类变量中添加没有指定值的 PHP 数组键吗?

    我目前正在努力通过IBM 关于 CakePHP 的教程 http www 128 ibm com developerworks edu os dw os php cake1 html 有一次我遇到了这段代码
  • Chrome 问题 - 视频流和会话冲突

    我在使用 javascript 和 PHP 实现视频时遇到问题 索引 php session start do other stuff include video php 视频 php
  • PHP - 递归搜索数组中的键和子键,成功时返回键['subkey]

    因此 我编写了一个函数 该函数可以在数组中深入搜索两个级别以查找键和子键对 基本上是在寻找key subkey 如果找到 则返回key subkey 我正在寻找一种以真正递归的方式执行此操作的方法 并根据需要进行尽可能多的深度搜索 直到到达
  • PHP:使用输入和输出参数(不是“INOUT”)调用 MySQL 存储过程

    我想从 PHP 调用 MySQL 中的存储过程 该过程需要输入and输出参数 not INOUT 参数 举一个简单的例子 假设我在 MySQL 中有以下存储过程 DELIMITER DROP PROCEDURE IF EXISTS test
  • FOSRestBundle:显示我的自定义异常消息

    我试图在 FOSRestBundle 中添加异常的自定义控制 但它似乎忽略了我的自定义消息 响应的状态代码正常 I have throw new HttpException 404 User id not found 但得到这个 json
  • 为什么 sql 字段名称中不应该包含逗号?

    人们一直告诉我列名中不应包含空格 我只是想知道 这是为什么 这是我为学校创建的一些数据库表遇到的问题 字段名称包括 Preble 和 Darke 相反 它们需要是 普雷布尔县 俄亥俄州 和 达克县 俄亥俄州 如果它们是行名称 我只需创建一个

随机推荐