IOS 微信支付

思路:

  • 申请商户:https://kf.qq.com/
  • 支付流程说明: https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3
  • 微信支付集成包:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html#jump2
  • 开发步骤官方文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5

具体实施步骤:

  • 接入SDK:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html#jump2
  • 在info -> URL Types -> URL Schemes中设置自己的appID
  • 在appDelegate.m文件中 设置appID
//向微信注册
- (BOOL) application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
  [WXApi registerApp: @"你的appID" universalLink: @"服务器地址"];
}
  • 在需要微信支付的控件处 调起微信支付
- (void) weChatPay {
  [NSDictionary micDic = [NSDictionary alloc] init];
  [micDic setValue:useId forKey: @"userId"];
  
  [self.myViewModel appweChatpay: micDic requestResult:^(NSString * _Nonnull string, BOOL success) { //appweChatpay  支付接口
    if (success) {
      if (string != nil || string.length != 0) {
        NSRange range = [string rangeOfString: @"?"];
        NSString  *rightStr = [string substringFromIndex: range.location+1];
        NSArray *objArray = [rightStr componentsSeparatedByString: @"&"];
        [NSDictionary *paramDic = [NSDictionary alloc] init];
        for (int i=0; i*/
       request.package = [paramDic valueForKey:@"package"];
       /** 随机串,防重发 */
       request.nonceStr = [paramDic valueForKey:@"nonceStr"];
       /** 时间戳,防重发 */
       request.timeStamp = [paramDic valueForKey:@"timeStamp"];
       /** 商家根据微信开放平台文档对数据做的签名, 可从服务器获取,也可本地生成*/
       request.sign = [paramDic valueForKey:@"sign"];
       [WXApi sendReq: request completion: nil];
      }
    }
  }];
}
  • 成功调起接口之后 在AppDelegate.m中进行回调
//支付返回结果
- (void)onResp:(BaseResp *)resp {
  if ([resp isKindOfClass:[PayResp class]]) {
    PayResp *payresp = (PayResp *)resp;
    [[NSNotificationCenter defaultCenter]
    postNotificationName:wxpayResultNotification object:@{@"resp":payresp}];
  }
}
//进行支付回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
  return [WXApi handleOpenUrl: url delegate:self];
}

你可能感兴趣的:(IOS 微信支付)