0

I don't know why I'm getting this error in objective c while integrating CashFree Payment service.

I'm trying to integrate cashfree payment gateway service in my iOS through Objective-C but don't know where I'm getting this error

ViewController.h:

@interface ViewController : UIViewController <CFResponseDelegate>

ViewController.m:

@interface ViewController() <CFResponseDelegate>

-(void) cashFreePayment;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self cashFreePayment];
}

-(void) cashFreePayment {
    @try {
        
        CFSessionBuilder* sessionBuilder = [[CFSessionBuilder alloc]init];
        sessionBuilder = [sessionBuilder setEnvironment:CFENVIRONMENTSANDBOX];
        sessionBuilder = [sessionBuilder setPaymentSessionId:@""];
        sessionBuilder = [sessionBuilder setOrderID:@""];
        sessionBuilder = [sessionBuilder buildAndReturnError:nil];
        
        CFPaymentComponentBuilder *paymentComponent = [[CFPaymentComponentBuilder alloc]init];
        paymentComponent = [paymentComponent enableComponents:@"order-details"];
        paymentComponent = [paymentComponent enableComponents:@"card"];
        paymentComponent = [paymentComponent enableComponents:@"upi"];
        paymentComponent = [paymentComponent enableComponents:@"wallet"];
        paymentComponent = [paymentComponent enableComponents:@"netbanking"];
        paymentComponent = [paymentComponent enableComponents:@"emi"];
        paymentComponent = [paymentComponent enableComponents:@"paylater"];
        paymentComponent = [paymentComponent buildAndReturnError:nil];
        
        
        CFThemeBuilder *theme = [[CFThemeBuilder alloc]init];
        theme = [theme setPrimaryFont:@"Futura"];
        theme = [theme setSecondaryFont:@"Menlo"];
        theme = [theme setButtonTextColor:@"#FFFFFF"];
        theme = [theme setButtonBackgroundColor:@"#FF0000"];
        theme = [theme setNavigationBarTextColor: @"#FFFFFF"];
        theme = [theme setNavigationBarBackgroundColor:@"#C3C3C3"];
        theme = [theme buildAndReturnError:nil];
        
        CFWebCheckoutPaymentBuilder *payout = [[CFWebCheckoutPaymentBuilder alloc]init];
        payout = [payout setSession:sessionBuilder];
        payout = [payout buildAndReturnError:nil];
        
    } @catch (NSException *exception) {
        NSLog(@"%@", exception.reason);
    }
}


- (void)onError:(CFErrorResponse * _Nonnull)error order_id:(NSString * _Nonnull)order_id {
    NSLog(error, order_id);
}

- (void)verifyPaymentWithOrder_id:(NSString * _Nonnull)order_id {
    NSLog(order_id);
}
koen
  • 5,383
  • 7
  • 50
  • 89
  • 1
    Try adding an exception breakpoint, that should hopefully tell you where the error occurs: https://stackoverflow.com/questions/17802662/ – koen Mar 26 '23 at 11:32
  • Not getting the error through these exception breakpoints. – YashSaxena Mar 26 '23 at 12:16
  • 1
    Do you have the stacktrace? From what I guess, at some point, your are using setting a `NSString` where it should be an `NSArray` (or any other class with a `count` method/getter). I don't use CashFree, but check the code, if it uses a value in a plist, it could be there, or also if there a "cloud" values, there too. – Larme Mar 26 '23 at 12:38
  • 2
    From what I see: `[paymentComponent enableComponents:@"order-details"];` seems wrong. Instead, you should do: `paymentComponent enableComponents:@[@"order-details", @"netbanking", @"upi"...]];` as I read `componentS` with an "s" (plural), and see the method https://github.com/cashfree/core-ios-sdk/blob/6ea73372d0ae25bd2ec9f5aef951ea7d906ff329/CashfreePGUISDK.xcframework/ios-arm64_x86_64-simulator/CashfreePGUISDK.framework/Headers/CashfreePGUISDK-Swift.h#L301 `- (CFPaymentComponentBuilder * _Nonnull)enableComponents:(NSArray * _Nonnull)components` Don't you have a warning in code? – Larme Mar 26 '23 at 12:46
  • I’d advise against `NSException` catching because these are bugs that should be resolved during development, not simply caught and logged at runtime. If your program catches these, we can’t use the exception breakpoint and/or look at the stack trace. Bottom line, as Larme said, some method was provided a string rather than an array, but why should we be guessing. And exception breakpoint and associated stack track would tell you precisely where the problem was. Don’t using exception handling, or if you do, look at `callStackSymbols`. – Rob Mar 27 '23 at 03:40

1 Answers1

0

The runtime error is saying this: I tried to call the method/getter count on an object I supposed had it, but in fact it's a NSString object (__NSCFConstantString being an internal class of Apple for NSString for optimization purposes). And since it doesn't have that method, it will throw a unrecognized selector exception.

count being a common method, we could guess, since NSArray has it, that you put a NSString instead of a NSArray somewhere. It could also be a NSString instead of a NSDictionary.

Now, let's try your code:

You are ignoring compiler warning! Why?

If you don't ignore them, you'll see them on:

paymentComponent = [paymentComponent enableComponents:@"order-details"];

The warning says:

Incompatible pointer types sending 'NSString *' to parameter of type 'NSArray<NSString *> * _Nonnull'

If we see the method, it's as said by the warning:

- (CFPaymentComponentBuilder * _Nonnull)enableComponents:(NSArray<NSString *> * _Nonnull)components SWIFT_WARN_UNUSED_RESULT;

So instead of

paymentComponent = [paymentComponent enableComponents:@"order-details"];
paymentComponent = [paymentComponent enableComponents:@"card"];
paymentComponent = [paymentComponent enableComponents:@"upi"];
paymentComponent = [paymentComponent enableComponents:@"wallet"];
paymentComponent = [paymentComponent enableComponents:@"netbanking"];
paymentComponent = [paymentComponent enableComponents:@"emi"];
paymentComponent = [paymentComponent enableComponents:@"paylater"];

Do:

let components = @[@"order-details", @"card", @"upi", @"wallet", @"netbanking", @"emi", @"paylater"];
paymentComponent = [paymentComponent enableComponents:components];
Larme
  • 24,190
  • 6
  • 51
  • 81