WeChat card package issues vouchers

WeChat card package issues vouchers

Interface document: https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter5_1_4.shtml

Preparation before access: https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter5_1_2.shtml

Note: After the merchant platform/API completes the voucher generation, you can use the voucher issuance interface to issue vouchers. By calling this interface, designated batches can be issued to designated users. The coupon issuance scenario can be small programs, H5, APP, etc.

Help document for applying for merchant API certificate: https://kf.qq.com/faq/161222NneAJf161222U7fARv.html

Request url: https://api.mch.weixin.qq.com/v3/marketing/favor/users/{openid}/coupons
Request method: post
Request parameters:
Insert image description here

Insert image description here

Signature:
Merchants need to use their own private keys to SHA-256 with RSA to sign the combination of key data such as API URL and message body. The requested signature information is passed through the HTTP header Authorization. For details, please see the signature generation guide. Requests that do not carry a signature or fail signature verification will not be executed and 401 Unauthorized will be returned.

Preparation:
Merchant needs to have a WeChat payment merchant account, log in to the merchant platform through the super administrator account, and obtain the merchant API certificate. The compressed package of the merchant API certificate contains the private key and merchant certificate necessary for signature.

Signature generation help document: https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml

Construct a signature string:
There are five lines in the signature string, each line has a parameter. The end of the line ends with \n (newline character, ASCII encoding value 0x0A), including the last line. If the parameter itself ends with \n, a \n also needs to be appended.
Insert image description here

Construct signature string:

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";

Compute signature:

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

How to obtain the merchant certificate serial number?
Please go to the merchant platform to download: https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F

Signature information:

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

Initiate a post request:

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

Private certificate signature:

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

end

Guess you like

Origin blog.csdn.net/weixin_49543015/article/details/125501953