|
| 1 | +package net.authorize.sample.PaypalExpressCheckout; |
| 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 Credit { |
| 11 | + |
| 12 | + public static void run(String apiLoginId, String transactionKey, String transactionId) { |
| 13 | + |
| 14 | + System.out.println("PayPal Credit Transaction"); |
| 15 | + //Common code to set for all requests |
| 16 | + ApiOperationBase.setEnvironment(Environment.SANDBOX); |
| 17 | + MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType() ; |
| 18 | + merchantAuthenticationType.setName(apiLoginId); |
| 19 | + merchantAuthenticationType.setTransactionKey(transactionKey); |
| 20 | + ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType); |
| 21 | + |
| 22 | + // Populate PayPal Transaction Data |
| 23 | + PayPalType payPalType = new PayPalType(); |
| 24 | + payPalType.setCancelUrl("http://www.merchanteCommerceSite.com/Success/TC25262"); |
| 25 | + payPalType.setSuccessUrl("http://www.merchanteCommerceSite.com/Success/TC25262"); |
| 26 | + |
| 27 | + // Populate the payment data |
| 28 | + PaymentType paymentType = new PaymentType(); |
| 29 | + paymentType.setPayPal(payPalType); |
| 30 | + |
| 31 | + // Create the payment transaction request |
| 32 | + TransactionRequestType txnRequest = new TransactionRequestType(); |
| 33 | + txnRequest.setTransactionType(TransactionTypeEnum.REFUND_TRANSACTION.value()); |
| 34 | + txnRequest.setPayment(paymentType); |
| 35 | + txnRequest.setAmount(new BigDecimal(500.00)); |
| 36 | + txnRequest.setRefTransId(transactionId); |
| 37 | + |
| 38 | + // Make the API Request |
| 39 | + CreateTransactionRequest apiRequest = new CreateTransactionRequest(); |
| 40 | + apiRequest.setTransactionRequest(txnRequest); |
| 41 | + CreateTransactionController controller = new CreateTransactionController(apiRequest); |
| 42 | + controller.execute(); |
| 43 | + |
| 44 | + |
| 45 | + CreateTransactionResponse response = controller.getApiResponse(); |
| 46 | + |
| 47 | + if (response!=null) { |
| 48 | + |
| 49 | + // If API Response is ok, go ahead and check the transaction response |
| 50 | + if (response.getMessages().getResultCode() == MessageTypeEnum.OK) { |
| 51 | + |
| 52 | + TransactionResponse result = response.getTransactionResponse(); |
| 53 | + if (result.getResponseCode().equals("1")) { |
| 54 | + System.out.println(result.getResponseCode()); |
| 55 | + System.out.println("Successful PayPal Transaction"); |
| 56 | + //System.out.println("Reference Transaction ID: " + result.getRefTransID()); |
| 57 | + System.out.println(result.getMessages().getMessage().get(0).getDescription()); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + System.out.println("Transaction Error: "+result.getErrors().getError().get(0).getErrorCode()+" "+result.getErrors().getError().get(0).getErrorText()); |
| 62 | + |
| 63 | + } |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + System.out.println("Error: "+response.getMessages().getMessage().get(0).getCode()+ " " + response.getMessages().getMessage().get(0).getText()); |
| 68 | + |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +} |
0 commit comments