微信支付(JS API) JAVA

基于微信支付文档V3.3.7进行编码,使用JS API(网页内)并测试通过,应用在项目中。

Form:

public class PayForm extends BaseForm{
	private String code;		      //通过code换取微信的openid,谁支付的
	private Integer transactionID;    //交易ID
	private String appId;             //公众账号ID
	private String timeStamp;         //时间戳
	private String nonceStr;          //随机字符串
	private String packages;          //订单详情扩展字符串
	private String paySign;		      //签名
	private String openid;		      //微信openid
	private Integer masterOrderID;    //订单ID
    //set get
}

请求支付的Action:

@ParentPackage("main")
@Namespace("/pay")
@Controller
public class PayAction extends BaseAction{
     private PayForm form;
     @Resource
     private TransactionService transactionService;
     @Action(value = "weChatPay",
 			results = { 
 			@Result(name = SUCCESS, location = "/html/transaction/jsp/weChatPay.jsp"),
 			@Result(name =  ERROR , location = "/html/common/errors/sys_error.jsp")}
     )
     public String weChatPay(){
    	SystemLogger.info("用户请求支付订单号:"+form.getTransactionID());
 		if (StringUtils.isNotEmpty(form.getCode())) {
 			String url =  WeixinUtil.getUrl().replace("CODE", form.getCode());
 			String openid ="";
 			try {
 				Map<String, Object> map = JsonUtil.getMapByUrl(url); 
 				openid = map.get("openid").toString();
 				form.setOpenid(openid);
 			} catch (Exception e) {
 				SystemLogger.info("用户请求支付订单号:"+form.getTransactionID()+"时拉取微信用户信息失败");
 			}
 			try {
				transactionService.createWeChatRequest(form);
				return SUCCESS;
			} catch (BusinessException e) {
				SystemLogger.error(e.getMessage());
				form.setMessage(e.getMessage());
				return ERROR;
			}
 		}else{
 			SystemLogger.info("用户请求支付订单号:"+form.getTransactionID()+"的链接为非法链接,已拒绝");
 			form.setMessage("请通过合法链接进行订单支付");
			return ERROR;
 		}
     }
     @Override
     public Object getModel() {
	  if(form==null){
	  	  form = new PayForm();
	  }
	  return form;
     }
}

微信支付结果回调请求的Action:

@ParentPackage("main")
@Namespace("/pay")
@Controller
public class NotifyAction {
        @Resource
	private TransactionService transactionService;
	@Action(value = "notify",
 			results = { 
 			@Result(name = "success", type = "plainText",params = { "charSet", "UTF-8"})},
 			interceptorRefs = { @InterceptorRef("defaultStack")}
        )
     public String weChatNotify(){
		Map<String, String> requestMap = null;
		SortedMap<String, String> returnMap = new TreeMap<String, String>();
		String requestXML = "";
		try {
			requestMap = TenPayUtil.parseXml(ServletActionContext.getRequest());
			SystemLogger.info("接收到的微信支付回调通知:"+requestMap.toString());
			SortedMap<String, String> map = new TreeMap<String, String>();
			map.putAll(requestMap);
			String weixinSign = map.get("sign");
			map.remove("sign");
			String sign = TenPayUtil.sign(map, WeixinUtil.partnerKey);
			if(!sign.equals(weixinSign)){
				SystemLogger.info("签名失败");
				returnMap.put("return_code", "FAIL");
				returnMap.put("return_msg", "签名失败");
				requestXML = TenPayUtil.getRequestXml(returnMap);
			}else{
				try {
					//更新订单的状态等等操作...
		
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值