mariusbackes / cordova-plugin-sumup

:pager: :moneybag: Cordova plugin for SumUp SDK integration
5 stars 18 forks source link

Programatically set SUMUP_API_KEY #1

Closed steffanhalv closed 4 years ago

steffanhalv commented 5 years ago

Hi, this project looks great. :)

I just have some questions:

Can the SUMUP_API_KEY be set programatically within the cordova app or does it have to be set in the JAVA or Objective-C code? I see you have this set as an argument on installation, but we need to set this programatically using javascript. (The API KEY should be able to update on runtime for our use case)

Do you have any idea when we can expect an IOS version? Will this only work for IONIC apps or also for other kind of cordova apps?

Thank you very much! @mariusbackes

galash13 commented 4 years ago

It is possible to programmatically set the API key, you need to modify the code as below (should work on ionic v1 but i've only modified the login function and only on android as on ios the plugin doesnt work at all):

FILE: www/sumup.js change:

login: (accessToken, success, failure) => {
    cordova.exec(
      success,
      failure,
      CLASS,
      "login",
      accessToken ? [accessToken] : []
    );
  },

to:

login: (params, success, failure) => {
    cordova.exec(
      success,
      failure,
      CLASS,
      "login",
      params
    );
  },

FILE: src/android/sumup.java change:

String affiliateKey = this.cordova.getActivity().getString(cordova.getActivity().getResources()
                .getIdentifier("SUMUP_API_KEY", "string", cordova.getActivity().getPackageName()));

To: String affiliateKey = args.get(0).toString();

and

private boolean login(String affiliateKey, JSONArray args, CallbackContext callbackContext) {
        Runnable runnable = () -> {
            Object accessToken = null;
            try {
                accessToken = args.get(0);

to

private boolean login(String affiliateKey, JSONArray args, CallbackContext callbackContext) {
        Runnable runnable = () -> {
            Object accessToken = null;
            try {
                accessToken = args.get(1);

you can then call the login function like this:

var params = [
      <AFFILIATE_KEY>,
      <ACCESS_TOKEN>
]
SumUp.login(
     params,
      function(success) {
        console.log(JSON.stringify(success));
      },
      function(error) {
        console.log(JSON.stringify(error));
      }
    );

it should work but haven't fully tested it so it might need some more work, @mariusbackes

mariusbackes commented 4 years ago

Hey, I have edited the plugin. You can pass your SUMUP_API_KEY now from JavaScript and change it on runtime.

For a login you have now to use an object:

SumUpKeys {
    accessToken: string;
    affiliateKey: string;
}

const sumUpKeys = new SumUpKeys();
sumUpKeys.affiliateKey = "YOUR_AFFILIATE_KEY";
sumUpKeys.accessToken = "YOUR_ACCESS_TOKEN";
this.sumUp.login(sumUpKeys);

Both values are empty strings. You can set your accessToken or affiliateKey just like you need it.

Reinstall the plugin like this:

cordova plugin add cordova-sumup-plugin --variable SUMUP_API_KEY=""

Make sure that SUMUP_API_KEY value is an empty string.

You can also use this Plugin with pure JavaScript and Cordova. You don't need an Ionic based Project.

To reference the methods from the plugin just call window.SumUp.[methodName]