reactnative ios微信支付升级1.8.7以上版本
1.首先把sdk相关文件替换,在官网下载。
2.创建wxApi文件夹,在里面引入对应的sdk文件
Wxpay的目录2.1 在Wxpay文件夹内创建WxpayModule.h文件
内容为:
#import
#import
#import "WXApiObject.h"
#import
#import "WXApi.h"
@interface WxpayModule : NSObject <RCTBridgeModule, WXApiDelegate>
@end
2.2 在Wxpay文件夹内创建WxpayModule.m文件
内容为:
#import "WxpayModule.h"
@implementation WxpayModule
RCTPromiseResolveBlock resolveBlock = nil;
- (instancetype)init
{
self= [superinit];
if(self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWXPay:) name:@"WXPay" object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)handleWXPay:(NSNotification*)aNotification
{
NSString* errCode = [aNotificationuserInfo][@"errCode"];
resolveBlock(@{@"errCode": errCode});
}
RCT_EXPORT_METHOD(registerApp:(NSString*)APP_ID){
[WXApi registerApp: APP_ID universalLink:@"https://xx.xx.xx/xx/"];//向微信注册
}
RCT_EXPORT_METHOD(pay:(NSDictionary *)order
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject){
resolveBlock= resolve;
//调起微信支付
//注意order取的值对应的key要和自己服务器提供的一致
PayReq*req = [[PayReqalloc]init];
req.partnerId= [orderobjectForKey:@"partnerid"];
req.prepayId= [orderobjectForKey:@"prepayid"];
req.nonceStr= [orderobjectForKey:@"noncestr"];
req.timeStamp= [[orderobjectForKey:@"timestamp"]intValue];
req.package= [orderobjectForKey:@"package"];
req.sign= [orderobjectForKey:@"sign"];
[WXApi sendReq:req completion: nil];
}
RCT_REMAP_METHOD(isSupported, // 判断是否支持调用微信SDK
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject){
if(![WXApiisWXAppInstalled]) resolve(@NO);
elseresolve(@YES);
}
RCT_EXPORT_MODULE(Wxpay);
@end
注意把universalLink替换成自己的,这个是短链接具体配置有专门的文章介绍,可以自行百度。
3.AppDelegate.h
#import
@class WeChatPayViewController;
@interface AppDelegate : UIResponder<UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) WeChatPayViewController *viewController;
@end
4.AppDelegate.m
引入:#import "WxpayModule.h"
添加如下方法
- (BOOL)application:(UIApplication*)applicationcontinueUserActivity:(NSUserActivity*)userActivityrestorationHandler:(void(^)(NSArray<id> *__nullablerestorableObjects))restorationHandler{
NSURL*LinkUrl = userActivity.webpageURL;
NSLog(@"linkurl == %@",LinkUrl);
return [WXApi handleOpenURL:LinkUrl delegate: self];
//do something..
// return [WXApi handleOpenUniversalLink:userActivity delegate:[WXApiManager sharedManager]];//微信SDK1.8.6开始的 必须验证短链
// return YES;
}
//支付回调9以前
- (BOOL)application:(UIApplication*)applicationhandleOpenURL:(NSURL*)url {
return [WXApi handleOpenURL:url delegate: self];
}
#pragma mark- wx callback
- (void)onReq:(BaseReq*)req
{
// TODO Something
}
- (void)onResp:(BaseResp*)resp
{
//判断是否是微信支付回调 (注意是PayResp 而不是PayReq)
if ([resp isKindOfClass:[PayResp class]])
{
//发出通知 从微信回调回来之后,发一个通知,让请求支付的页面接收消息,并且展示出来,或者进行一些自定义的展示或者跳转
NSNotification* notification = [NSNotificationnotificationWithName:@"WXPay"object:niluserInfo:@{@"errCode":@(resp.errCode)}];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}