Skip to content

Commit

Permalink
Fixed supported currencies by SumUp
Browse files Browse the repository at this point in the history
  • Loading branch information
ital0 committed Mar 15, 2018
1 parent 843d00e commit 7fed1c8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 40 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {
annotationProcessor 'com.neenbedankt.bundles:argument:1.0.4'
compile 'com.neenbedankt.bundles:argument:1.0.4'
compile 'com.facebook.react:react-native:+'
compile('com.sumup:merchant-sdk:2.5.1@aar') {
compile('com.sumup:merchant-sdk:2.5.2@aar') {
transitive = true
}
}
Expand Down
79 changes: 70 additions & 9 deletions android/src/main/java/com/nextar/sumup/RNSumUpModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
Expand All @@ -11,6 +12,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.sumup.merchant.Models.TransactionInfo;
import com.sumup.merchant.api.SumUpAPI;
Expand All @@ -20,6 +22,8 @@
import com.sumup.merchant.Models.UserModel;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
Expand All @@ -32,9 +36,23 @@ public class RNSumUpModule extends ReactContextBaseJavaModule {
private static final int REQUEST_CODE_LOGIN = 1;
private static final int REQUEST_CODE_PAYMENT = 2;
private static final int REQUEST_CODE_PAYMENT_SETTINGS = 3;

private static final int TRANSACTION_SUCCESSFUL = 1;

private static final String SMPCurrencyCodeBGN = "SMPCurrencyCodeBGN";
private static final String SMPCurrencyCodeBRL = "SMPCurrencyCodeBRL";
private static final String SMPCurrencyCodeCHF = "SMPCurrencyCodeCHF";
private static final String SMPCurrencyCodeCLP = "SMPCurrencyCodeCLP";
private static final String SMPCurrencyCodeCZK = "SMPCurrencyCodeCZK";
private static final String SMPCurrencyCodeDKK = "SMPCurrencyCodeDKK";
private static final String SMPCurrencyCodeEUR = "SMPCurrencyCodeEUR";
private static final String SMPCurrencyCodeGBP = "SMPCurrencyCodeGBP";
private static final String SMPCurrencyCodeHUF = "SMPCurrencyCodeHUF";
private static final String SMPCurrencyCodeNOK = "SMPCurrencyCodeNOK";
private static final String SMPCurrencyCodePLN = "SMPCurrencyCodePLN";
private static final String SMPCurrencyCodeRON = "SMPCurrencyCodeRON";
private static final String SMPCurrencyCodeSEK = "SMPCurrencyCodeSEK";
private static final String SMPCurrencyCodeUSD = "SMPCurrencyCodeUSD";

private Promise mSumUpPromise;

public RNSumUpModule(ReactApplicationContext reactContext) {
Expand All @@ -47,6 +65,26 @@ public String getName() {
return "RNSumUp";
}

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("SMPCurrencyCodeBGN", SMPCurrencyCodeBGN);
constants.put("SMPCurrencyCodeBRL", SMPCurrencyCodeBRL);
constants.put("SMPCurrencyCodeCHF", SMPCurrencyCodeCHF);
constants.put("SMPCurrencyCodeCLP", SMPCurrencyCodeCLP);
constants.put("SMPCurrencyCodeCZK", SMPCurrencyCodeCZK);
constants.put("SMPCurrencyCodeDKK", SMPCurrencyCodeDKK);
constants.put("SMPCurrencyCodeEUR", SMPCurrencyCodeEUR);
constants.put("SMPCurrencyCodeGBP", SMPCurrencyCodeGBP);
constants.put("SMPCurrencyCodeHUF", SMPCurrencyCodeHUF);
constants.put("SMPCurrencyCodeNOK", SMPCurrencyCodeNOK);
constants.put("SMPCurrencyCodePLN", SMPCurrencyCodePLN);
constants.put("SMPCurrencyCodeRON", SMPCurrencyCodeRON);
constants.put("SMPCurrencyCodeSEK", SMPCurrencyCodeSEK);
constants.put("SMPCurrencyCodeUSD", SMPCurrencyCodeUSD);
return constants;
}

@ReactMethod
public void authenticate(String affiliateKey, Promise promise) {
mSumUpPromise = promise;
Expand Down Expand Up @@ -74,17 +112,35 @@ public void logout(Promise promise) {
mSumUpPromise.resolve(true);
}

private SumUpPayment.Currency getCurrency(String currency) {
switch (currency) {
case SMPCurrencyCodeBGN: return SumUpPayment.Currency.BGN;
case SMPCurrencyCodeBRL: return SumUpPayment.Currency.BRL;
case SMPCurrencyCodeCHF: return SumUpPayment.Currency.CHF;
case SMPCurrencyCodeCLP: return SumUpPayment.Currency.CLP;
case SMPCurrencyCodeCZK: return SumUpPayment.Currency.CZK;
case SMPCurrencyCodeDKK: return SumUpPayment.Currency.DKK;
case SMPCurrencyCodeEUR: return SumUpPayment.Currency.EUR;
case SMPCurrencyCodeGBP: return SumUpPayment.Currency.GBP;
case SMPCurrencyCodeHUF: return SumUpPayment.Currency.HUF;
case SMPCurrencyCodeNOK: return SumUpPayment.Currency.NOK;
case SMPCurrencyCodePLN: return SumUpPayment.Currency.PLN;
case SMPCurrencyCodeRON: return SumUpPayment.Currency.RON;
case SMPCurrencyCodeSEK: return SumUpPayment.Currency.SEK;
default: case SMPCurrencyCodeUSD: return SumUpPayment.Currency.USD;
}
}

@ReactMethod
public void checkout(String affiliateKey, String value, String name, Promise promise) {
// TODO: replace foreignTransactionId to transaction UUID sent by user.
public void checkout(ReadableMap request, Promise promise) {
// TODO: replace foreignTransactionId for transaction UUID sent by user.
mSumUpPromise = promise;
try {

SumUpPayment.Currency currencyCode = this.getCurrency(request.getString("currencyCode"));
SumUpPayment payment = SumUpPayment.builder()
.affiliateKey(affiliateKey)
.total(new BigDecimal(Double.parseDouble(value)))
.currency(SumUpPayment.Currency.BRL)
.title(name)
.total(new BigDecimal(Double.parseDouble(request.getString("totalAmount"))))
.currency(currencyCode)
.title(request.getString("title"))
.foreignTransactionId(UUID.randomUUID().toString())
.skipSuccessScreen()
.build();
Expand All @@ -104,7 +160,12 @@ public void preferences(Promise promise) {
@ReactMethod
public void isLoggedIn(Promise promise) {
WritableMap map = Arguments.createMap();
map.putBoolean("isLoggedIn", ((UserModel)CoreState.Instance().get(UserModel.class)).isLoggedIn());
if (CoreState.Instance() == null) {
map.putBoolean("isLoggedIn", false);
} else {
map.putBoolean("isLoggedIn", ((UserModel)CoreState.Instance().get(UserModel.class)).isLoggedIn());
}

promise.resolve(map);
}

Expand Down
23 changes: 21 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ const RNSumUpWrapper = NativeModules.RNSumUp;
const RNSumUp = {
apiKey: '',

paymentOptionAny: (Platform.OS === ios) ? RNSumUpWrapper.SMPPaymentOptionAny : null,
paymentOptionCardReader: (Platform.OS === ios) ? RNSumUpWrapper.SMPPaymentOptionCardReader : null,
paymentOptionMobilePayment: (Platform.OS === ios) ? RNSumUpWrapper.SMPPaymentOptionMobilePayment : null,

SMPCurrencyCodeBGN: RNSumUpWrapper.SMPCurrencyCodeBGN,
SMPCurrencyCodeBRL: RNSumUpWrapper.SMPCurrencyCodeBRL,
SMPCurrencyCodeCHF: RNSumUpWrapper.SMPCurrencyCodeCHF,
SMPCurrencyCodeCLP: (Platform.OS === 'android') ? RNSumUpWrapper.SMPCurrencyCodeCLP : null, // iOS SDK version currently doesn't supports this currency
SMPCurrencyCodeCZK: RNSumUpWrapper.SMPCurrencyCodeCZK,
SMPCurrencyCodeDKK: RNSumUpWrapper.SMPCurrencyCodeDKK,
SMPCurrencyCodeEUR: RNSumUpWrapper.SMPCurrencyCodeEUR,
SMPCurrencyCodeGBP: RNSumUpWrapper.SMPCurrencyCodeGBP,
SMPCurrencyCodeHUF: RNSumUpWrapper.SMPCurrencyCodeHUF,
SMPCurrencyCodeNOK: RNSumUpWrapper.SMPCurrencyCodeNOK,
SMPCurrencyCodePLN: RNSumUpWrapper.SMPCurrencyCodePLN,
SMPCurrencyCodeRON: RNSumUpWrapper.SMPCurrencyCodeRON,
SMPCurrencyCodeSEK: RNSumUpWrapper.SMPCurrencyCodeSEK,
SMPCurrencyCodeUSD: RNSumUpWrapper.SMPCurrencyCodeUSD,

setup(key) {
this.apiKey = key;
if (Platform.OS === 'ios') {
Expand All @@ -17,7 +36,7 @@ const RNSumUp = {
},

authenticateWithToken(token) {
return (Platform.OS === 'ios') ? RNSumUpWrapper.authenticate(token) : RNSumUpWrapper.authenticate(this.apiKey, token);
return (Platform.OS === 'ios') ? RNSumUpWrapper.authenticateWithToken(token) : RNSumUpWrapper.authenticateWithToken(this.apiKey, token);
},

logout() {
Expand All @@ -30,7 +49,7 @@ const RNSumUp = {
},

checkout(request) {
return (Platform.OS === 'ios') ? RNSumUpWrapper.checkout(request) : RNSumUpWrapper.checkout(this.apiKey, request.totalAmount, request.name);
return RNSumUpWrapper.checkout(request);
},

preferences() {
Expand Down
76 changes: 48 additions & 28 deletions ios/RNSumUp.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@ - (NSDictionary *)constantsToExport
{
return @{ @"SMPPaymentOptionAny": @(SMPPaymentOptionAny),
@"SMPPaymentOptionCardReader": @(SMPPaymentOptionCardReader),
@"SMPPaymentOptionMobilePayment": @(SMPPaymentOptionMobilePayment)};
@"SMPPaymentOptionMobilePayment": @(SMPPaymentOptionMobilePayment),
@"SMPCurrencyCodeBGN" : (SMPCurrencyCodeBGN),
@"SMPCurrencyCodeBRL" : (SMPCurrencyCodeBRL),
@"SMPCurrencyCodeCHF" : (SMPCurrencyCodeCHF),
@"SMPCurrencyCodeCZK" : (SMPCurrencyCodeCZK),
@"SMPCurrencyCodeDKK" : (SMPCurrencyCodeDKK),
@"SMPCurrencyCodeEUR" : (SMPCurrencyCodeEUR),
@"SMPCurrencyCodeGBP" : (SMPCurrencyCodeGBP),
@"SMPCurrencyCodeHUF" : (SMPCurrencyCodeHUF),
@"SMPCurrencyCodeNOK" : (SMPCurrencyCodeNOK),
@"SMPCurrencyCodePLN" : (SMPCurrencyCodePLN),
@"SMPCurrencyCodeRON" : (SMPCurrencyCodeRON),
@"SMPCurrencyCodeSEK" : (SMPCurrencyCodeSEK),
@"SMPCurrencyCodeUSD" : (SMPCurrencyCodeUSD)};
}

RCT_EXPORT_METHOD(setup:(NSString *)key resolver:(RCTPromiseResolveBlock)resolve
Expand Down Expand Up @@ -60,13 +73,19 @@ - (NSDictionary *)constantsToExport

RCT_EXPORT_METHOD(authenticateWithToken:(NSString *)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[SMPSumUpSDK loginWithToken:token completion:^(BOOL success, NSError * _Nullable error) {
if (!success) {
reject(@"004", @"It was not possible to login with SumUp using a token. Please, try again.", nil);
} else {
resolve(nil);
}
}];
BOOL isLoggedIn = [SMPSumUpSDK isLoggedIn];
if (isLoggedIn) {
resolve(nil);
} else {
NSString *aToken = token;
[SMPSumUpSDK loginWithToken:aToken completion:^(BOOL success, NSError * _Nullable error) {
if (!success) {
reject(@"004", @"It was not possible to login with SumUp using a token. Please, try again.", nil);
} else {
resolve(nil);
}
}];
}
}

RCT_EXPORT_METHOD(logout:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
Expand Down Expand Up @@ -105,18 +124,18 @@ - (NSDictionary *)constantsToExport
UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
[SMPSumUpSDK checkoutWithRequest:checkoutRequest
fromViewController:rootViewController
completion:^(SMPCheckoutResult *result, NSError *error) {
if (error) {
reject(@"001", @"It was not possible to perform checkout with SumUp. Please, try again.", error);
} else {
NSDictionary *additionalInformation = [result additionalInfo];
NSString *cardType = [additionalInformation valueForKeyPath:@"card.type"];
NSString *cardLast4Digits = [additionalInformation valueForKeyPath:@"card.last_4_digits"];
NSString *installments = [additionalInformation valueForKeyPath:@"installments"];

resolve(@{@"success": @([result success]), @"transactionCode": [result transactionCode], @"additionalInfo": @{ @"cardType": cardType, @"cardLast4Digits": cardLast4Digits, @"installments": installments }});
}
}];
completion:^(SMPCheckoutResult *result, NSError *error) {
if (error) {
reject(@"001", @"It was not possible to perform checkout with SumUp. Please, try again.", error);
} else {
NSDictionary *additionalInformation = [result additionalInfo];
NSString *cardType = [additionalInformation valueForKeyPath:@"card.type"];
NSString *cardLast4Digits = [additionalInformation valueForKeyPath:@"card.last_4_digits"];
NSString *installments = [additionalInformation valueForKeyPath:@"installments"];

resolve(@{@"success": @([result success]), @"transactionCode": [result transactionCode], @"additionalInfo": @{ @"cardType": cardType, @"cardLast4Digits": cardLast4Digits, @"installments": installments }});
}
}];
});
}

Expand All @@ -125,14 +144,14 @@ - (NSDictionary *)constantsToExport
dispatch_sync(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
[SMPSumUpSDK presentCheckoutPreferencesFromViewController:rootViewController
animated:YES
completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
resolve(nil);
} else {
reject(@"002", @"It was not possible to open Preferences window. Please, try again.", nil);
}
}];
animated:YES
completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
resolve(nil);
} else {
reject(@"002", @"It was not possible to open Preferences window. Please, try again.", nil);
}
}];
});
}

Expand All @@ -146,3 +165,4 @@ - (NSDictionary *)constantsToExport
RCT_EXPORT_MODULE();

@end

0 comments on commit 7fed1c8

Please sign in to comment.