微信卡包发放代金券

微信卡包发放代金券

接口文档:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter5_1_4.shtml

接入前准备:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter5_1_2.shtml

说明: 商户平台/API完成制券后,可使用发放代金券接口发券。通过调用此接口可发放指定批次给指定用户,发券 场景可以是小程序、H5,APP等

申请商户API证书帮助文档:https://kf.qq.com/faq/161222NneAJf161222U7fARv.html

请求url: https://api.mch.weixin.qq.com/v3/marketing/favor/users/{openid}/coupons
请求方式:post
请求参数:
在这里插入图片描述

在这里插入图片描述

签名:
商户需要使用自身的私钥对API URL、消息体等关键数据的组合进行SHA-256 with RSA签名。请求的签名信息通过HTTP头Authorization 传递,具体说明请见 签名生成指南。没有携带签名或者签名验证不通过的请求,都不会被执行,并返回401 Unauthorized 。

准备:
商户需要拥有一个微信支付商户号,并通过超级管理员账号登录商户平台,获取商户API证书。 商户API证书的压缩包中包含了签名必需的私钥和商户证书。

签名生成帮助文档:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml

构造签名串:
签名串一共有五行,每一行为一个参数。行尾以 \n(换行符,ASCII编码值为0x0A)结束,包括最后一行。如果参数本身以\n结束,也需要附加一个\n。
在这里插入图片描述

构造签名串:

string nonce_str = timestr(6, true, true, true, false, "xdm");
string timestrs = AutoPlanRun.ConvertDateTimeToStamp(DateTime.Now);
string xxurl = $"/v3/marketing/favor/users/{
      
      openid}/coupons";
string content = $"POST\n{
      
      xxurl}\n{
      
      timestrs}\n{
      
      nonce_str}\n{
      
      Contentjson}\n";
string p12path = @"D:\iis\qgtcc\zhengshu\1589845951_00000000_cert\apiclient_cert.p12";

计算签名:

 string signature = AutoPlanRun.Sign(content, p12path, stock_creator_mchid);

如何获取商户证书序列号?
请前往商户平台下载:https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F

签名信息:

   string Authorization = string.Format("WECHATPAY2-SHA256-RSA2048 mchid=\"{0}\",nonce_str=\"{1}\",signature=\"{2}\",timestamp=\"{3}\",serial_no=\"{4}\"",
                stock_creator_mchid, nonce_str, signature, timestrs, serial_no);

发起post请求:

string res = AutoPlanRun.PostHttptt(url, Contentjson, "application/json", Authorization);

私有证书签名:

 public static string Sign(string source, string pfxFilePath, string passwd)
        {
    
    
            X509Certificate2 cert = new X509Certificate2(pfxFilePath, passwd, X509KeyStorageFlags.Exportable);
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            string privatekey = cert.PrivateKey.ToXmlString(true);
            provider.FromXmlString(privatekey);
            byte[] abc = provider.SignData(Encoding.UTF8.GetBytes(source), new SHA256CryptoServiceProvider());
            Base64Encoder encoder = new Base64Encoder();
            MemoryStream stream = new MemoryStream();
            int a = encoder.Encode(abc, 0, abc.Length, stream);
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }

完结

猜你喜欢

转载自blog.csdn.net/weixin_49543015/article/details/125501953