微信Naitve下单支付和回调

编程入门 行业动态 更新时间:2024-10-18 12:30:28

微信Naitve下单支付和<a href=https://www.elefans.com/category/jswz/34/1771356.html style=回调"/>

微信Naitve下单支付和回调

1.场景介绍:用户扫描商户展示在各种场景的二维码进行支付

目前已上线支付案例,商户可进行实际体验。

线下:家乐福超市、7-11便利店、上品折扣线下店等

线上:大众点评网站、携程网站、唯品会、美丽说网站等

商户后台系统先调用微信支付的统一下单接口,微信后台系统返回链接参数code_url,商户后台系统将code_url值生成二维码图片,用户使用微信客户端扫码后发起支付。注意:code_url有效期为2小时,过期后扫码不能再发起支付。

注意:商户后台只会得到支付成功的通知,支付失败的通知会堆积一起等下次支付成功一起发送。

2.下单支付

访问地址:

http://localhost:8080/pay?outtradeno=1001&totalfee=1
server:port: 8080
#微信支付信息配置
weixin:#应用IDappid: #商户IDpartner: #秘钥partnerkey: #支付回调地址,需要内网穿透notifyurl: 
    //应用ID@Value("${weixin.appid}")private String appid;//商户号@Value("${weixin.partner}")private String partner;//秘钥@Value("${weixin.partnerkey}")private String partnerkey;//支付回调地址@Value("${weixin.notifyurl}")private String  notifyurl;/*** 微信支付* @param parameterMap* @return* @throws Exception*/@RequestMapping("/pay")public void createNative(@RequestParam Map<String,String> parameterMap, HttpServletResponse response) throws Exception {//设置响应头为图片response.setContentType("image/png");try {//参数Map<String,String> paramMap=new HashMap<>();paramMap.put("appid",appid);paramMap.put("mch_id",partner);//随机字符串paramMap.put("nonce_str", WXPayUtil.generateNonceStr());paramMap.put("body","腾讯充值中心-QQ会员充值");//订单号paramMap.put("out_trade_no",parameterMap.get("outtradeno"));//交易金额,单位:分paramMap.put("total_fee",parameterMap.get("totalfee"));paramMap.put("spbill_create_ip","127.0.0.1");//交易结果回调地址paramMap.put("notify_url",notifyurl);paramMap.put("trade_type","NATIVE");//Map转成XML字符串,可以携带签名String xmlparameters=WXPayUtil.generateSignedXml(paramMap,partnerkey);//URL地址String url="";HttpClient httpClient=new HttpClient(url);//提交方式httpClient.setHttps(true);//提交参数httpClient.setXmlParam(xmlparameters);System.out.println(xmlparameters);//执行请求httpClient.post();//获取返回的数据String result = httpClient.getContent();//返回数据转成MapMap<String,String> resultMap=WXPayUtil.xmlToMap(result);String returnCode = resultMap.get("return_code");String resultCode = resultMap.get("result_code");if(returnCode.equalsIgnoreCase("SUCCESS")&&resultCode.equalsIgnoreCase("SUCCESS")) {String codeUrl = resultMap.get("code_url");//TODO 拿到codeUrl,写代码生成二维码System.out.println("codeUrl=" + codeUrl);int width = 300;int height = 300;//二维码的图片格式String format = "JPEG";Hashtable hints = new Hashtable();//内容所使用编码hints.put(EncodeHintType.CHARACTER_SET, "utf-8");BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl,BarcodeFormat.QR_CODE, width, height, hints);// response.setContentType("image/JPEG");MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());//存订单}else{//失败throw new Exception(result);}}catch(Exception e) {e.printStackTrace();}}
MatrixToImageWriter类生成图片二维码
package com.example.wechatpay.wepay;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxingmon.BitMatrix;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;public final class MatrixToImageWriter {private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;private MatrixToImageWriter() {}public static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);}}return image;}public static void writeToFile(BitMatrix matrix, String format, File file)throws IOException {BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, file)) {throw new IOException("Could not write an image of format " + format + " to " + file);}}public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)throws IOException {BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, stream)) {throw new IOException("Could not write an image of format " + format);}}public static void main(String[] args) throws Exception {String text = "www.baidu";int width = 300;int height = 300;String format = "gif";Hashtable hints = new Hashtable();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, width, height, hints);File outputFile = new File("d:"+ File.separator+"new.gif");MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);}
}
package com.example.wechatpay.wepay;import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;import javax.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;public class HttpClient {private String url;private Map<String, String> param;private int statusCode;private String content;private String xmlParam;private boolean isHttps;public boolean isHttps() {return isHttps;}public void setHttps(boolean isHttps) {this.isHttps = isHttps;}public String getXmlParam() {return xmlParam;}public void setXmlParam(String xmlParam) {this.xmlParam = xmlParam;}public HttpClient(String url, Map<String, String> param) {this.url = url;this.param = param;}public HttpClient(String url) {this.url = url;}public void setParameter(Map<String, String> map) {param = map;}public void addParameter(String key, String value) {if (param == null)param = new HashMap<String, String>();param.put(key, value);}public void post() throws ClientProtocolException, IOException {HttpPost http = new HttpPost(url);setEntity(http);execute(http);}public void put() throws ClientProtocolException, IOException {HttpPut http = new HttpPut(url);setEntity(http);execute(http);}public void get() throws ClientProtocolException, IOException {if (param != null) {StringBuilder url = new StringBuilder(this.url);boolean isFirst = true;for (String key : param.keySet()) {if (isFirst)url.append("?");elseurl.append("&");url.append(key).append("=").append(param.get(key));}this.url = url.toString();}HttpGet http = new HttpGet(url);execute(http);}private void setEntity(HttpEntityEnclosingRequestBase http) {if (param != null) {List<NameValuePair> nvps = new LinkedList<NameValuePair>();for (String key : param.keySet())nvps.add(new BasicNameValuePair(key, param.get(key)));http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));}if (xmlParam != null) {http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));}}private void execute(HttpUriRequest http) throws ClientProtocolException,IOException {CloseableHttpClient httpClient = null;try {if (isHttps) {SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {// 信任所有public boolean isTrusted(X509Certificate[] chain,String authType)throws CertificateException {return true;}}).build();SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();} else {httpClient = HttpClients.createDefault();}CloseableHttpResponse response = httpClient.execute(http);try {if (response != null) {if (response.getStatusLine() != null)statusCode = response.getStatusLine().getStatusCode();HttpEntity entity = response.getEntity();// 响应内容content = EntityUtils.toString(entity, Consts.UTF_8);}} finally {response.close();}} catch (Exception e) {e.printStackTrace();} finally {httpClient.close();}}public int getStatusCode() {return statusCode;}public String getContent() throws ParseException, IOException {return content;}}

3.回调接口

package com.example.wechatpay.service.impl;import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;import java.io.ByteArrayInputStream;
import java.io.InputStream;@Configuration
public class WXPayConfigImpl implements WXPayConfig {//应用ID@Value("${weixin.appid}")private String appid;//商户号@Value("${weixin.partner}")private String partner;//秘钥@Value("${weixin.partnerkey}")private String partnerkey;private byte[] certData;@Overridepublic String getAppID() {return appid;}@Overridepublic String getMchID() {return partner;}@Overridepublic String getKey() {return partnerkey;}@Overridepublic InputStream getCertStream() {return   new ByteArrayInputStream(this.certData);}@Overridepublic int getHttpConnectTimeoutMs() {return 0;}@Overridepublic int getHttpReadTimeoutMs() {return 0;}
}
package com.example.wechatpay.service.impl;import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;import java.io.ByteArrayInputStream;
import java.io.InputStream;@Configuration
public class WXPayConfigImpl implements WXPayConfig {//应用ID@Value("${weixin.appid}")private String appid;//商户号@Value("${weixin.partner}")private String partner;//秘钥@Value("${weixin.partnerkey}")private String partnerkey;private byte[] certData;@Overridepublic String getAppID() {return appid;}@Overridepublic String getMchID() {return partner;}@Overridepublic String getKey() {return partnerkey;}@Overridepublic InputStream getCertStream() {return   new ByteArrayInputStream(this.certData);}@Overridepublic int getHttpConnectTimeoutMs() {return 0;}@Overridepublic int getHttpReadTimeoutMs() {return 0;}
}

更多推荐

微信Naitve下单支付和回调

本文发布于:2024-03-23 16:35:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1740394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   下单   Naitve

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!