verifyCode static method
Implementation
static Future<Result> verifyCode(String phoneNumber, String code) async {
final result = await checkKwikpassHealth();
if(result.isSuccess) {
final healthData = result.getDataOrThrow();
if(healthData?.isKwikpassHealthy == false){
throw Exception('Kwikpass is unhealthy');
}
}
try {
final gokwik = DioClient().getClient();
await SnowplowTrackerService.sendCustomEventToSnowPlow({
'category': 'login_modal',
'label': 'submit_otp',
'action': 'automated',
'property': 'phone_number',
'value': int.tryParse(phoneNumber) ?? 0,
});
// Set the integration type header
gokwik.options.headers['kp-integration-type'] = 'APP_MAKER';
final deviceInfoJson =
await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkDeviceInfo)!);
final deviceInfoDetails =
deviceInfoJson != null ? jsonDecode(deviceInfoJson) : {};
final Map<String, String> bodyParams = {};
if (Platform.isAndroid) {
bodyParams['google_ad_id'] =
deviceInfoDetails[cdnConfigInstance.getKeys(StorageKeyKeys.gkGoogleAdId)!] ?? '';
}
if (Platform.isIOS) {
bodyParams['ios_ad_id'] =
deviceInfoDetails[cdnConfigInstance.getKeys(StorageKeyKeys.gkGoogleAdId)!] ?? '';
}
final response = (await gokwik.post(
cdnConfigInstance.getEndpoint(APIEndpointKeys.verifyCode)!,
data: {
'phone': phoneNumber,
'otp': int.tryParse(code),
...bodyParams,
},
))
.toBaseResponse();
if (response.isSuccess == false) {
return Failure(response.errorMessage ?? 'Failed to verify OTP');
}
final data = response.data;
final String token = data['token'];
final coreToken = data['coreToken'];
final String kpToken = data?['kpToken'];
String? merchantType = await cacheInstance.getValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkMerchantTypeKey)!);
if (merchantType! == 'shopify' || merchantType == 'custom_shopify') {
final res = await _handleShopifyVerifyResponse(
response.data,
phoneNumber,
token,
coreToken,
kpToken,
);
// final data = response.data as Map<String, dynamic>?;
// if (data!.containsKey('email')) {
// await trackAnalyticsEvent(AnalyticsEvents.appLoginSuccess, {
// 'email': data['email']?.toString() ?? "",
// 'phone': phoneNumber,
// 'customer_id':
// response.data?['shopifyCustomerId']?.toString() ?? "",
// });
// }
return res;
}
await cacheInstance.setValue(cdnConfigInstance.getKeys(StorageKeyKeys.gkAccessTokenKey)!, token);
if (coreToken != null) {
await cacheInstance.setValue(
cdnConfigInstance.getKeys(StorageKeyKeys.checkoutAccessTokenKey)!, coreToken);
}
// final responseForAffluence = await customerIntelligence();
await validateUserToken();
final loginResponse = await loginKpUser();
final responseData = loginResponse.getDataOrThrow();
if (loginResponse.isSuccess) {
if (responseData?.email != null) {
if (responseData != null) {
final updatedData = LoginResponseData(
phone: responseData.phone,
emailRequired: responseData.emailRequired,
email: responseData.email,
merchantResponse: responseData.merchantResponse,
isSuccess: responseData.isSuccess,
message: responseData.message,
/* affluence: responseForAffluence.isSuccess
? responseForAffluence.getDataOrThrow()
: responseData.affluence,*/
);
await cacheInstance.setValue(
cdnConfigInstance.getKeys(StorageKeyKeys.gkVerifiedUserKey)!,
jsonEncode(updatedData),
);
await SnowplowTrackerService.sendCustomEventToSnowPlow({
'category': 'login_modal',
'label': 'phone_Number_logged_in',
'action': 'logged_in',
'property': 'phone_number',
'value': int.tryParse(phoneNumber) ?? 0,
});
return Success(updatedData);
}
}
}
return loginResponse;
} catch (error) {
throw handleApiError(error);
}
}