Java 3 - 2025

Week 11

Payment Processing

  • Access Authorize.net's Sandbox Merchant Interface at https://developer.authorize.net/hello_world/sandbox.html
  • Click the blue "Create sandbox account" button.
  • After signing in, click the Account tab.
  • In the Security Settings section, click the "API Credentials & Keys" link.
  • Copy your API Login ID.
  • Generate and copy a new transaction key.
  • Create two new environment variables, both locally and on your Azure Web App.
    AUTHORIZE_API_LOGIN_ID
    AUTHORIZE_TRANSACTION_KEY

API Reference

Charge a Credit Card

  • In the "shared" package, create a new package called "authorize_net".
  • Create a class called "ChargeCreditCard". Add this code.
  • Use this code in the main method.
    run(
      Config.getEnv("AUTHORIZE_API_LOGIN_ID"),
      Config.getEnv("AUTHORIZE_TRANSACTION_KEY"),
      25.00);
  • Run the program. It should display the following:
    Successfully created transaction with Transaction ID: 0
    Response Code: 1
    Message Code: 1
    Description: This transaction has been approved.
    Auth Code: 000000
  • You will receive a Merchant Email Receipt.

Charge a Credit Card

  • Update the run method to include a String[] for creditCard info.
    run(String apiLoginId, String transactionKey, Double amount, String[] creditCardInfo)
  • Populate the payment data.
    CreditCardType creditCard = new CreditCardType();
    creditCard.setCardNumber(creditCardInfo[0]);
    creditCard.setExpirationDate(creditCardInfo[1]);
    creditCard.setCardCode(creditCardInfo[2]);
  • Use this code in the main method.
    run(
     Config.getEnv("AUTHORIZE_API_LOGIN_ID"),
     Config.getEnv("AUTHORIZE_TRANSACTION_KEY"),
     25.00,
     new String[]{"4242424242424242", "0835", "111"});
  • Run the program.

Test Credit Card Info

Billing and Shipping Information

  • Update the run method to include two String[] for billing and shipping data.
    public static ANetApiResponse run(String apiLoginId, String transactionKey, Double amount, String[] creditCardInfo, String[] billingInfo, String[] shippingInfo)
  • In the main method, add two String[] array objects.
    run(
     Config.getEnv("AUTHORIZE_API_LOGIN_ID"),
     Config.getEnv("AUTHORIZE_TRANSACTION_KEY"),25.00,
     new String[]{"370000000000002", "0835", "1111"},
     new String[]{"John", "Doe", "123 Main Street", "Cedar Rapids","IA","52404","USA","3191234567"},
     new String[]{"John", "Doe", "123 Main Street", "Cedar Rapids", "IA", "52404", "USA"});

Billing and Shipping Information

  • Add this code to set billing and shipping information.
  • Be sure to view other available methods on the txnRequest object.
// Billing Address (includes cardholder name)
CustomerAddressType billingAddress = new CustomerAddressType();
billingAddress.setFirstName(billingInfo[0]);
billingAddress.setLastName(billingInfo[1]);
billingAddress.setAddress(billingInfo[2]);
billingAddress.setCity(billingInfo[3]);
billingAddress.setState(billingInfo[4]);
billingAddress.setZip(billingInfo[5]);
billingAddress.setCountry(billingInfo[6]);
billingAddress.setPhoneNumber(billingInfo[7]);

// Shipping Address
CustomerAddressType shippingAddress = new CustomerAddressType();
shippingAddress.setFirstName(shippingInfo[0]);
shippingAddress.setLastName(shippingInfo[1]);
shippingAddress.setAddress(shippingInfo[2]);
shippingAddress.setCity(shippingInfo[3]);
shippingAddress.setState(shippingInfo[4]);
shippingAddress.setZip(shippingInfo[5]);
shippingAddress.setCountry(shippingInfo[6]);

// code omitted
txnRequest.setBillTo(billingAddress);
txnRequest.setShipTo(shippingAddress);

Other Payment Processors

Checkout Page

  • https://github.com/mlhaus/toy-store/commit/6cacfadf79675a9a3f2160ffe82872ea2b5e410a
  • https://github.com/mlhaus/toy-store/commit/b05320057b0faa01665620dc7485f2301a58cb4b
  • Use flashMessageDanger instead of flashMessageError