Skip to content

Commit

Permalink
Compatibility of Android SDK and iOS SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
ital0 committed Mar 6, 2018
1 parent 5316001 commit 36835fd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
74 changes: 45 additions & 29 deletions android/src/main/java/com/nextar/sumup/RNSumUpModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
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;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.sumup.merchant.api.SumUpAPI;
import com.sumup.merchant.api.SumUpLogin;
import com.sumup.merchant.api.SumUpPayment;
import com.sumup.merchant.CoreState;
import com.sumup.merchant.Models.UserModel;

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

/**
* Created by Robert Suman on 22/02/2018.
* Updated by Ítalo Menezes :)
*/

public class RNSumUpModule extends ReactContextBaseJavaModule {
Expand All @@ -43,18 +47,15 @@ public String getName() {
}

@ReactMethod
public void toastString(String text) {
Toast.makeText(getReactApplicationContext(), text, Toast.LENGTH_SHORT).show();
}

@ReactMethod
public void authenticate(String affiliateKey) {
public void authenticate(String affiliateKey, Promise promise) {
mSumUpPromise = promise;
SumUpLogin sumupLogin = SumUpLogin.builder(affiliateKey).build();
SumUpAPI.openLoginActivity(getCurrentActivity(), sumupLogin, REQUEST_CODE_LOGIN);
}

@ReactMethod
public void prepareForCheckout() {
public void prepareForCheckout(Promise promise) {
mSumUpPromise = promise;
SumUpAPI.prepareForCheckout();
}

Expand All @@ -66,65 +67,80 @@ public void logout(Promise promise) {
}

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

SumUpPayment payment = SumUpPayment.builder()
.affiliateKey(affiliateKey)
.total(new BigDecimal(value)) // minimum 1.00
.total(new BigDecimal(value))
.currency(SumUpPayment.Currency.BRL)
.title(name)
// optional: foreign transaction ID, must be unique!
.foreignTransactionId(UUID.randomUUID().toString()) // can not exceed 128 chars
.foreignTransactionId(UUID.randomUUID().toString())
.skipSuccessScreen()
.build();
SumUpAPI.checkout(getCurrentActivity(), payment, REQUEST_CODE_PAYMENT);
}
catch (Exception ex) {
} catch (Exception ex) {
mSumUpPromise.reject(ex);
mSumUpPromise = null;
}
}

public void preferences() {
SumUpAPI.openPaymentSettingsActivity(getCurrentActivity(), 3);
@ReactMethod
public void preferences(Promise promise) {
mSumUpPromise = promise;
SumUpAPI.openPaymentSettingsActivity(getCurrentActivity(), REQUEST_CODE_PAYMENT_SETTINGS);
}

@ReactMethod
public void isLoggedIn(Promise promise) {
WritableMap map = Arguments.createMap();
map.putBoolean("isLoggedIn", ((UserModel)CoreState.Instance().get(UserModel.class)).isLoggedIn());
promise.resolve(map);
}

private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {

switch (requestCode) {
case REQUEST_CODE_LOGIN:
if (data != null) {
Bundle extra = data.getExtras();
String text = "Result code: " + extra.getInt(SumUpAPI.Response.RESULT_CODE) + "Message: " + extra.getString(SumUpAPI.Response.MESSAGE);
//Toast.makeText(getReactApplicationContext(), text, Toast.LENGTH_SHORT).show();
SumUpAPI.openPaymentSettingsActivity(getCurrentActivity(), REQUEST_CODE_PAYMENT_SETTINGS);
if (extra.getInt(SumUpAPI.Response.RESULT_CODE) == REQUEST_CODE_LOGIN) {
WritableMap map = Arguments.createMap();
map.putBoolean("success", true);
mSumUpPromise.resolve(map);
} else {
mSumUpPromise.reject(extra.getString(SumUpAPI.Response.RESULT_CODE), extra.getString(SumUpAPI.Response.MESSAGE));
}
}
break;

case REQUEST_CODE_PAYMENT:
if (data != null) {
Bundle extra = data.getExtras();
if (mSumUpPromise != null) {
Bundle extra = data.getExtras();
if (extra.getInt(SumUpAPI.Response.RESULT_CODE) == TRANSACTION_SUCCESSFUL)
mSumUpPromise.resolve(extra.getString(SumUpAPI.Response.MESSAGE));
else
mSumUpPromise.reject(extra.getString(SumUpAPI.Response.RESULT_CODE),extra.getString(SumUpAPI.Response.MESSAGE));
if (extra.getInt(SumUpAPI.Response.RESULT_CODE) == TRANSACTION_SUCCESSFUL) {
WritableMap map = Arguments.createMap();
map.putBoolean("success", true);
map.putString("transactionCode", extra.getString(SumUpAPI.Response.TX_CODE));
mSumUpPromise.resolve(map);
}else
mSumUpPromise.reject(extra.getString(SumUpAPI.Response.RESULT_CODE), extra.getString(SumUpAPI.Response.MESSAGE));
}
}
break;

case REQUEST_CODE_PAYMENT_SETTINGS:
WritableMap map = Arguments.createMap();
map.putBoolean("success", true);
mSumUpPromise.resolve(map);
break;
default:
break;
}
}

};
}


4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const RNSumUpWrapper = NativeModules.RNSumUp;

const RNSumUp = {
apiKey: '',
isLoggedIn: false,

setup(key) {
this.apiKey = key;
Expand All @@ -18,6 +17,7 @@ const RNSumUp = {
},

logout() {
this.isLoggedIn = false;
return RNSumUpWrapper.logout();
},

Expand All @@ -34,7 +34,7 @@ const RNSumUp = {
},

isLoggedIn() {
return (Platform.OS === 'ios') ? RNSumUpWrapper.isLoggedIn() : this.isLoggedIn();
return RNSumUpWrapper.isLoggedIn();
}
};

Expand Down

0 comments on commit 36835fd

Please sign in to comment.