iOS杂技开发demoIOS

iOS微信开发(微信支付、他人代付、APP跳转微信公众号)

2016-10-19  本文已影响2395人  Trowa
最近公司心血来潮,一心要搞微信。废话不多说,直接上干货。

开发前准备:

1、在微信开发者平台获取开发者认证:(一年300元人民币)
PS:具体流程按照微信流程指示操作即可,在这就不废话了。

微信开发者平台_账号中心

2、下载微信最新的SDK,引入到工程里边

Xcode_目录文件

3、导入相关系统库及文件,不导入会报错

相关系统库

4、在你的工程文件中选择Build Setting,在Search Paths中添加 libWeChatSDK.a, WXApi.h, WXApiObject.h 三个文件所在位置

SearchPath

5、配置info.plist文件

info配置

6、URL type设置
选中你的工程设置项,选中targets一栏,在info标签栏的URL type 添加URL scheme 为你所注册的应用程序id

URL_Types

1.app跳转微信

1、app跳转微信公众号
在工程中添加pch文件,导入 WXApi.h头文件,并在你需要跳转的界面增加WXApiDelegate协议
在你的跳转方法中添加一下代码:

JumpToBizProfileReq *req = [JumpToBizProfileReq new];
req.username = @"gh_64a69d298ecf"; // 原始ID
req.profileType = WXBizProfileType_Normal;
req.extMsg = @"";
[WXApi sendReq:req];

这样APP就可以掉起微信界面,进入微信公众号(如果之前没有关注过此微信公众号,就会进入微信公众号的关注界面)

2、APP跳转到微信相关的其他网页(如微信小店相关商品信息界面)

OpenWebviewReq *req = [OpenWebviewReq new];
req.url = @"需要微信内置浏览器打开的网址";
[WXApi sendReq:req];

微信官方AppId: wxd930ea5d5a258f4f

PS:
原始ID: 申请公共号时系统给你的唯一编号,有此 id你更改微信号后依然能绑定公共号
APPID: 开通 api 服务时,系统给你的唯一编号
APPSECRET: 通俗一点儿来说就是密钥

2、微信支付

微信支付流程

流程可能看起来有点复杂,下面我尽量给你们配图来说明

1、APP客户端下单(选择商品)

选择产品

2、APP将商品数据传给后台服务器,请求生成支付订单

生成订单信息

3、后台调用统一下单API向微信服务器发送请求生成预付单,并生成一个prepay_id返回给后台,后台再将prepay_id返回给客户端,成功生成订单信息

成功生成订单

生成订单相关代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *signParaDic = @{@"schoolId":schoolId,
                              @"studentId":studentId,
                              @"parentId":parentId,
                              @"parentTel":phone,
                              @"totalFee":[NSString stringWithFormat:@"%i",payProduct.totalFee],
                              @"productId":[NSString stringWithFormat:@"%i",payProduct.productId],
                              @"productName":payProduct.productName};
NSString *sign = [EncodeParameter md5:signParaDic];
NSDictionary *parameters = @{@"schoolId":schoolId,
                             @"parentId":parentId,
                             @"studentId":studentId,
                             @"parentTel":phone,
                             @"totalFee":[NSString stringWithFormat:@"%i",payProduct.totalFee],
                             @"productId":[NSString stringWithFormat:@"%i",payProduct.productId],
                             @"productName":payProduct.productName,
                             @"sign":sign};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:WechatPlaceOrderAPI parameters:parameters error:nil];

_palceOrderOperation = [manager HTTPRequestOperationWithRequest:request  success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //
    NSDictionary *resDic = (NSDictionary *)responseObject;
    NSNumber *code = [resDic objectForKey:@"result"];
    NSString *msg = [resDic objectForKey:@"message"];
    if ([code integerValue] == Success) {
        WeChatPreOrder *preOrder = [[WeChatPreOrder alloc] initWithDic:resDic];
        if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderSucess:preOder:)]) {
            [self.delegate placeOrderSucess:self preOder:preOrder];
        }
    } else if ([code integerValue] == 300) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderFailed:error:)]) {
            [self.delegate placeOrderFailed:self error:msg];
        }
    }else {
        if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderFailed:error:)]) {
            [self.delegate placeOrderFailed:self error:RequestError];
        }
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (self.delegate && [self.delegate respondsToSelector:@selector(placeOrderFailed:error:)]) {
        [self.delegate placeOrderFailed:self error:RequestError];
    }
}];

4、APP调用SDK打开微信,进行支付
微信客户端向微信服务器发起支付请求并返回支付结果(中间交互参数prepay_id,微信服务器要验证这个参数是否跟之前的prepay_id一致)

微信支付

调起微信支付相关代码

PayReq* req             = [[PayReq alloc] init];
req.partnerId           = weChatPreOrder.partnerId;
req.prepayId            = weChatPreOrder.prepayId;
req.nonceStr            = weChatPreOrder.noncestr;
req.timeStamp           = [weChatPreOrder.timestamp intValue];
req.package             = weChatPreOrder.package;
req.sign                = weChatPreOrder.sign;
[WXApi sendReq:req];

5、用户输入支付密码后,微信客户端跟微信服务器交互,确认支付

输入密码进行支付.png

6、微信服务器将支付结果返回给微信客户端,并异步发送支付结果给我们后台

客户端支付结果

7、客户端可以调用后台查询支付订单接口,查询是否支付成功,并显示支付结果

客户端支付结果

查询订单相关代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *signParaDic = @{@"schoolId":[BCHTTPRequest schoolId],@"parentId":[BCHTTPRequest getUserId]};
NSString *sign = [EncodeParameter md5:signParaDic];
NSDictionary *parameters = @{@"schoolId":[BCHTTPRequest schoolId],@"parentId":[BCHTTPRequest getUserId],@"sign":sign};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:QueryOrderAPI parameters:parameters error:nil];

_operation = [manager HTTPRequestOperationWithRequest:request  success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //
    NSDictionary *resDic = (NSDictionary *)responseObject;
    NSNumber *code = [resDic objectForKey:@"result"];
    NSString *msg = [resDic objectForKey:@"message"];
    if ([code integerValue] == Success) {
        NSArray *order = [self orderArray:resDic];
        if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderSucess:orders:)]) {
            [self.delegate queryOrderSucess:self orders:order];
        }
    } else if ([code integerValue] == 300) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderFailed:error:)]) {
            [self.delegate queryOrderFailed:self error:msg];
        }
    }else {
        if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderFailed:error:)]) {
            [self.delegate queryOrderFailed:self error:RequestError];
        }
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (self.delegate && [self.delegate respondsToSelector:@selector(queryOrderFailed:error:)]) {
        [self.delegate queryOrderFailed:self error:RequestError];
    }
}];

MD5签名相关代码

+ (NSString *)md5:(NSDictionary *)paraDic {
  // NOTE: 排序,得出最终请求字串
 NSArray* sortedKeyArray = [[paraDic allKeys] sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
      return [obj1 compare:obj2];
 }];
  NSMutableArray *tmpArray = [NSMutableArray new];
  for (NSString* key in sortedKeyArray) {
      NSString* value = [paraDic objectForKey:key];
      if ([value isKindOfClass:[NSNumber class]]) {
          value = [(NSNumber *)value stringValue];
      }
      if (value.length > 0) {
          NSString *keyValueStr = [NSString stringWithFormat:@"%@=%@",key,value];
          [tmpArray addObject:keyValueStr];
      }
  }
  NSString *parameterStr = [tmpArray componentsJoinedByString:@"&"];
  NSString *res = [NSString stringWithFormat:@"%@&key=%@",parameterStr,Key];
  return [[BCBaseObject MD5Hash:res] uppercaseString];
}

开发相关文档及资源
SDK下载
支付账户
协议规则
安全规范_签名方式

上一篇下一篇

猜你喜欢

热点阅读