Skip to content

Commit

Permalink
Merge pull request #22 from d3x773r/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
williaanlopes committed Oct 22, 2020
2 parents 62519ab + 533c03b commit 5f38874
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void connectPinPad(JSONArray jsonArray, CallbackContext callbackContext)
pinPadObject.put("macAddress", pinPad.getAddress());
jsonObject.put("pinPad", pinPadObject);

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
connectPinPadCallback.sendPluginResult(pluginResult);
openConnection(jsonArray, callbackContext);
Expand Down Expand Up @@ -405,7 +405,7 @@ public void receiveInitialization() {
jsonObject.put("code", Constants.INITIALIZED_SUCCESSFULLY);
jsonObject.put("message", "initialized");

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
openConnectionCallback.sendPluginResult(pluginResult);

Expand All @@ -432,7 +432,7 @@ public void receiveNotification(String notification) {
jsonObject.put("code", Constants.PIN_PAD_CANCELED);
jsonObject.put("message", "pin pad by canceled");

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
callbackContext.error(jsonObject);
} catch (JSONException e) {
Expand All @@ -447,7 +447,7 @@ public void receiveOperationCompleted() {
jsonObject.put("code", 20);
jsonObject.put("message", "done");

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
callbackContext.sendPluginResult(pluginResult);
} catch (JSONException e) {
Expand All @@ -465,7 +465,7 @@ public void receiveTableUpdated(boolean loaded) {
jsonObject.put("message", "received table updated");
jsonObject.put("loaded", loaded);

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
downloadTablesCallback.sendPluginResult(pluginResult);
} catch (JSONException e) {
Expand All @@ -484,7 +484,7 @@ public void receiveFinishTransaction() {
jsonObject.put("code", 20);
jsonObject.put("message", "transaction approved");

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
payCallback.sendPluginResult(pluginResult);
} catch (JSONException e) {
Expand All @@ -501,7 +501,7 @@ public void receiveClose() {
jsonObject.put("code", 20);
jsonObject.put("message", "close connection");

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
callbackContext.sendPluginResult(pluginResult);
} catch (JSONException e) {
Expand All @@ -525,7 +525,7 @@ public void receiveCardHash(String cardHash, MposPaymentResult result) {
jsonObject.put("message", "delegate remote api");
jsonObject.put("data", charge.toJson());

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
callbackContext.sendPluginResult(pluginResult);
} catch (JSONException e) {
Expand Down Expand Up @@ -701,7 +701,7 @@ private void pay(JSONArray jsonArray, CallbackContext callbackContext) throws JS
jsonObject.put("code", Constants.PAYMENT_ERROR_PARAMETERS);
jsonObject.put("message", "wrong parameters");

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
payCallback.error(jsonObject);
} catch (JSONException ex) {
Expand All @@ -721,7 +721,7 @@ private void pay(JSONArray jsonArray, CallbackContext callbackContext) throws JS
jsonObject.put("code", Constants.PAYMENT_ERROR_PARAMETERS);
jsonObject.put("message", "wrong parameters");

PluginResult pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
PluginResult pluginResult = ResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
pluginResult.setKeepCallback(false);
payCallback.error(jsonObject);
} catch (JSONException ex) {
Expand Down Expand Up @@ -811,7 +811,7 @@ private void close(String message) {

private void callRemoteServer(Charge charg) {

AndroidNetworking.post(configParameter.getRemoteApi())
/*AndroidNetworking.post(configParameter.getRemoteApi())
.addHeaders("X-Api-Key", charge.getApiKey())
.addHeaders("Authorization", charge.getToken())
.addJSONObjectBody(charge.toJson())
Expand Down Expand Up @@ -877,7 +877,7 @@ public void onError(ANError error) {
} catch (JSONException e) {
}
}
});
});*?
}
public PluginResult makePluginResult(PluginResult.Status status, Object... args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.gurpster.cordova.pagarme.mpos.PaymentParameter;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.ANRequest;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.gurpster.cordova.pagarme.mpos.MposCallback;
import com.gurpster.cordova.pagarme.mpos.entity.Charge;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import com.gurpster.cordova.pagarme.mpos.entity.Message;
import com.gurpster.cordova.pagarme.mpos.entity.response.Response;
import com.leve.ai.R;
import me.pagar.mposandroid.Mpos;
import me.pagar.mposandroid.MposPaymentResult;
import me.pagar.mposandroidexample.listeners.MposCallback;
import com.gurpster.cordova.pagarme.mpos.entity.Charge;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

/**
* Created by Williaan Souza (dextter) on 16/10/2020
Expand All @@ -36,6 +38,7 @@ public class MposService extends Service {
private Mpos mpos;
public static boolean isRunning;
public static boolean isMposConnected;
public static boolean isPaymentInProgress;

@Override
public IBinder onBind(Intent intent) {
Expand Down Expand Up @@ -108,7 +111,7 @@ public void bluetoothConnected() {
@Override
public void receiveInitialization() {
super.receiveInitialization();
display(getString(R.string.pin_pad_name));
display(getString(R.string.app_name));
mpos.downloadEMVTablesToDevice(false);
display("configurando...", 1000);
}
Expand All @@ -117,7 +120,7 @@ public void receiveInitialization() {
public void receiveTableUpdated(boolean b) {
super.receiveTableUpdated(b);
EventBus.getDefault().post(new ConnectedEvent());
display(getString(R.string.pin_pad_name));
display(getString(R.string.app_name));
}

@Override
Expand All @@ -136,7 +139,7 @@ public void receiveError(int i) {
super.receiveError(i);
String message = Message.getErrorFromCode(i);
display(message);
display(getString(R.string.pin_pad_name), 7000);
display(getString(R.string.app_name), 7000);
EventBus.getDefault().post(new FinishEvent(true, message));
}

Expand All @@ -146,35 +149,48 @@ public void receiveCardHash(String cardHash, MposPaymentResult mposPaymentResult
charge.setCardHash(cardHash);
charge.setOnline(mposPaymentResult.isOnline);
callRemoteServer(charge);
display("processando...");
}
});
mpos.payAmount(
charge.getAmountInt(),
charge.getEmvApplications(),
charge.getPaymentMethod()
);
isPaymentInProgress = true;
display("processando...");
}

private void callRemoteServer(final Charge charge) {
ANRequest.PostRequestBuilder<?> requestBuilder = AndroidNetworking.post(charge.getRemoteApi());

// requestBuilder.addHeaders(charge.getParams());
requestBuilder.addHeaders("X-Api-Key", charge.getApiKey());
requestBuilder.addHeaders("Authorization", charge.getToken());
ANRequest.PostRequestBuilder<?> requestBuilder;
switch (charge.getRemoteApi().getType().toLowerCase()) {
case "put":
requestBuilder = AndroidNetworking.put(charge.getRemoteApi().getUrl());
break;
case "patch":
requestBuilder = AndroidNetworking.patch(charge.getRemoteApi().getUrl());
break;
default:
requestBuilder = AndroidNetworking.post(charge.getRemoteApi().getUrl());
break;
}

// requestBuilder.addBodyParameter(charge.getParams());
requestBuilder.addJSONObjectBody(charge.toJson());
// add headers
requestBuilder.addHeaders(charge.getRemoteApi().getHeaders());
// add body params
requestBuilder.addBodyParameter(charge.getRemoteApi().getParams());

requestBuilder.setPriority(Priority.HIGH);
ANRequest<?> request = requestBuilder.build();
request.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
// Response res = JSON.parseObject(
// response.toString(),
// Response.class
// );
isPaymentInProgress = false;
Response res = JSON.parseObject(
response.toString(),
Response.class
);
try {
JSONObject jsonObject = response.getJSONObject("data");
if (charge.isOnline()) {
Expand All @@ -195,15 +211,18 @@ public void onResponse(JSONObject response) {

display("finalizado");
display("remova o cartao", 2500);
display(getString(R.string.pin_pad_name), 5500);
display(getString(R.string.app_name), 5500);

}

@Override
public void onError(ANError error) {
Log.d("", error.getErrorBody());
EventBus.getDefault().post(new FinishEvent(true, "erro de conexao"));
isPaymentInProgress = false;
Log.d("", error.getMessage());
display("erro de conexao");
display(getString(R.string.pin_pad_name), 5000);
display("remova o cartao", 2500);
display(getString(R.string.app_name), 5000);
EventBus.getDefault().post(new FinishEvent(true, "erro de conexao"));
}
});
}
Expand Down

0 comments on commit 5f38874

Please sign in to comment.