IOS 集成微信支付功能的实现方法

2020-01-21 01:30:32于丽

IOS 集成微信支付功能的实现方法

第一步:集成微信的SDK

https://www.easck.com/wiki/doc/api/index.html    

点击进入

下载对应SDK或示例,最后可以看看示例程序

第二步:在Xcode中填写微信开放平台申请的Appid

Xcode>info>URL Types  中新建加入Appid

第三步:在Appdelegate.m 中注册微信支付 和回调


#import "WXApi.h"
添加 代理
WXApiDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  //self.window.backgroundColor = [UIColor clearColor];

  // 微信支付注册
  [WXApiregisterApp:PAY_WEIXIN_ID];

  
  returnYES;
}

// ios 9.0以上系统版本回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
  // 微信
  if ([url.schemeisEqualToString:PAY_WEIXIN_ID]) {
    [WXApihandleOpenURL:url delegate:(id<WXApiDelegate>)self];
  }
  
  // 支付宝
  if ([url.schemeisEqualToString:@"SearchPigeonWorld"]) {
    //跳转支付宝钱包进行支付,处理支付结果
    [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) {
      if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) {
        [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO];
      }
    }];
  }

  returnYES;
}


//支付成功时调用,回到第三方应用中 ios 9.0以下系统版本回调
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  // 微信
  if ([url.schemeisEqualToString:PAY_WEIXIN_ID])
  {
    [WXApihandleOpenURL:url delegate:(id<WXApiDelegate>)self];
  }
  
  // 支付宝
  if ([url.hostisEqualToString:PAY_ALIPAY_appID]) {
    //跳转支付宝钱包进行支付,处理支付结果
    [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) {
      if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) {
        [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO];
      }
    }];
  }
  
  
  returnYES;
}


/**
 微信自己的结果返回方法

 @param resp 返回结果状态
 */
- (void)onResp:(BaseResp*)resp
{
  if([respisKindOfClass:[PayRespclass]]){
    BOOL isPaySuccess =NO;
    switch (resp.errCode) {
      caseWXSuccess:
        isPaySuccess = YES;
        break;
      caseWXErrCodeUserCancel:
        isPaySuccess = NO;
        break;
      caseWXErrCodeSentFail:
        isPaySuccess = NO;
        break;
      caseWXErrCodeAuthDeny:
        isPaySuccess = NO;
        break;
      default:
        isPaySuccess = NO;
        break;
    }
    if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) {
      [self.appMyDelegatepayCenterWeixinOnResultWith:isPaySuccess];
    }
  }
}