|
| 1 | +package net.authorize.sample.PaymentTransactions; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | + |
| 5 | +import net.authorize.Environment; |
| 6 | +import net.authorize.api.contract.v1.*; |
| 7 | +import net.authorize.api.controller.base.ApiOperationBase; |
| 8 | +import net.authorize.api.controller.CreateTransactionController; |
| 9 | + |
| 10 | +public class ChargeCreditCard { |
| 11 | + |
| 12 | + // |
| 13 | + // Run this sample from command line with: |
| 14 | + // java -jar target/ChargeCreditCard-jar-with-dependencies.jar |
| 15 | + // |
| 16 | + public static void main(String[] args) { |
| 17 | + |
| 18 | + |
| 19 | + //Common code to set for all requests |
| 20 | + ApiOperationBase.setEnvironment(Environment.SANDBOX); |
| 21 | + |
| 22 | + MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ; |
| 23 | + merchantAuthenticationType.setName("5KP3u95bQpv"); |
| 24 | + merchantAuthenticationType.setTransactionKey("4Ktq966gC55GAX7S"); |
| 25 | + ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType); |
| 26 | + |
| 27 | + // Populate the payment data |
| 28 | + PaymentType paymentType = new PaymentType(); |
| 29 | + CreditCardType creditCard = new CreditCardType(); |
| 30 | + creditCard.setCardNumber("4242424242424242"); |
| 31 | + creditCard.setExpirationDate("0822"); |
| 32 | + paymentType.setCreditCard(creditCard); |
| 33 | + |
| 34 | + // Create the payment transaction request |
| 35 | + TransactionRequestType txnRequest = new TransactionRequestType(); |
| 36 | + txnRequest.setTransactionType(TransactionTypeEnum.AUTH_CAPTURE_TRANSACTION.value()); |
| 37 | + txnRequest.setPayment(paymentType); |
| 38 | + txnRequest.setAmount(new BigDecimal(500.00)); |
| 39 | + |
| 40 | + // Make the API Request |
| 41 | + CreateTransactionRequest apiRequest = new CreateTransactionRequest(); |
| 42 | + apiRequest.setTransactionRequest(txnRequest); |
| 43 | + CreateTransactionController controller = new CreateTransactionController(apiRequest); |
| 44 | + controller.execute(); |
| 45 | + |
| 46 | + |
| 47 | + CreateTransactionResponse response = controller.getApiResponse(); |
| 48 | + |
| 49 | + if (response!=null) { |
| 50 | + |
| 51 | + // If API Response is ok, go ahead and check the transaction response |
| 52 | + if (response.getMessages().getResultCode() == MessageTypeEnum.OK) { |
| 53 | + |
| 54 | + TransactionResponse result = response.getTransactionResponse(); |
| 55 | + if (result.getResponseCode().equals("1")) { |
| 56 | + System.out.println(result.getResponseCode()); |
| 57 | + System.out.println("Successful Credit Card Transaction"); |
| 58 | + System.out.println(result.getAuthCode()); |
| 59 | + System.out.println(result.getTransId()); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + System.out.println("Failed Transaction"+result.getResponseCode()); |
| 64 | + } |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + System.out.println("Failed Transaction: "+response.getMessages().getResultCode()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +} |
0 commit comments