iOS开发-微信支付集成

微信支付集成

接入准备

登录微信开放平台 https://open.weixin.qq.com
注册/登录,然后创建应用,申请支付功能

申请流程
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Pay/Vendor_Service_Center.html

微信接入指南
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html

1.通过 CocoaPods 导入,Podfile文件中增加
  pod 'WechatOpenSDK'

然后终端执行

pod install --verbose --no-repo-update
2.头文件引入
#import <AlipaySDK/AlipaySDK.h>
#import <WXApi.h>
#import <WXApiObject.h>
3.配置

1.白名单

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>wechat</string>
    <string>weixin</string>
    <string>weixinULAPI</string>
</array>

image

  1. TAGETS- info- URL types 设置URL Schemes
    image

3.配置Universal Link

3.APP启动后代码设置

1.向微信终端程序注册第三方应用。

OC:

[WXApi registerApp:wxAppId universalLink:universalLink];

swift:

 WXApi.registerApp(wxAppId, universalLink: universalLink)
4.组装请求信息(这6个参数全部由后端接口返回即可)

前后端appId要统一,否则调起会失败

OC:

//调起微信支付
PayReq* req             = [[PayReq alloc] init];
req.partnerId           = @"";
req.prepayId            = @"";
req.nonceStr            = @"";
req.timeStamp           = 1602663569;
req.package             = @"Sign=WXPay";
req.sign                = @"";
[WXApi sendReq:req completion:^(BOOL success) {

}];

Swift:

    let payReq       = PayReq()
    payReq.partnerId = ""
    payReq.prepayId  =  ""
    payReq.package   = "Sign=WXPay"
    payReq.nonceStr  = ""
    payReq.timeStamp = UInt32(timeStamp)!
    payReq.sign      = ""
    WXApi.send(payReq) { success in
        if success {
             debugPrint("微信成功调起")
        } else {
            debugPrint("微信调起异常")
        }
    }

以上就可以调起微信了,

微信回调配置

1.回调代码处理
OC:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler
{
    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}

#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp{
//    resp.errCode (通过code判断状态)
[[NSNotificationCenter defaultCenter] postNotificationName:@"wxLogin" object:resp];
}

Swift

func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
    return  WXApi.handleOpen(url, delegate: self)
}

func application(_ application:UIApplication, continue userActivity:NSUserActivity, restorationHandler:@escaping([UIUserActivityRestoring]?) ->Void) ->Bool{
    return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}

//WXApiDelegate 代理方法
func onResp(_ resp: BaseResp) {
    if resp is PayResp {
        let response: PayResp? = resp as? PayResp
        print(response?.errCode as Any)
      /*
           enum  WXErrCode {
           WXSuccess           = 0,    < 成功
           WXErrCodeCommon     = -1,  < 普通错误类型
           WXErrCodeUserCancel = -2,   < 用户点击取消并返回
           WXErrCodeSentFail   = -3,   < 发送失败
           WXErrCodeAuthDeny   = -4,   < 授权失败
           WXErrCodeUnsupport  = -5,   < 微信不支持
           };
           */
    }
    
}