基于微信支付文档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 {
//更新订单的状态等等操作...