Created
July 2, 2020 14:14
-
-
Save wesleyguirra/3f22c62f1491ea8b9bbd5226b474e40b to your computer and use it in GitHub Desktop.
Classe com a implementação do métodos nativos em Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.enjoyin.flutter_paystore_client.flutter_paystore_client; | |
import androidx.annotation.NonNull; | |
import android.util.Log; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.math.BigDecimal; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Objects; | |
import br.com.phoebus.android.payments.api.ApplicationInfo; | |
import br.com.phoebus.android.payments.api.Credentials; | |
import br.com.phoebus.android.payments.api.ErrorData; | |
import br.com.phoebus.android.payments.api.Payment; | |
import br.com.phoebus.android.payments.api.PaymentClient; | |
import br.com.phoebus.android.payments.api.PaymentRequest; | |
import br.com.phoebus.android.payments.api.PaymentRequestV2; | |
import br.com.phoebus.android.payments.api.PaymentType; | |
import br.com.phoebus.android.payments.api.PaymentV2; | |
import br.com.phoebus.android.payments.api.ReversePayment; | |
import br.com.phoebus.android.payments.api.ReversePaymentRequest; | |
import br.com.phoebus.android.payments.api.client.Client; | |
import br.com.phoebus.android.payments.api.exception.ClientException; | |
import io.flutter.plugin.common.MethodCall; | |
import io.flutter.plugin.common.MethodChannel; | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | |
import io.flutter.plugin.common.MethodChannel.Result; | |
import io.flutter.plugin.common.PluginRegistry.Registrar; | |
/** FlutterPaystoreClientPlugin */ | |
public class FlutterPaystoreClientPlugin implements MethodCallHandler { | |
private final PaymentClient paymentClient = new PaymentClient(); | |
private final Registrar registrar; | |
private FlutterPaystoreClientPlugin(Registrar registrar) { | |
this.registrar = registrar; | |
} | |
/** Plugin registration. */ | |
public static void registerWith(Registrar registrar) { | |
final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_paystore_client"); | |
channel.setMethodCallHandler(new FlutterPaystoreClientPlugin(registrar)); | |
} | |
@Override | |
public void onMethodCall(final @NonNull MethodCall call, final @NonNull Result result) { | |
if (call.method.startsWith("startPayment")) { | |
List<String> paymentTypeNames = call.argument("paymentTypes"); | |
List<PaymentType> paymentTypes = new ArrayList<>(); | |
for (String name : paymentTypeNames) { | |
paymentTypes.add(PaymentType.valueOf(name)); | |
} | |
@NonNull | |
String applicationId = Objects.requireNonNull(call.argument("applicationId").toString()); | |
@NonNull | |
String token = Objects.requireNonNull(call.argument("token").toString()); | |
@NonNull | |
double value = Double.parseDouble(Objects.requireNonNull(call.argument("value").toString())); | |
@NonNull | |
String appVersion = Objects.requireNonNull(call.argument("appVersion").toString()); | |
@NonNull | |
String transactionId = Objects.requireNonNull(call.argument("transactionId").toString()); | |
ApplicationInfo appInfo = new ApplicationInfo(); | |
Credentials credentials = new Credentials(); | |
if (call.method.equals("startPaymentV2")) { | |
@NonNull | |
String tokenizeEmail = Objects.requireNonNull(call.argument("tokenizeEmail").toString()); | |
PaymentRequestV2 paymentRequestV2 = new PaymentRequestV2(); | |
credentials.setApplicationId(applicationId); | |
credentials.setSecretToken(token); | |
appInfo.setCredentials(credentials); | |
appInfo.setSoftwareVersion(appVersion); | |
paymentRequestV2.setValue(new BigDecimal(value)); | |
paymentRequestV2.setAppTransactionId(transactionId); | |
paymentRequestV2.setAppInfo(appInfo); | |
paymentRequestV2.setTokenizeEmail(tokenizeEmail); | |
try { | |
paymentClient.startPaymentV2(paymentRequestV2, new PaymentClient.PaymentCallback<PaymentV2>() { | |
@Override | |
public void onSuccess(PaymentV2 paymentV2) { | |
Log.d("FlutterPaystoreClient", "Payment v2 Success"); | |
result.success(paymentV2); | |
} | |
@Override | |
public void onError(ErrorData errorData) { | |
Log.e("FlutterPaystoreClient", errorData.getResponseMessage()); | |
result.error(errorData.getResponseMessage(), "", errorData); | |
} | |
}); | |
} catch (ClientException e) { | |
result.error(e.getMessage(), e.getLocalizedMessage(), e); | |
} | |
} | |
if (call.method.equals("startPayment")) { | |
PaymentRequest paymentRequest = new PaymentRequest(); | |
credentials.setApplicationId(applicationId); | |
credentials.setSecretToken(token); | |
appInfo.setCredentials(credentials); | |
appInfo.setSoftwareVersion(appVersion); | |
paymentRequest.setValue(new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_EVEN)); | |
paymentRequest.setAppTransactionId(transactionId); | |
paymentRequest.setAppInfo(appInfo); | |
paymentRequest.setPaymentTypes(paymentTypes); | |
try { | |
paymentClient.startPayment(paymentRequest, new PaymentClient.PaymentCallback<Payment>() { | |
@Override | |
public void onSuccess(final Payment payment) { | |
Log.d("FlutterPaystoreClient", "Payment Success"); | |
try { | |
paymentClient.confirmPayment(payment.getPaymentId(), new PaymentClient.PaymentCallback<Payment>() { | |
@Override | |
public void onSuccess(Payment paymentConfirmation) { | |
Log.d("FlutterPaystoreClient", "Payment Confirmed"); | |
Map paymentMap = new ObjectMapper().convertValue(payment, Map.class); | |
result.success(paymentMap); | |
} | |
@Override | |
public void onError(ErrorData errorData) { | |
Log.e("FlutterPaystoreClient", errorData.getResponseMessage()); | |
} | |
}); | |
} catch (ClientException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onError(ErrorData errorData) { | |
Log.e("FlutterPaystoreClient", errorData.getResponseMessage()); | |
Map errorDataMap = new ObjectMapper().convertValue(errorData, Map.class); | |
result.error(errorData.getResponseMessage(), errorData.getPaymentsResponseCode(), errorDataMap); | |
} | |
}); | |
} catch (ClientException e) { | |
Log.e("FlutterPaystoreClient", e.getMessage()); | |
} | |
} | |
} else if (call.method.equals("bindClient")) { | |
paymentClient.bind(registrar.context(), new Client.OnConnectionCallback() { | |
@Override | |
public void onConnected() { | |
Log.d("FlutterPaystoreClient", "Connected"); | |
result.success("Connected"); | |
} | |
@Override | |
public void onDisconnected(boolean b) { | |
Log.e("FlutterPaystoreClient", "Disconnected"); | |
result.error("Disconnected", "", b); | |
} | |
}); | |
} else if (call.method.equals("reversePayment")) { | |
@NonNull | |
String applicationId = Objects.requireNonNull(call.argument("applicationId").toString()); | |
@NonNull | |
String paymentId = Objects.requireNonNull(call.argument("paymentId").toString()); | |
@NonNull | |
String token = Objects.requireNonNull(call.argument("token").toString()); | |
@NonNull | |
double value = Double.parseDouble(Objects.requireNonNull(call.argument("value").toString())); | |
@NonNull | |
String appVersion = Objects.requireNonNull(call.argument("appVersion").toString()); | |
@NonNull | |
String transactionId = Objects.requireNonNull(call.argument("transactionId").toString()); | |
ApplicationInfo appInfo = new ApplicationInfo(); | |
Credentials credentials = new Credentials(); | |
ReversePaymentRequest reversePaymentRequest = new ReversePaymentRequest(); | |
credentials.setApplicationId(applicationId); | |
credentials.setSecretToken(token); | |
appInfo.setCredentials(credentials); | |
appInfo.setSoftwareVersion(appVersion); | |
reversePaymentRequest.setValue(new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_EVEN)); | |
reversePaymentRequest.setPaymentId(paymentId); | |
reversePaymentRequest.setAppTransactionId(transactionId); | |
reversePaymentRequest.setCredentials(credentials); | |
try { | |
paymentClient.reversePayment(reversePaymentRequest, new PaymentClient.PaymentCallback<ReversePayment>() { | |
@Override | |
public void onSuccess(ReversePayment reversePayment) { | |
Log.d("FlutterPaystoreClient", "Payment Success"); | |
Map paymentMap = new ObjectMapper().convertValue(reversePayment, Map.class); | |
result.success(paymentMap); | |
} | |
@Override | |
public void onError(ErrorData errorData) { | |
Log.e("FlutterPaystoreClient", errorData.getResponseMessage()); | |
Map errorDataMap = new ObjectMapper().convertValue(errorData, Map.class); | |
result.error(errorData.getResponseMessage(), errorData.getPaymentsResponseCode(), errorDataMap); | |
} | |
}); | |
} catch (ClientException e) { | |
result.error(e.getMessage(), e.getLocalizedMessage(), e); | |
} | |
} else { | |
result.notImplemented(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment