|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | + |
| 7 | +package net.authorize.sample.PaypalExpressCheckout; |
| 8 | + |
| 9 | +import java.math.BigDecimal; |
| 10 | +import net.authorize.api.controller.base.ApiOperationBase; |
| 11 | +import net.authorize.api.contract.v1.*; |
| 12 | +import net.authorize.api.controller.CreateTransactionController; |
| 13 | +import net.authorize.Environment; |
| 14 | + |
| 15 | +/** |
| 16 | + * |
| 17 | + * @author gnongsie |
| 18 | + */ |
| 19 | +public class PriorAuthorizationCapture { |
| 20 | + public static void run(String apiLoginId, String apiTransactionKey, String transactionId){ |
| 21 | + System.out.println("Paypal Prior Authorization Transaction"); |
| 22 | + |
| 23 | + //Common code to set for all requests |
| 24 | + ApiOperationBase.setEnvironment(Environment.SANDBOX); |
| 25 | + |
| 26 | + // Define the merchant information (Authentication / Transaction ID) |
| 27 | + MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ; |
| 28 | + merchantAuthenticationType.setName(apiLoginId); |
| 29 | + merchantAuthenticationType.setTransactionKey(apiTransactionKey); |
| 30 | + ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType); |
| 31 | + |
| 32 | + // Populate the payment data |
| 33 | + PayPalType payPalType = new PayPalType(); |
| 34 | + payPalType.setCancelUrl("http://www.merchanteCommerceSite.com/Success/TC25262"); |
| 35 | + payPalType.setSuccessUrl("http://www.merchanteCommerceSite.com/Success/TC25262"); |
| 36 | + |
| 37 | + // Standard api call to retrieve response |
| 38 | + PaymentType paymentType = new PaymentType(); |
| 39 | + paymentType.setPayPal(payPalType); |
| 40 | + |
| 41 | + // Create the payment transaction request |
| 42 | + TransactionRequestType transactionRequest = new TransactionRequestType(); |
| 43 | + transactionRequest.setTransactionType(TransactionTypeEnum.PRIOR_AUTH_CAPTURE_TRANSACTION.value()); |
| 44 | + transactionRequest.setPayment(paymentType); |
| 45 | + transactionRequest.setAmount(BigDecimal.valueOf(19.99)); |
| 46 | + transactionRequest.setRefTransId(transactionId); |
| 47 | + |
| 48 | + // Make the API Request |
| 49 | + CreateTransactionRequest request = new CreateTransactionRequest(); |
| 50 | + request.setTransactionRequest(transactionRequest); |
| 51 | + |
| 52 | + // Instantiate the contoller that will call the service |
| 53 | + CreateTransactionController controller = new CreateTransactionController(request); |
| 54 | + controller.execute(); |
| 55 | + |
| 56 | + CreateTransactionResponse response = controller.getApiResponse(); |
| 57 | + |
| 58 | + // If API Response is ok, go ahead and check the transaction response |
| 59 | + if (response.getMessages().getResultCode() == MessageTypeEnum.OK){ |
| 60 | + if (response.getTransactionResponse() != null){ |
| 61 | + System.out.println("Success, Auth Code: " + response.getTransactionResponse().getAuthCode()); |
| 62 | + } |
| 63 | + } |
| 64 | + else{ |
| 65 | + System.out.println("Error: " + response.getMessages().getMessage().get(0).getCode() + |
| 66 | + " " + response.getMessages().getMessage().get(0).getText()); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments