微信扫码支付生成二维码字符串

流程:前端商品请求------》后台调用微信扫码支付接口(生成二维码字符串)--------------》将字符串返回,利用js再次转换为二维码图片-------》展示------》支付宝app扫码支付------》开启线程不断查询订单状态------》订单(二维码)失效—》交易成功或者失败。
准备工作:
1.需要去微信官网下载java demo
2.修改微信config
package com.shuaishoubao.dcj.wx;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class WXPayConfigImpl extends WXPayConfig {
private byte[] certData;
private static WXPayConfigImpl INSTANCE;

public final static String NOTIFY_URL = "http://***/wx-pay/callback";
// public final static String NOTIFY_URL =
// "https://api.mch.weixin.qq.com/pay/unifiedorder";

// private WXPayConfigImpl() throws Exception {
// //获取证书
// File file = new ClassPathResource("apiclient_cert.p12").getFile();
// InputStream certStream = new FileInputStream(file);
// this.certData = new byte[(int) file.length()];
// certStream.read(this.certData);
// certStream.close();
// }

public static WXPayConfigImpl getInstance() throws Exception {
	if (INSTANCE == null) {
		synchronized (WXPayConfigImpl.class) {
			if (INSTANCE == null) {
				INSTANCE = new WXPayConfigImpl();
			}
		}
	}
	return INSTANCE;
}

public String getAppID() {
	return "必填";
}

public String getMchID() {
	return "必填";
}

public String getKey() {
	return "必填";
}

public InputStream getCertStream() {
	ByteArrayInputStream certBis;
	certBis = new ByteArrayInputStream(this.certData);
	return certBis;
}

public int getHttpConnectTimeoutMs() {
	return 2000;
}

public int getHttpReadTimeoutMs() {
	return 10000;
}

// @Override
// protected IWXPayDomain getWXPayDomain() {
// return null;
// }

public IWXPayDomain getWXPayDomain() {
	return WXPayDomainSimpleImpl.instance();
}

public String getPrimaryDomain() {
	return "api.mch.weixin.qq.com";
}

public String getAlternateDomain() {
	return "api2.mch.weixin.qq.com";
}

// @Override
// public int getReportWorkerNum() {
// return 1;
// }
//
// @Override
// public int getReportBatchSize() {
// return 2;
// }

}
//------------------------------------------------------
java代码正式开始:
/**
*
* @param 微信支付
* @return
* @throws Exception
/
public Map<String, String> payByWX(User loginUser,String outTradeNo,Map<String,Object> map) throws Exception {
Map<String, String> resp=null;
/
*
* 1个月 58元 3个月174元 1年696元再送3个月
*/
String body =(String) map.get(“body”);
String totalAmount =(String) map.get(“totalAmount”);
String goodsId = (String) map.get(“goodsId”);
String time2 = (String) map.get(“time2”);
String months = (String) map.get(“months”);
//将String类型的totalAmount转换为long
int p = (Integer.parseInt(totalAmount))*100;//单位元 ‘元’转化为’分’
// long price = p;
long price = 1L;//代表0.01元,用于测试

	Map<String,String> data = new HashMap<String,String>();
	String timeExpire = UtilDate.getOrderExpireTime(2*60*1000L);
	WXPayConfigImpl config= new WXPayConfigImpl();
    WXPay wxpay = new WXPay(config);
    data.put("body", UtilsConstant.SUBJECT);//描述
    data.put("out_trade_no", outTradeNo);//订单号
    data.put("time_expire", timeExpire);//设置订单失效时间 2分钟
    data.put("device_info", "");//设备类型
    data.put("fee_type", "CNY");//人民币
    data.put("total_fee", ""+price);//金额必须为整  单位为分
    data.put("spbill_create_ip", "192.168.42.31");//本机ip
    //支付成功后回调地址,可以不要
    //data.put("notify_url", "http://www.example.com/wxpay/notify");//
    data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
    data.put("product_id", goodsId);//商户根据自己业务传递的参数

    try {
        resp = wxpay.unifiedOrder(data);//调用方法返回二维码字符串
        System.out.println(resp);
    } catch (Exception e) {
        e.printStackTrace();
    }
	return resp;
}

//由二维码字符串-------》二维码图片 请参考上一篇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值