Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Authorize.net plugin for Android Phonegap
  3.  * Luis Balam 2013
  4.  * @version 0.0.1
  5.  *
  6.  */
  7. package org.opencorebanking.plugins;
  8.  
  9.  
  10. import java.math.BigDecimal;
  11. import net.authorize.Environment;
  12. import net.authorize.Merchant;
  13. import net.authorize.TransactionType;
  14. import net.authorize.aim.cardpresent.Result;
  15. import net.authorize.aim.Transaction;
  16. import net.authorize.data.*;
  17. import net.authorize.data.creditcard.*;
  18.  
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. import android.util.Log;
  24.  
  25. import org.apache.cordova.api.Plugin;
  26. import org.apache.cordova.api.PluginResult;
  27. //import org.apache.cordova.api.PluginResult.Status;
  28.  
  29. public class AuthorizeNet extends Plugin {
  30.     public String callback;
  31.     /**
  32.      * Constructor.
  33.      */
  34.     public AuthorizeNet() {
  35.     }  
  36.    
  37.     @Override
  38.     public PluginResult execute(String action, JSONArray args, String callbackId) {
  39.         this.callback       = callbackId;
  40.         if (action.equals("request")) {
  41.             JSONObject rec              = new JSONObject();
  42.  
  43.             try{
  44.             JSONObject jo               = args.getJSONObject(0); //getJSONObject(0);
  45.              if (jo != null) {
  46.                  //obj.has("resources")
  47.                   String apiLoginID         = jo.has("APILoginID")                  ? jo.getString("APILoginID") : "";
  48.                   String transactionKey     = jo.has("TransactionKey")              ? jo.getString("TransactionKey") : "";
  49.                   String MD5Value           = jo.has("APILoginID")                  ? jo.getString("APILoginID") : "";
  50.                   String CType              = jo.has("CreditCardType")              ? jo.getString("CreditCardType") : ""; //MasterCard Visa AmericanExpress Discover DinersClub JCB eCheck ""
  51.                   String CCNumber           = jo.has("CreditCardNumber")            ? jo.getString("CreditCardNumber") :"";
  52.                   String CCExpM             = jo.has("CreditCardExpirationMonth")   ? jo.getString("CreditCardExpirationMonth") : "";
  53.                   String CCExpY             = jo.has("CreditCardExpirationYear")    ? jo.getString("CreditCardExpirationYear") : "";
  54.                   String CCCode             = jo.has("CreditCardCode")              ? jo.getString("CreditCardCode") : "";
  55.                   Double TMount             = jo.has("Amount")                      ? jo.getDouble("Amount") : 0.00;
  56.                   String CCode              = jo.has("CurrencyCode")                ? jo.getString("CurrencyCode") : "";
  57.        
  58.                   Merchant merchant         = Merchant.createMerchant(Environment.SANDBOX, apiLoginID, transactionKey);
  59.                  
  60.                   merchant.setDeviceType(net.authorize.DeviceType.VIRTUAL_TERMINAL);
  61.                   merchant.setMarketType(net.authorize.MarketType.RETAIL);
  62.                   merchant.setMD5Value(MD5Value);
  63.        
  64.                 /*
  65.                     VISA("Visa"),
  66.                     MASTER_CARD("MasterCard"),
  67.                     AMERICAN_EXPRESS("American Express"),
  68.                     DISCOVER("Discover"),
  69.                     DINERS_CLUB("Diners Club"),
  70.                     JCB("JCB"),
  71.                     ECHECK("eCheck"),   // added for the reporting API
  72.                     UNKNOWN("");
  73.                  */
  74.                  
  75.                   //creditCard.setTrack1("%B4111111111111111^CARDUSER/JOHN^1803101000000000020000831000000?");
  76.                  // creditCard.setTrack2(";4111111111111111=1803101000020000831?");
  77.        
  78.                   // create transaction
  79.                     // create credit card
  80.                     CreditCard creditCard = CreditCard.createCreditCard();
  81.                     creditCard.setCreditCardNumber(CCNumber);
  82.                     creditCard.setExpirationMonth(CCExpM);
  83.                     creditCard.setExpirationYear(CCExpY);
  84.                     creditCard.setCardCode(CCCode);
  85.                     creditCard.setCardPresent(true);
  86.                    
  87.                   //MasterCard Visa AmericanExpress Discover DinersClub JCB eCheck ""
  88.                     if(CType.equals("Visa") ){
  89.                         creditCard.setCardType(CardType.VISA);
  90.                     } else if (CType.equals("MasterCard") ){
  91.                         creditCard.setCardType(CardType.MASTER_CARD);
  92.                     } else if (CType.equals("AmericanExpress") ){
  93.                         creditCard.setCardType(CardType.AMERICAN_EXPRESS);
  94.                     } else if (CType.equals("Discover") ){
  95.                         creditCard.setCardType(CardType.DISCOVER);
  96.                     } else if (CType.equals("DinersClub") ){
  97.                         creditCard.setCardType(CardType.DINERS_CLUB);
  98.                     } else if (CType.equals("JCB") ){
  99.                         creditCard.setCardType(CardType.JCB);
  100.                     } else if (CType.equals("eCheck") ){
  101.                         creditCard.setCardType(CardType.ECHECK);
  102.                     } else {
  103.                         creditCard.setCardType(CardType.UNKNOWN);
  104.                     }
  105.                    
  106.            
  107.                   Transaction authCaptureTransaction = merchant.createAIMTransaction(TransactionType.AUTH_CAPTURE, new BigDecimal(TMount));
  108.                   authCaptureTransaction.setCurrencyCode(CCode);
  109.                   authCaptureTransaction.setCreditCard(creditCard);
  110.        
  111.                   Result<Transaction> result = (Result<Transaction>)merchant.postTransaction(authCaptureTransaction);
  112.  
  113.                   if(result.isApproved()) {
  114.                       //Log.e("org.opencorebanking", result.getRefTransId() );
  115.                       rec.put("TransactionID", result.getTransId());
  116.                       //rec.put("TransactionRefID", result.getRefTransId());
  117.                      return new PluginResult(PluginResult.Status.OK, rec);
  118.  
  119.                   } else if (result.isDeclined()) {
  120.                       //Log.e("org.opencorebanking", result.getResponseReasonCodes().get(0) + " : " + result.getResponseReasonCodes().get(0).getReasonText());
  121.                       return new PluginResult(PluginResult.Status.ERROR,
  122.                               "ERROR-DECL: " + result.getResponseReasonCodes().get(0) + " : " + result.getResponseReasonCodes().get(0).getReasonText());
  123.                   } else {
  124.                       //Log.e("org.opencorebanking", result.getResponseReasonCodes().get(0) + " : " + result.getResponseReasonCodes().get(0).getReasonText());
  125.                       return new PluginResult(PluginResult.Status.ERROR,
  126.                               "ERROR: " + result.getResponseReasonCodes().get(0) + " : " + result.getResponseReasonCodes().get(0).getReasonText());
  127.                   }
  128.              }
  129.             } catch (JSONException e) {
  130.                 Log.e("org.opencorebanking", "Error in Intent action " + e.getMessage());
  131.                 return new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
  132.             }
  133.         } else {
  134.                return new PluginResult(PluginResult.Status.INVALID_ACTION, "Invalid option " + action );
  135.         }
  136.         PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
  137.         r.setKeepCallback(true);
  138.         return r;
  139.     }
  140.  
  141. }