1、根据 京东支付IOS接入说明文档 集成sdk依赖的framework,配置相关的内容(xcode 需要配置签名)
2、在ios 下封装调用sdk的方法
JDPayManager.h
//
// JDPayManager.h
// b2bapp
//
// Created by edz on 2021/3/9.
// Copyright © 2021 Facebook. All rights reserved.
//
#import <React/RCTBridgeModule.h>
@interface JDPayManager : NSObject <RCTBridgeModule>
@end
JDPayManager.m
.m文件是对.h文件中方法是实现,对外部是不可见的
//
// JDPayManager.m
// b2bapp
//
// Created by edz on 2021/3/9.
// Copyright © 2021 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "JDPayManager.h"
#import "ViewController.h"
#import <JDPay/JDPay.h>
#import <JDPay/JDPayJSONUtils.h>
@implementation JDPayManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSDictionary *)payReq
callback:(RCTResponseSenderBlock) callback)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *weakSelfView = [self getCurrentVC];
NSString *appId = [payReq objectForKey:@"appId"];
NSString *merchant = [payReq objectForKey:@"merchant"];
NSString *orderId = [payReq objectForKey:@"orderId"];
NSString *signData = [payReq objectForKey:@"signData"];
NSLog(@"京东支付");
[JDPayAuth_mainModule() payWithViewController:weakSelfView
appID:appId
merchant:merchant
orderId:orderId
signData:signData
extraInfo:nil
completion:^(NSDictionary *resultDict) {
NSLog(@"支付结果");
NSLog(@"京东 result = %@",resultDict);
callback(@[resultDict]);
}];
});
}
- (UIViewController *)getCurrentVC
{
UIViewController * curVC = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
{
curVC = nextResponder;
}
else
{
UIViewController * appRootVC = window.rootViewController;
UIViewController * topVC = appRootVC;
curVC = topVC;
if (topVC.presentedViewController)
{
curVC = topVC.presentedViewController;
}
}
return curVC;
}
@end
AppDelegate.m
#pragma mark - Handle url
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithCapacity:2];
if (sourceApplication != nil) {
options[UIApplicationOpenURLOptionsSourceApplicationKey] = sourceApplication;
}
if (annotation != nil) {
options[UIApplicationOpenURLOptionsAnnotationKey] = annotation;
}
return [self application:application openURL:url options:options];
}
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
if ([JDPayModule canHandleURL:url options:options]) {
[JDPayModule handleURL:url
options:options
completionHandler:^(NSDictionary * __nullable results, BOOL handled) {
NSLog(@"handleURL:%@ options:%@ completionHandler:(%@, %@)",
url, options, results, handled ? @"YES" : @"NO");
}];
return YES;
}
return NO;
}
3、唤起京东支付(jdpay接口需要生成一个京东订单,返回商户id、订单id和京东支付MD5的密钥)
MD5的密钥需要通过MD5进行加密生产签名
const { code, context, message } = await webapi.jdpay(tid);
if (code == config.SUCCESS_CODE) {
const text =
'merchant=' +
context.merchant +
'&orderId=' +
context.orderId +
'&key=' +
Const.MD5_KET;
const signData = MD5.hex_md5(text).toLowerCase();
const payRequest = {
orderId: context.orderId,
merchant: context.merchant,
appId: Const.APP_ID,
signData: signData
};
let JDPayModule;
if (Platform.OS === 'ios') {
JDPayModule = NativeModules.JDPayManager;
JDPayModule.addEvent(payRequest, (result) => {
console.log('result====>', result);
if (result.payStatus == 'JDP_PAY_SUCCESS') {
msg.emit('router: goToNext', {
routeName: 'PaySuccess',
tid: tid,
payType: 'online'
});
}
});
}