Advertisement
maneesf

Hyperpay.java

Sep 8th, 2022
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. package com.melkior;
  2.  
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.net.Uri;
  8. import android.os.IBinder;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11. import androidx.annotation.NonNull;
  12.  
  13. import com.facebook.react.bridge.Arguments;
  14. import com.facebook.react.bridge.Promise;
  15. import com.facebook.react.bridge.ReactApplicationContext;
  16. import com.facebook.react.bridge.ReactContextBaseJavaModule;
  17. import com.facebook.react.bridge.ReactMethod;
  18. import com.facebook.react.bridge.ReadableMap;
  19. import com.facebook.react.bridge.WritableMap;
  20. import com.facebook.react.modules.core.DeviceEventManagerModule;
  21. import com.oppwa.mobile.connect.exception.PaymentError;
  22. import com.oppwa.mobile.connect.payment.BrandsValidation;
  23. import com.oppwa.mobile.connect.payment.CheckoutInfo;
  24. import com.oppwa.mobile.connect.payment.ImagesRequest;
  25. import com.oppwa.mobile.connect.payment.card.CardPaymentParams;
  26. import com.oppwa.mobile.connect.payment.token.TokenPaymentParams;
  27. import com.oppwa.mobile.connect.provider.ITransactionListener;
  28. import com.oppwa.mobile.connect.provider.Transaction;
  29. import com.oppwa.mobile.connect.provider.TransactionType;
  30. import com.oppwa.mobile.connect.service.IProviderBinder;
  31. import com.oppwa.mobile.connect.service.ConnectService;
  32. import com.oppwa.mobile.connect.provider.Connect;
  33. import com.oppwa.mobile.connect.exception.PaymentException;
  34. import java.lang.reflect.Method;
  35.  
  36. public class HyperpayModule extends ReactContextBaseJavaModule implements ITransactionListener {
  37.  
  38.     private IProviderBinder binder;
  39.     private Context mApplicationContext;
  40.     private Intent bindIntent;
  41.  
  42.     public HyperpayModule(@NonNull ReactApplicationContext reactContext) {
  43.         super(reactContext);
  44.  
  45.         mApplicationContext = reactContext.getApplicationContext();
  46.         Intent intent = new Intent(mApplicationContext, ConnectService.class);
  47.  
  48.         mApplicationContext.startService(intent);
  49.         mApplicationContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
  50.     }
  51.     public void unBindService() {
  52.         if (serviceConnection != null) {
  53.             // Unbind from the In-app Billing service when we are done
  54.             // Otherwise, the open service connection could cause the device’s performance
  55.             // to degrade
  56.             mApplicationContext.unbindService(serviceConnection);
  57.         }
  58.     }
  59.     private ServiceConnection serviceConnection = new ServiceConnection() {
  60.         @Override
  61.         public void onServiceConnected(ComponentName name, IBinder service) {
  62.             binder = (IProviderBinder) service;
  63.             /* we have a connection to the service */
  64.             try {
  65.                 binder.initializeProvider(Connect.ProviderMode.TEST);
  66.             } catch (PaymentException ee) {
  67.                 /* error occurred */
  68.                 Log.d("BinderException", ee.getMessage());
  69.             }
  70.         }
  71.  
  72.         @Override
  73.         public void onServiceDisconnected(ComponentName name) {
  74.             binder = null;
  75.         }
  76.  
  77.     };
  78.  
  79.     @NonNull
  80.     @Override
  81.     public String getName() {
  82.         return "Hyperpay";
  83.     }
  84.  
  85.     @ReactMethod
  86.     public void transactionPayment(ReadableMap options, boolean withToken, Promise promise) {
  87.         // promiseModule = promise;
  88.  
  89.         try {
  90.  
  91.             if(withToken){
  92.                 Log.d("ReactNative", "withToken true");
  93.                 TokenPaymentParams tokenPaymentParams = new TokenPaymentParams(
  94.                     options.getString("checkoutID"),
  95.                     options.getString("tokenID"),
  96.                     options.getString("paymentBrand"),
  97.                     options.getString("cvv")
  98.                 );
  99.  
  100.                 tokenPaymentParams.setShopperResultUrl("melkior://product");
  101.                 Transaction transaction = null;
  102.  
  103.                 try {
  104.  
  105.                     transaction = new Transaction(tokenPaymentParams);
  106.                     // Log.d("Hyeprpay", transaction);
  107.                     binder.submitTransaction(transaction);
  108.                     binder.addTransactionListener(HyperpayModule.this);
  109.                     promise.resolve(null);
  110.                     Log.d("ReactNative", "android call");
  111.                 } catch (PaymentException ee) {
  112.                     Log.d("TransactionError", ee.getMessage());
  113.                     promise.reject(null, ee.getMessage());
  114.                 }
  115.             } else {
  116.  
  117.                 boolean isTokenizationEnabled = true;
  118.                 CardPaymentParams cardPaymentParams = new CardPaymentParams(
  119.                     options.getString("checkoutID"),
  120.                     options.getString("paymentBrand"),
  121.                     options.getString("cardNumber"),
  122.                     options.getString("holderName"),
  123.                     options.getString("expiryMonth"),
  124.                     options.getString("expiryYear"),
  125.                     options.getString("cvv")
  126.                 );
  127.  
  128.                 cardPaymentParams.setTokenizationEnabled(isTokenizationEnabled);
  129.                 cardPaymentParams.setShopperResultUrl("melkior://product");
  130.                 Transaction transaction = null;
  131.  
  132.                 try {
  133.  
  134.                     transaction = new Transaction(cardPaymentParams);
  135.                     // Log.d("Hyeprpay", transaction);
  136.                     // binder.submitTransaction(transaction);
  137.                     binder.registerTransaction(transaction);
  138.                     binder.addTransactionListener(HyperpayModule.this);
  139.                     promise.resolve(null);
  140.                     Log.d("ReactNative", "android call");
  141.                 } catch (PaymentException ee) {
  142.                     Log.d("TransactionError", ee.getMessage());
  143.                     promise.reject(null, ee.getMessage());
  144.                 }
  145.             }
  146.         } catch (PaymentException e) {
  147.             Log.d("Payment setup error", e.getMessage());
  148.             promise.reject(null, e.getMessage());
  149.         }
  150.  
  151.     }
  152.  
  153.     @Override
  154.     public void brandsValidationRequestSucceeded(BrandsValidation brandsValidation) {
  155.  
  156.     }
  157.  
  158.     @Override
  159.     public void brandsValidationRequestFailed(PaymentError paymentError) {
  160.  
  161.     }
  162.  
  163.     @Override
  164.     public void imagesRequestSucceeded(ImagesRequest imagesRequest) {
  165.  
  166.     }
  167.  
  168.     @Override
  169.     public void imagesRequestFailed() {
  170.  
  171.     }
  172.  
  173.     @Override
  174.     public void paymentConfigRequestSucceeded(CheckoutInfo checkoutInfo) {
  175.         String resourcePath = checkoutInfo.getResourcePath();
  176.         Log.d("ResourcePath", resourcePath);
  177.     }
  178.  
  179.     @Override
  180.     public void paymentConfigRequestFailed(PaymentError paymentError) {
  181.         Log.d("ResourcePathFailed", "line 157");
  182.     }
  183.  
  184.     @Override
  185.     public void transactionCompleted(Transaction transaction) {
  186.  
  187.         WritableMap data = Arguments.createMap();
  188.         Log.d("Hyperpay after argumets", "transaction complete");
  189.         if (transaction.getTransactionType() == TransactionType.ASYNC){
  190. //            Intent viewIntent =
  191. //                    new Intent("android.intent.action.VIEW",
  192. //                            Uri.parse("http://www.stackoverflow.com/"));
  193.  
  194.             data.putString("redirectUrl", transaction.getRedirectUrl());
  195.             data.putString("status", "pending");
  196.         }else{
  197.             data.putString("status", "completed");
  198.         }
  199.         // shoud add listner
  200.  
  201.  
  202.  
  203.         data.putString("checkoutID", transaction.getPaymentParams().getCheckoutId());
  204.         data.putString("url", transaction.getTransactionType().toString());
  205.  
  206.         getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  207.             .emit("transactionStatus", data);
  208.  
  209.         // promiseModule.resolve(transaction);
  210.  
  211.     }
  212.  
  213.     @Override
  214.     public void transactionFailed(Transaction transaction, PaymentError paymentError) {
  215.         // Method[] methods = transaction.getClass().getMethods();
  216.         Log.d("FailCallback", paymentError.getErrorInfo());
  217.         // for (int i = 0; i < methods.length; i++) {
  218.         //  System.out.println("public method: " + methods[i]);
  219.         // }
  220.     }
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement