UNPKG

react-native-klarna-inapp-sdk

Version:

This library wraps Klarna Mobile SDK and exposes its functionality as React Native components.

72 lines 2.61 kB
import RNKlarnaSignIn from './specs/NativeKlarnaSignIn'; // declare a class that receive the KlarnaSignInProps in the initializer and expose a method to sign in export class KlarnaSignInSDK { constructor(props, instanceId) { this.environment = props.environment; this.region = props.region; this.returnUrl = props.returnUrl; this.instanceId = instanceId; } static async createInstance(props) { if (props.returnUrl.trim() === '') { return new Promise((_, reject) => { const sdkError = { isFatal: true, message: 'Invalid or null return URL', name: 'KlarnaSignInMissingReturnUrl' }; reject(sdkError); }); } return new Promise((resolve, reject) => { let instanceId = Math.random().toString(36).substring(2, 15); RNKlarnaSignIn.init(instanceId, props.environment, props.region, props.returnUrl).then(_ => { resolve(new KlarnaSignInSDK(props, instanceId)); }).catch(error => { const errorInfo = error.userInfo; const sdkError = { isFatal: errorInfo.isFatal, message: errorInfo.message, name: errorInfo.name, sessionId: errorInfo.sessionId, params: errorInfo.params }; reject(sdkError); }); }); } dispose() { return RNKlarnaSignIn.dispose(this.instanceId); } signIn(clientId, scope, market, locale, tokenizationId) { const scopes = scope.split(' '); const tokenizationScopes = ['payment:customer_not_present', 'payment:customer_present']; const isTokenizationScopeIncluded = tokenizationScopes.some(value => scopes.includes(value)); if (isTokenizationScopeIncluded && (!tokenizationId || tokenizationId.trim() === '')) { return new Promise((_, reject) => { const sdkError = { isFatal: false, message: `Found ${tokenizationScopes} in scope but tokenizationId is empty.`, name: 'KlarnaSignInMissingTokenizationId' }; reject(sdkError); }); } return new Promise((resolve, reject) => { RNKlarnaSignIn.signIn(this.instanceId, clientId, scope, market, locale, tokenizationId).then(result => { resolve(result); }).catch(error => { const errorInfo = error.userInfo; const sdkError = { isFatal: errorInfo.isFatal, message: errorInfo.message, name: errorInfo.name, sessionId: errorInfo.sessionId, params: errorInfo.params }; reject(sdkError); }); }); } } //# sourceMappingURL=KlarnaSignInSDK.js.map