diff --git a/CHANGELOG.md b/CHANGELOG.md index be6f9d5b..f34e7b39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [3.0.0](https://github.com/smallcase/react-native-smallcase-gateway/compare/v2.3.1...v3.0.0) (2024-01-19) + + +### Features + +* loans ([1ac32dc](https://github.com/smallcase/react-native-smallcase-gateway/commit/1ac32dc1506b5511ab7924a73bfd2b31d471fe8a)) + + +### Bug Fixes + +* **native:** changed Bridge method responses to conform to ScLoanResponse ([6f1a2b6](https://github.com/smallcase/react-native-smallcase-gateway/commit/6f1a2b621f90b7677d37e6dd2150487f46e3d5a7)) + ### [2.3.1](https://github.com/smallcase/react-native-smallcase-gateway/compare/v2.3.0...v2.3.1) (2023-12-08) ## [2.3.0](https://github.com/smallcase/react-native-smallcase-gateway/compare/v2.2.2...v2.3.0) (2023-10-17) diff --git a/android/src/main/java/com/reactnativesmallcasegateway/SmallcaseGatewayModule.kt b/android/src/main/java/com/reactnativesmallcasegateway/SmallcaseGatewayModule.kt index d2a5ab34..c78b62e9 100644 --- a/android/src/main/java/com/reactnativesmallcasegateway/SmallcaseGatewayModule.kt +++ b/android/src/main/java/com/reactnativesmallcasegateway/SmallcaseGatewayModule.kt @@ -315,18 +315,13 @@ class SmallcaseGatewayModule(reactContext: ReactApplicationContext) : ReactConte val scGatewayConfig = ScLoanConfig(gateway, scEnvironment) val setupResponse = ScLoan.setup(scGatewayConfig, object : ScLoanResult { override fun onFailure(error: ScLoanError) { - val errorWritableMap = createErrorJSON(error.code, error.message, error.data) - promise.reject("error", errorWritableMap) - } - - override fun onSuccess(response: ScLoanSuccess) { - promise.resolve(response.toString()) - } + promise.reject("${error.code}", scLoanResponseToWritableMap(error) ?: return) + } + + override fun onSuccess(response: ScLoanSuccess) { + promise.resolve(scLoanResponseToWritableMap(response) ?: return) + } }) - // val writableMap: WritableMap = Arguments.createMap() - // writableMap.putString("version", setupResponse.version) - // writableMap.putInt("versionCode", setupResponse.versionCode.toInt()) - // promise.resolve(Gson().toJson(writableMap.toHashMap())) } @ReactMethod @@ -341,12 +336,11 @@ class SmallcaseGatewayModule(reactContext: ReactApplicationContext) : ReactConte val loanConfigObj = ScLoanInfo(interactionToken) ScLoan.apply(appCompatActivity, loanConfigObj, object : ScLoanResult { override fun onFailure(error: ScLoanError) { - val errorWritableMap = createErrorJSON(error.code, error.message, error.data) - promise.reject("error", errorWritableMap) + promise.reject("${error.code}", scLoanResponseToWritableMap(error) ?: return) } override fun onSuccess(response: ScLoanSuccess) { - promise.resolve(response.toString()) + promise.resolve(scLoanResponseToWritableMap(response) ?: return) } }) } @@ -363,12 +357,11 @@ class SmallcaseGatewayModule(reactContext: ReactApplicationContext) : ReactConte val loanConfigObj = ScLoanInfo(interactionToken) ScLoan.pay(appCompatActivity, loanConfigObj, object : ScLoanResult { override fun onFailure(error: ScLoanError) { - val errorWritableMap = createErrorJSON(error.code, error.message, error.data) - promise.reject("error", errorWritableMap) + promise.reject("${error.code}", scLoanResponseToWritableMap(error) ?: return) } override fun onSuccess(response: ScLoanSuccess) { - promise.resolve(response.toString()) + promise.resolve(scLoanResponseToWritableMap(response) ?: return) } }) } @@ -385,16 +378,15 @@ class SmallcaseGatewayModule(reactContext: ReactApplicationContext) : ReactConte val loanConfigObj = ScLoanInfo(interactionToken) ScLoan.withdraw(appCompatActivity, loanConfigObj, object : ScLoanResult { override fun onFailure(error: ScLoanError) { - val errorWritableMap = createErrorJSON(error.code, error.message, error.data) - promise.reject("error", errorWritableMap) + promise.reject("${error.code}", scLoanResponseToWritableMap(error) ?: return) } override fun onSuccess(response: ScLoanSuccess) { - promise.resolve(response.toString()) + promise.resolve(scLoanResponseToWritableMap(response) ?: return) } }) } - + @ReactMethod fun service(loanConfig: ReadableMap, promise: Promise) { val appCompatActivity = currentActivity as? AppCompatActivity ?: return @@ -407,12 +399,11 @@ class SmallcaseGatewayModule(reactContext: ReactApplicationContext) : ReactConte val loanConfigObj = ScLoanInfo(interactionToken) ScLoan.service(appCompatActivity, loanConfigObj, object : ScLoanResult { override fun onFailure(error: ScLoanError) { - val errorWritableMap = createErrorJSON(error.code, error.message, error.data) - promise.reject("error", errorWritableMap) + promise.reject("${error.code}", scLoanResponseToWritableMap(error) ?: return) } override fun onSuccess(response: ScLoanSuccess) { - promise.resolve(response.toString()) + promise.resolve(scLoanResponseToWritableMap(response) ?: return) } }) } @@ -472,5 +463,20 @@ class SmallcaseGatewayModule(reactContext: ReactApplicationContext) : ReactConte return errObj } + private fun scLoanResponseToWritableMap(response: ScLoansResponse?): WritableMap? { + val map = Arguments.createMap() + response?.also { + map.putBoolean("isSuccess", it.isSuccess) + map.putString("data", it.data) + } + if(response is ScLoanError) { + map.apply { + putInt("code", response.code) + putString("message", response.message) + } + } + return map + } + } diff --git a/ios/SmallcaseGateway.m b/ios/SmallcaseGateway.m index 81e8845b..5fa914df 100644 --- a/ios/SmallcaseGateway.m +++ b/ios/SmallcaseGateway.m @@ -518,19 +518,10 @@ @interface RCT_EXTERN_MODULE(SmallcaseGateway, NSObject) [ScLoan.instance setupWithConfig:gatewayLoanConfig completion:^(ScLoanSuccess * success, ScLoanError * error) { if(error != nil) { - if(error != nil) { - NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init]; - [responseDict setValue:[NSNumber numberWithInteger:error.code] forKey:@"errorCode"]; - [responseDict setValue:error.domain forKey:@"errorMessage"]; - - NSError *err = [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:responseDict]; - - reject(@"apply", @"Error while applying for Loan", err); - return; - } + reject([NSString stringWithFormat:@"%li", (long)error.errorCode], error.errorMessage, [self scLoanErrorToDict:error]); + return; } - - resolve(@(YES)); + resolve([self scLoanSuccessToDict:success]); }]; } @@ -554,17 +545,10 @@ @interface RCT_EXTERN_MODULE(SmallcaseGateway, NSObject) [ScLoan.instance applyWithPresentingController:[[[UIApplication sharedApplication] keyWindow] rootViewController] loanInfo:gatewayLoanInfo completion:^(ScLoanSuccess * success, ScLoanError * error) { if(error != nil) { - NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init]; - [responseDict setValue:[NSNumber numberWithInteger:error.code] forKey:@"errorCode"]; - [responseDict setValue:error.domain forKey:@"errorMessage"]; - - NSError *err = [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:responseDict]; - - reject(@"apply", @"Error while applying for Loan", err); + reject([NSString stringWithFormat:@"%li", (long)error.errorCode], error.errorMessage, [self scLoanErrorToDict:error]); return; } - - resolve(success.data); + resolve([self scLoanSuccessToDict:success]); }]; } @@ -588,17 +572,10 @@ @interface RCT_EXTERN_MODULE(SmallcaseGateway, NSObject) [ScLoan.instance payWithPresentingController:[[[UIApplication sharedApplication] keyWindow] rootViewController] loanInfo:gatewayLoanInfo completion:^(ScLoanSuccess * success, ScLoanError * error) { if(error != nil) { - NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init]; - [responseDict setValue:[NSNumber numberWithInteger:error.code] forKey:@"errorCode"]; - [responseDict setValue:error.domain forKey:@"errorMessage"]; - - NSError *err = [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:responseDict]; - - reject(@"apply", @"Error while applying for Loan", err); + reject([NSString stringWithFormat:@"%li", (long)error.errorCode], error.errorMessage, [self scLoanErrorToDict:error]); return; } - - resolve(success.data); + resolve([self scLoanSuccessToDict:success]); }]; } @@ -622,17 +599,10 @@ @interface RCT_EXTERN_MODULE(SmallcaseGateway, NSObject) [ScLoan.instance withdrawWithPresentingController:[[[UIApplication sharedApplication] keyWindow] rootViewController] loanInfo:gatewayLoanInfo completion:^(ScLoanSuccess * success, ScLoanError * error) { if(error != nil) { - NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init]; - [responseDict setValue:[NSNumber numberWithInteger:error.code] forKey:@"errorCode"]; - [responseDict setValue:error.domain forKey:@"errorMessage"]; - - NSError *err = [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:responseDict]; - - reject(@"apply", @"Error while applying for Loan", err); + reject([NSString stringWithFormat:@"%li", (long)error.errorCode], error.errorMessage, [self scLoanErrorToDict:error]); return; } - - resolve(success.data); + resolve([self scLoanSuccessToDict:success]); }]; } @@ -656,22 +626,41 @@ @interface RCT_EXTERN_MODULE(SmallcaseGateway, NSObject) [ScLoan.instance serviceWithPresentingController:[[[UIApplication sharedApplication] keyWindow] rootViewController] loanInfo:gatewayLoanInfo completion:^(ScLoanSuccess * success, ScLoanError * error) { if(error != nil) { - NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init]; - [responseDict setValue:[NSNumber numberWithInteger:error.code] forKey:@"errorCode"]; - [responseDict setValue:error.domain forKey:@"errorMessage"]; - - NSError *err = [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:responseDict]; - - reject(@"apply", @"Error while applying for Loan", err); + reject([NSString stringWithFormat:@"%li", (long)error.errorCode], error.errorMessage, [self scLoanErrorToDict:error]); return; } - - resolve(success.data); + resolve([self scLoanSuccessToDict:success]); + }]; } }); } +- (NSDictionary *)scLoanSuccessToDict:(ScLoanSuccess *)success { + NSMutableDictionary *successDict = [NSMutableDictionary dictionary]; + successDict[@"isSuccess"] = @(success.isSuccess); + + id data = success.data; + if (data && ![data isKindOfClass:[NSNull class]]) { + successDict[@"data"] = data; + } + + return successDict; +} + +- (NSError *)scLoanErrorToDict:(ScLoanError *)error { + + NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init]; + [responseDict setValue:[NSNumber numberWithInteger:error.errorCode] forKey:@"code"]; + [responseDict setValue:error.errorMessage forKey:@"message"]; + [responseDict setValue:error.data forKey:@"data"]; + [responseDict setValue:@NO forKey:@"isSuccess"]; + + NSError *err = [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:responseDict]; + return err; + +} + @end diff --git a/package.json b/package.json index f1f81add..d840881e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-native-smallcase-gateway", "title": "React Native Smallcase Gateway", - "version": "2.3.1", + "version": "3.0.0", "description": "smallcase gateway bindings for react native", "main": "src/index.js", "files": [ diff --git a/src/ScLoan.js b/src/ScLoan.js index 434b502b..788551ce 100644 --- a/src/ScLoan.js +++ b/src/ScLoan.js @@ -11,13 +11,24 @@ const { SmallcaseGateway: SmallcaseGatewayNative } = NativeModules; * * @typedef {Object} ScLoanInfo * @property {String} interactionToken + * + * @typedef {Object} ScLoanSuccess + * @property {boolean} isSuccess + * @property {string} data + * + * @typedef {Object} ScLoanError + * @property {boolean} isSuccess + * @property {number} code + * @property {string} message + * @property {string} data */ /** * Setup ScLoans * * @param {ScLoanConfig} config - * @returns {Promise} + * @returns {Promise} + * @throws {ScLoanError} */ const setup = async (config) => { const safeConfig = safeObject(config); @@ -30,7 +41,8 @@ const setup = async (config) => { * Triggers the LOS Journey * * @param {ScLoanInfo} loanInfo - * @returns {Promise} + * @returns {Promise} + * @throws {ScLoanError} */ const apply = async (loanInfo) => { const safeLoanInfo = safeObject(loanInfo); @@ -42,7 +54,8 @@ const apply = async (loanInfo) => { * Triggers the Repayment Journey * * @param {ScLoanInfo} loanInfo - * @returns {Promise} + * @returns {Promise} + * @throws {ScLoanError} */ const pay = async (loanInfo) => { const safeLoanInfo = safeObject(loanInfo); @@ -54,7 +67,8 @@ const pay = async (loanInfo) => { * Triggers the Withdraw Journey * * @param {ScLoanInfo} loanInfo - * @returns {Promise} + * @returns {Promise} + * @throws {ScLoanError} */ const withdraw = async (loanInfo) => { const safeLoanInfo = safeObject(loanInfo); @@ -66,7 +80,8 @@ const withdraw = async (loanInfo) => { * Triggers the Servicing Journey * * @param {ScLoanInfo} loanInfo - * @returns {Promise} + * @returns {Promise} + * @throws {ScLoanError} */ const service = async (loanInfo) => { const safeLoanInfo = safeObject(loanInfo);