最近做一个PC端网上商城,需要微信扫码支付,几经摸索,终于搞出来了,现将代码贴下,以供参考!
1.扫码支付演示
看起来很简单,其实实现起来也不难,soEasy!
2.将生成二维码的js导入到项目
jquery.qrcode.min.js
3.在后台建一个微信参数配置
支付链接: http://www.xxxxxx.com/mobile/ec/pay/tp006MobileEcPaymentAction_payNotify.action
4.代码实现
4.1*微支付实体类*WxPayConfig*
public class WxPayConfig extends BaseEntity {
private static final long serialVersionUID = 1L;
/** appid */
private String appId;
/** 商户号 */
private String mchId;
/** 订单名称 */
private String body;
/** 设备信息 */
private String deviceInfo;
/** 交易类型 */
private String tradeType;
/** 支付链接 */
private String notifyUrl;
/** 店铺服务器ip */
private String spbillCreateIp;
/** 支付秘钥 */
private String privateKey;
/** 商户证书 */
private String securityCertificate;
/** 退款资金来源
* REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
* REFUND_SOURCE_RECHARGE_FUNDS ---可用余额退款
*/
private String refundAccount;
public WxPayConfig(){}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getMchId() {
return mchId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getDeviceInfo() {
return deviceInfo;
}
public void setDeviceInfo(String deviceInfo) {
this.deviceInfo = deviceInfo;
}
public String getTradeType() {
return tradeType;
}
public void setTradeType(String tradeType) {
this.tradeType = tradeType;
}
public String getNotifyUrl() {
return notifyUrl;
}
public void setNotifyUrl(String notifyUrl) {
this.notifyUrl = notifyUrl;
}
public String getSpbillCreateIp() {
return spbillCreateIp;
}
public void setSpbillCreateIp(String spbillCreateIp) {
this.spbillCreateIp = spbillCreateIp;
}
public String getPrivateKey() {
return privateKey;
}
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public String getSecurityCertificate() {
return securityCertificate;
}
public void setSecurityCertificate(String securityCertificate) {
this.securityCertificate = securityCertificate;
}
public String getRefundAccount() {
return refundAccount;
}
public void setRefundAccount(String refundAccount) {
this.refundAccount = refundAccount;
}
public String weixinJsPayPrepare(){
if(StrUtils.objectIsNotNull(id)){
try{
Object cusObj = getSession().getAttribute(CustomerConstant.CUSTOMER);
if(null != cusObj && cusObj instanceof EcCustomer){
microShopId = getSessionString(PcMicroShopConstant.MMS_MICROSHOP_ID);
if(StrUtils.objectIsNull(microShopId)){
return "404";
}
microShop = microShopService.findEntityById(MicroShop.class, microShopId);
WxPayConfig wxPayConfig = microShopService.findWxPayConfigByMicroShopId(microShopId);
if(null == wxPayConfig || StrUtils.objectIsNull(wxPayConfig.getAppId()) || StrUtils.objectIsNull(wxPayConfig.getMchId()) || StrUtils.objectIsNull(wxPayConfig.getPrivateKey())){
System.out.println("error:后台微信支付配置数据不全!");
}else{
/*id = id.replaceAll("\r|\n", "");*/
order = microShopService.findEntityById(EcOrder.class, id);
if(null != order){
Double totalPrice = order.getTotalPrice();
if(totalPrice > 0){
// 账号信息
String appId = wxPayConfig.getAppId();
//商业号
String mch_id = wxPayConfig.getMchId();
//支付秘钥
//String key = wxPayConfig.getPrimaryKey();
String key = "7db5da60334a97c301323b46e57b1498";
String spbill_create_ip = getIpAddr(getRequest());
Double order_price = order.getTotalPrice();
int price =(int) (order_price *100);
String out_trade_no = order.getCode();
String nonce_str = getRandomStringByLength(32);
/** 设置url 必填,不能修改 */
StringBuffer rn_url = getRequest().getRequestURL();
String contextUrl = rn_url.delete(rn_url.length() - getRequest().getRequestURI().length(), rn_url.length()).append(getRequest().getContextPath()).toString();
//回调接口
String notify_url = contextUrl +"/pc/ec/pay/tp002PcEcPaymentActiondian!getPayBack.action";
String trade_type = "NATIVE";
SortedMap
$(function(){
var str = $("#urlCode").val();
str = toUtf8(str);
$("#code").qrcode({
render: "table",
width: 300,
height:300,
text: str
});
})
setInterval("checkPay()",1000*1);
function checkPay(){
var orderId = $("#orderId").val();
$.ajax({
url : '${base}/pc/ec/pay/tp002PcEcPaymentAction_checkPay.action',
cache : false,
data : {
"id" : orderId
},
success : function(result) {
var r = result.split(":");
if(Number(r[0]) > 0){
/*支付成功跳转页面*/
document.location.href = "${base}/pc/ec/order/tp002PcEcOrderAction_goEcOrderSuccess.action?orderId="+orderId;
}
}
})
}
function toUtf8(str) {
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
注意引入jquery.qrcode.min.js
以上就是微信扫码支付的全部代码,我也该休息会了