RN对接京东支付sdk(IOS)

京东支付IOS接入说明文档

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'
            });
          }
        });
      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值