微信APP支付(IOS手机端+java后台)版

 更新时间:2019年05月05日 09:37:11   作者:只要你能好  
这篇文章主要为大家详细介绍了微信APP支付(IOS手机端+java后台)版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

0.介绍预览

针对需要在IOS手机上接入原生微信支付场景,调用微信进行支付。如图:

1.资料准备

1.1 账号注册

打开https://open.weixin.qq.com,注册微信开放平台开发者账号

1.2 开发者认证

登录,进入账号中心,进行开发者资质认证。

1.3 注册应用

认证完成后,进入管理中心,新建移动应用。填写应用资料,其中android版应用签名可通过扫码安装温馨提供的应用获得,详细参考微信文档。创建完成后点击查看,申请开通微信支付。一切准备就绪!

2.Java后台开发

添加依赖

<!-- 微信支付依赖 -->
<dependency>
 <groupId>org.xmlpull</groupId>
 <artifactId>xmlpull</artifactId>
 <version>1.1.3.1 </version>
</dependency>
<dependency>
 <groupId>net.sf.json-lib</groupId>
 <artifactId>json-lib</artifactId>
 <version>2.3</version>
 <classifier>jdk15</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
 <groupId>com.thoughtworks.xstream</groupId>
 <artifactId>xstream</artifactId>
 <version>1.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ning/async-http-client -->
<dependency>
 <groupId>com.ning</groupId>
 <artifactId>async-http-client</artifactId>
 <version>1.8.13</version>
</dependency>

生成统一订单

@RequestMapping(value="/pay/wxpay/params",produces="application/json;charset=utf-8")
 @ResponseBody
 public String signprams(HttpServletRequest request){
  String res = "{code:404}";
  try{
  // 充值金额
   String account = request.getParameter("account");
  // 用户id
   String sid = request.getParameter("sid");
   String subject = "订单标题";
   String body = "订单描述";
 
   int acc = (int) (Double.valueOf(account) * 100);
   String appid = "您的APPID";
   String out_trade_no = "生成您的订单号";
  // 生成订单数据
   SortedMap<String, String> payMap = genOrderData(request, subject, body, acc, appid, out_trade_no);
 
   savePayLog(out_trade_no,account,sid,body,payMap.get("paySign"),nid,2);
 
   // 4.返回数据
   res = buildPayRes(payMap,out_trade_no);
  }catch (Exception e){
   e.printStackTrace();
   res = "{code:500}";
  }
  return res;
 }
 
 private SortedMap<String, String> genOrderData(HttpServletRequest request, String subject, String body,
          int acc, String appid, String out_trade_no)
   throws IOException, ExecutionException, InterruptedException, XmlPullParserException {
  SortedMap<String, String> paraMap = new TreeMap<String, String>();
  paraMap.put("appid", appid);
  paraMap.put("attach", subject);
  paraMap.put("body", body);
  paraMap.put("mch_id", "您的商户id,到商户平台查看");
  paraMap.put("nonce_str", create_nonce_str());
  paraMap.put("notify_url", "http://pay.xxxxx.com/pay/wxpay/notify.htm ");// 此路径是微信服务器调用支付结果通知路径
  paraMap.put("out_trade_no", out_trade_no);
  paraMap.put("spbill_create_ip", request.getRemoteAddr());
  paraMap.put("total_fee", acc+"");
  paraMap.put("trade_type", "APP");
  String sign = createSign(paraMap);
  paraMap.put("sign", sign);
  // 统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder 
  String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  String xml = getRequestXml(paraMap);
  String xmlStr = HttpKit.post(url, xml);
 
  // 预付商品id
  String prepay_id = "";
  if (xmlStr.indexOf("SUCCESS") != -1) {
   Map<String, String> map = WXRequestUtil.doXMLParse(xmlStr);
   prepay_id = (String) map.get("prepay_id");
  }
 
  SortedMap<String, String> payMap = new TreeMap<String, String>();
  payMap.put("appid", appid);
  payMap.put("partnerid", "您的商户id,到商户平台查看");
  payMap.put("prepayid", prepay_id);
  payMap.put("package", "Sign=WXPay");
  payMap.put("noncestr", create_nonce_str());
  payMap.put("timestamp", WXRequestUtil.create_timestamp());
  String paySign = createSign(payMap);
  payMap.put("paySign", paySign);
  return payMap;
 }
 
 //请求xml组装 
 public static String getRequestXml(SortedMap<String,String> parameters){ 
  String sign = "";
   StringBuffer sb = new StringBuffer(); 
   sb.append("<xml>"); 
   Set es = parameters.entrySet(); 
   Iterator it = es.iterator(); 
   while(it.hasNext()) { 
    Map.Entry entry = (Map.Entry)it.next(); 
    String key = (String)entry.getKey(); 
    String value = (String)entry.getValue(); 
//    if ("attach".equalsIgnoreCase(key)||"body".equalsIgnoreCase(key)||"sign".equalsIgnoreCase(key)) { 
//     sb.append("<"+key+">"+value+"</"+key+">"); 
//    }
    if ("sign".equalsIgnoreCase(key)){
    sign = "<"+key+">"+value+"</"+key+">";
    }else { 
     sb.append("<"+key+">"+value+"</"+key+">"); 
    } 
   } 
   sb.append(sign); 
   sb.append("</xml>"); 
   return sb.toString(); 
  } 
 
 //生成签名 
 public String createSign(SortedMap<String,String> parameters){ 
   StringBuffer sb = new StringBuffer(); 
   Set es = parameters.entrySet(); 
   Iterator it = es.iterator(); 
   while(it.hasNext()) { 
    Map.Entry entry = (Map.Entry)it.next(); 
    String k = (String)entry.getKey(); 
    Object v = entry.getValue(); 
    if(null != v && !"".equals(v) 
      && !"sign".equals(k) && !"key".equals(k)) { 
     sb.append(k + "=" + v + "&"); 
    } 
   } 
   sb.append("key=" + WXConfig.APP_PERTNER_KEY); 
   System.out.println(sb.toString());
   String sign = MD5Utils.MD5Encode(sb.toString(),"UTF-8").toUpperCase(); 
   return sign; 
  } 
 
 
 public String create_nonce_str() {
 String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 String res = "";
 for (int i = 0; i < 32; i++) {
 Random rd = new Random();
 res += chars.charAt(rd.nextInt(chars.length() - 1));
 }
 return res;
 }

3.IOS客户端开发

导入微信开发包

添加URL Types

在AppDelegate.m中注册应用

#import "AppDelegate.h"
#import "XSTabBarViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
 
@interface AppDelegate ()<WXApiDelegate>
 
@end
 
@implementation AppDelegate
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Override point for customization after application launch.
 
// [NSThread sleepForTimeInterval:2.0];
 
 // 进入主控制器
 self.window = [[UIWindow alloc] init];
 self.window.frame = [UIScreen mainScreen].bounds;
 self.window.rootViewController = [[XSTabBarViewController alloc] init];
 
 [self.window makeKeyAndVisible];
 
 //向微信注册应用。
 [WXApi registerApp:@"wxfb96c2a9b531be26"];
 
 return YES;
}
 
-(void) onResp:(BaseResp*)resp
{
//  NSLog(@" ----onResp %@",resp);
 /*
  ErrCode ERR_OK = 0(用户同意)
  ERR_AUTH_DENIED = -4(用户拒绝授权)
  ERR_USER_CANCEL = -2(用户取消)
  code 用户换取access_token的code,仅在ErrCode为0时有效
  state 第三方程序发送时用来标识其请求的唯一性的标志,由第三方程序调用sendReq时传入,由微信终端回传,state字符串长度不能超过1K
  lang 微信客户端当前语言
  country 微信用户当前国家信息
  */
 if ([resp isKindOfClass:[SendAuthResp class]]) //判断是否为授权请求,否则与微信支付等功能发生冲突
 {
  SendAuthResp *aresp = (SendAuthResp *)resp;
  if (aresp.errCode== 0)
  {
   //   NSLog(@"code %@",aresp.code);
   [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatDidLoginNotification" object:self userInfo:@{@"code":aresp.code}];
  }
 }else{ // 支付请求回调
  //支付返回结果,实际支付结果需要去微信服务器端查询
  NSString *strMsg = [NSString stringWithFormat:@"支付结果"];
  NSString *respcode = @"0";
  switch (resp.errCode) {
   case WXSuccess:
    strMsg = @"支付结果:成功!";
    //    NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
    respcode = @"1";
    break;
   default:
    strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
    //    NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
    respcode = @"0";
    break;
  }
  [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatDidPayNotification" object:self userInfo:@{@"respcode":respcode}];
 }
}
 
 
//iOS 9.0 之前的处理方法不保证正确,如有错误还望指正
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
 NSLog(@"iOS 9.0 之前");
 return [self applicationOpenURL:url];
}
 
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
 NSLog(@"iOS 9.0 之后");
 return [self applicationOpenURL:url];
}
 
- (BOOL)applicationOpenURL:(NSURL *)url
{
 if([[url absoluteString] rangeOfString:@"wxfb96c2a9b531be26://pay"].location == 0){
  return [WXApi handleOpenURL:url delegate:self];
 }
 if ([url.host isEqualToString:@"safepay"])
 {
  [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:nil];
  return YES;
 }
 return YES;
}
 
}

在需要支付的Controller中接受微信支付通知

- (void)viewDidLoad {
 [super viewDidLoad];
 
 // 接受微信支付通知
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wechatDidPayNotification:) name:@"wechatDidPayNotification" object:nil];
}

向服务器端获取统一订单,并拉起微信进行支付

-(void)weixinPay
{
 NSString *userUrlStr = [NSString stringWithFormat:@"%@?sid=%@&account=%@&desc=%@", WX_PREPAY_URL, self.student.sid,self.payJinE,self.student.nid];
 NSURL *url = [NSURL URLWithString:userUrlStr];
//  NSLog(@"userUrlStr = %@", userUrlStr);
 
 NSURLRequest *request = [NSURLRequest requestWithURL:url];
 AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
 
 [MBProgressHUD showMessage:@"跳转中,请稍候"];
 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
  [MBProgressHUD hideHUD];
  
//   NSLog(@"微信支付的response = %@", operation.responseString);
  NSData *JSONData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
  NSDictionary *userDict = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
  
  // 调用微信支付
  PayReq *request = [[PayReq alloc] init];
  /** 商家向财付通申请的商家id */
  request.partnerId = [userDict objectForKey:@"partnerid"];
  /** 预支付订单 */
  request.prepayId= [userDict objectForKey:@"prepayid"];
  /** 商家根据财付通文档填写的数据和签名 */
  request.package = [userDict objectForKey:@"package"];
  /** 随机串,防重发 */
  request.nonceStr= [userDict objectForKey:@"noncestr"];
  /** 时间戳,防重发 */
  request.timeStamp= [[userDict objectForKey:@"timestamp"] intValue];
  /** 商家根据微信开放平台文档对数据做的签名 */
  request.sign= [userDict objectForKey:@"sign"];
  self.sign = request.sign;
  self.ordnum = [userDict objectForKey:@"ordnum"];
  
 
  [WXApi sendReq: request];
 }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  [MBProgressHUD hideHUD];
  NSLog(@"发生错误!%@",error);
 }];
 NSOperationQueue *queue = [[NSOperationQueue alloc] init];
 [queue addOperation:operation];
 
}
// 微信支付结果
-(void)wechatDidPayNotification:(NSNotification *)notification
{
// NSLog(@"wechatDidPayNotification");
 NSDictionary *nameDictionary = [notification userInfo];
 NSString *respcode = [nameDictionary objectForKey:@"respcode"];
 if([respcode isEqualToString:@"1"]){
  // 支付成功,更新用户信息
  [self payDidFinish];
 }else{
  // 支付失败,
  [self setupAlertControllerWithTitle:@"微信支付结果" messge:@"本次支付未完成,您可以稍后重试!" confirm:@"好的"];
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • MyBatis 三表外关联查询的实现(用户、角色、权限)

    MyBatis 三表外关联查询的实现(用户、角色、权限)

    这篇文章主要介绍了MyBatis 三表外关联查询的实现(用户、角色、权限),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Java中自定义LRU缓存详解

    Java中自定义LRU缓存详解

    这篇文章主要介绍了Java中自定义LRU缓存详解,基于LRU算法的缓存系统,可以在达到缓存容量上限时,清理最近最少使用的数据,为新的数据的插入腾出空间,需要的朋友可以参考下
    2023-09-09
  • SpringCloud创建多模块项目的实现示例

    SpringCloud创建多模块项目的实现示例

    ,Spring Cloud作为一个强大的微服务框架,提供了丰富的功能和组件,本文主要介绍了SpringCloud创建多模块项目的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • PowerJob的OmsLogHandler工作流程源码解析

    PowerJob的OmsLogHandler工作流程源码解析

    这篇文章主要为大家介绍了PowerJob的OmsLogHandler工作流程源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Java基于分治法实现的快速排序算法示例

    Java基于分治法实现的快速排序算法示例

    这篇文章主要介绍了Java基于分治法实现的快速排序算法,结合实例形式分析了java基于分治法的快速排序相关实现技巧,代码中备有较为详细的注释说明便于理解,需要的朋友可以参考下
    2017-12-12
  • java的IO流详细解读

    java的IO流详细解读

    这篇文章主要介绍了java的IO流详细解读,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • java程序员如何编写更好的单元测试的7个技巧

    java程序员如何编写更好的单元测试的7个技巧

    测试是开发的一个非常重要的方面,可以在很大程度上决定一个应用程序的命运。良好的测试可以在早期捕获导致应用程序崩溃的问题,但较差的测试往往总是导致故障和停机。本文主要介绍java程序员编写更好的单元测试的7个技巧。下面跟着小编一起来看下吧
    2017-03-03
  • Java制作验证码的完整实例代码

    Java制作验证码的完整实例代码

    这篇文章主要给大家介绍了关于Java制作验证码的完整实例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • java不可逆加密算法之md5加密算法使用示例

    java不可逆加密算法之md5加密算法使用示例

    MD5的全称是Message-Digest Algorithm 5,他是一个不可逆的字符串加密算法,下面使用示例说明他的用法
    2014-01-01
  • JavaEE账号注册模拟网站邮箱激活

    JavaEE账号注册模拟网站邮箱激活

    这篇文章主要为大家详细介绍了JavaEE账号注册模拟网站邮箱激活,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09

最新评论