您当前的位置: 首页 > 知识百科 > 发红包的微信小程序怎么开发?

发红包的微信小程序怎么开发?

时间:2023-07-01 14:05 阅读数:169 人阅读 分类:知识百科

  已经有很多人表示小程序既能满足不同的需求,又不占手机内存,功能的确强大。但是更多的人想利用小程序发红包,但是发红包的微信小程序需要开发与红包相关的小程序接口,以下是开发步骤。

  分装 红包工具类 :

  package com.tepusoft.web.weixin.utils;

  import java.io.BufferedReader;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.InputStreamReader;

  import java.security.KeyStore;

  import java.security.NoSuchAlgorithmException;

  import java.text.SimpleDateFormat;

  import java.util.Date;

  import java.util.Iterator;

  import java.util.Map;

  import java.util.Map.Entry;

  import java.util.Random;

  import java.util.Set;

  import java.util.SortedMap;

  import java.util.TreeMap;

  import java.util.UUID;

  import javax.ssl.SSLContext;

  import org.apachemons.codec.digest.DigestUtils;

  import org.apache..HttpEntity;

  import org.apache..client.methods.CloseableHttpResponse;

  import org.apache..client.methods.HttpPost;

  import org.apache..conn.ssl.SSLConnectionSocketFactory;

  import org.apache..conn.ssl.SSLContexts;

  import org.apache..entity.StringEntity;

  import org.apache..impl.client.CloseableHttpClient;

  import org.apache..impl.client.HttpClients;

  import org.apache..util.EntityUtils;

  /**

  * @author wangiegie

  * @date 2015年10月19日下午2:07:11

  * @description

  */

  public class HongBaoUtil {

  public static final String MCH_ID = ""; // 商户号

  public static final String WXAPPID = ""; // 公众账号appid

  public static final String NICK_NAME = "潍坊特普软件开发有限公司"; // 提供方名称

  public static final String SEND_NAME = "潍坊特普软件"; // 商户名称

  public static final int MIN_VALUE = ; // 红包最小金额 单位:分

  public static final int MAX_VALUE = ; // 红包最大金额 单位:分

  public static final int TOTAL_NUM = 1; // 红包发放人数

  public static final String WISHING = "生日快乐"; // 红包祝福语

  public static final String CLIENT_IP = "182.41.214.82"; // 调用接口的机器IP

  public static final String ACT_NAME = "??"; // 活动名称

  public static final String REMARK = "红包测试"; // 备注

  public static final String KEY = ""; // 秘钥

  public static final int FAIL = 0; // 领取失败

  public static final int SUCCESS = 1; // 领取成功

  public static final int LOCK = 2; // 已在余额表中锁定该用户的余额,防止领取的红包金额大于预算

  /**

  * 对请求参数名ASCII码从小到大排序后签名

  *

  * @param params

  */

  public static void sign(SortedMap params) {

  Set> entrys = params.entrySet();

  Iterator> it = entrys.iterator();

  StringBuffer result = new StringBuffer();

  while (it.hasNext()) {

  Entry entry = it.next();

  result.append(entry.getKey()).append("=").append(entry.getValue()).append("&");

  }

  result.append("key=").append(KEY);

  params.put("sign", DigestUtils.md5Hex(result.toString()));

  }

  /**

  * 生成提交给微信服务器的xml格式参数

  *

  * @param params

  * @return

  */

  public static String getRequestXml(SortedMap params) {

  StringBuffer sb = new StringBuffer();

  sb.append("");

  return sb.toString();

  }

  /**

  * 创建map

  *

  * @param billNo

  * @param openid

  * @param userId

  * @param amount

  * @return

  */

  public static SortedMap createMap(String openid, String userId, int amount) {

  SortedMap params = new TreeMap();

  params.put("wxappid", WXAPPID);

  params.put("nonce_str", createNonceStr());

  params.put("mch_billno", createBillNo(userId));

  params.put("mch_id", MCH_ID);

  params.put("nick_name", NICK_NAME);

  params.put("send_name", SEND_NAME);

  params.put("re_openid", openid);

  params.put("total_amount", amount + "");

  params.put("min_value", amount + "");

  params.put("max_value", amount + "");

  params.put("total_num", TOTAL_NUM + "");

  params.put("wishing", WISHING);

  params.put("client_ip", CLIENT_IP);

  params.put("act_name", ACT_NAME);

  params.put("remark", REMARK);

  return params;

  }

  /**

  * 生成随机字符串

  *

  * @return

  */

  public static String createNonceStr() {

  return UUID.randomUUID().toString().toUpperCase().replace("-", "");

  }

  /**

  * 生成商户订单号

  *

  * @param mch_id

  * 商户号

  * @param userId

  * 该用户的userID

  * @return

  */

  public static String createBillNo(String userId) {

  // 组成: mch_id+yyyymmdd+10位一天内不能重复的数字

  // 10位一天内不能重复的数字实现方法如下:

  // 因为每个用户绑定了userId,他们的userId不同,加上随机生成的(10-length(userId))可保证这10位数字不一样

  Date dt = new Date();

  SimpleDateFormat df = new SimpleDateFormat("yyyymmdd");

  String nowTime = df.format(dt);

  int length = 10 - userId.length();

  return MCH_ID + nowTime + userId + getRandomNum(length);

  }

  /**

  * 生成特定位数的随机数字

  *

  * @param length

  * @return

  */

  private static String getRandomNum(int length) {

  String val = "";

  Random random = new Random();

  for (int i = 0; i < length; i++) {

  val += String.valueOf(random.nextInt(10));

  }

  return val;

  }

  /**

  * post提交到微信小程序服务器

  *

  * @param requestXML

  * @param instream 传入的在微信支付的PKCS12证书的位置

  * @return

  * @throws NoSuchAlgorithmException

  * @throws CertificateException

  * @throws IOException

  * @throws KeyManagementException

  * @throws UnrecoverableKeyException

  * @throws KeyStoreException

  */

  public static String post(String requestXML, InputStream instream) throws Exception {

  KeyStore keyStore = KeyStore.getInstance("PKCS12");

  try {

  keyStore.load(instream, MCH_ID.toCharArray());

  } finally {

  instream.close();

  }

  SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, MCH_ID.toCharArray()).build();

  SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,

  SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

  CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(sslsf).build();

  String result = "";

  try {

  HttpPost Post = new HttpPost("api.mch.weixin.qq/mmpaymkttransfers/sendredpack");

  StringEntity reqEntity = new StringEntity(requestXML, "utf-8"); // 如果此处编码不对,可能导致客户端签名跟微信的签名不一致

  reqEntity.setContentType("application/x--form-urlencoded");

  Post.setEntity(reqEntity);

  CloseableHttpResponse response = client.execute(Post);

  try {

  HttpEntity entity = response.getEntity();

  if (entity != null) {

  BufferedReader bufferedReader = new BufferedReader(

  new InputStreamReader(entity.getContent(), "UTF-8"));

  String text;

  while ((text = bufferedReader.readLine()) != null) {

  result += text;

  }

  }

  EntityUtils.consume(entity);

  } finally {

  response.close();

  }

  } finally {

  client.close();

  }

  return result;

  }

  }

  小程序调用:

  @Test

  public void testHongBao() throws Exception {

  SortedMap sortedMap = HongBaoUtil.createMap(openId, userId, money);

  HongBaoUtil.sign(sortedMap);

  String postXML = HongBaoUtil.getRequestXml(sortedMap);

  FileInputStream instream = new FileInputStream(new File("证书文件地址"));

  HongBaoUtil.post(postXML, instream);

  }

  好咯 ...别被微信支付后台的那么多的小程序配置和参数吓到 ,发红包的微信小程序开发接口就是这么简单 ~

  大家看完以上资料,应该都了解了发红包的微信小程序首先需要开发小程序红包接口,上文中详细介绍了开发所需要的代码,希望大家都能开发出发红包的微信小程序。如果还有不懂的地方请关注微信小程序商店。

  

  小程序微信支付宝红包哪个好?

  微信小程序如何实现语音输入发红包?

  小程序发红包怎么设置?发红包小程序怎么使用?