Joseph DeVore's Blog: PayPal Pro


Viewing By Category : PayPal Pro / Main
August 1, 2005
/**
* Lab 506 DirectPayment Example
*/

import com.paypal.sdk.exceptions.*;
import com.paypal.sdk.services.CallerServices;
import com.paypal.sdk.profiles.APIProfile;
import com.paypal.sdk.profiles.ProfileFactory;
import com.paypal.soap.api.*;

/**
* Simple application which demonstrates use of PayPal's DoDirectPayment API call
* for processing a credit card transaction.
*/
public final class DirectPaymentSample {

// Profile object contains properties of API user private APIProfile profile;

// CallerServices is facade class used to invoke Paypal APIs private CallerServices caller;

// DoDirectPaymentRequest private DoDirectPaymentRequestType paymentRequest;


/**
* Configure the APIProfile
*/
private void setupProfile() throws PayPalException {
      profile = ProfileFactory.createAPIProfile();
      profile.setAPIUsername("lab506_api1.paypal.com");
      profile.setAPIPassword("developer");
      profile.setCertificateFile("C:/Downloads/PayPal/Lab506/cert/lab506.p12");
      profile.setPrivateKeyPassword("password");
      profile.setEnvironment("sandbox");
}


/**
* Prepare the request
*/
private void setupRequest() {
   // the request details object contains all payment details    DoDirectPaymentRequestDetailsType requestDetails = new DoDirectPaymentRequestDetailsType();
   
   // define the payment action to 'Sale'    // (another option is 'Authorization', which would be followed later with a DoCapture API call)    requestDetails.setPaymentAction(PaymentActionCodeType.Sale);
   
   // define the total amount and currency for the transaction    PaymentDetailsType paymentDetails = new PaymentDetailsType();
      BasicAmountType totalAmount = new BasicAmountType();
      totalAmount.set_value("99.95");
      totalAmount.setCurrencyID(CurrencyCodeType.USD);
      paymentDetails.setOrderTotal(totalAmount);

      // define the credit card to be used       CreditCardDetailsType creditCard = new CreditCardDetailsType();
      creditCard.setCreditCardNumber("4138848780259668");
      creditCard.setExpMonth(1);
      creditCard.setExpYear(2006);
      creditCard.setCVV2("000");
      creditCard.setCreditCardType(CreditCardTypeType.Visa);
      PayerInfoType cardHolder = new PayerInfoType();
      cardHolder.setPayerName(new PersonNameType("Mr.", "Big", "Cash", "Spender", "Sr."));
      AddressType payerAddress = new AddressType();
      payerAddress.setStreet1("2211 N. First St.");
      payerAddress.setCityName("San Jose");
      payerAddress.setStateOrProvince("CA");
      payerAddress.setPostalCode("95131");
      payerAddress.setCountry(CountryCodeType.US);
      cardHolder.setAddress(payerAddress);
      creditCard.setCardOwner(cardHolder);
   
      requestDetails.setCreditCard(creditCard);
   requestDetails.setPaymentDetails(paymentDetails);
   requestDetails.setIPAddress("127.0.0.1");
   
   // instantiate the actual request object    paymentRequest = new DoDirectPaymentRequestType();
   paymentRequest.setDoDirectPaymentRequestDetails(requestDetails);
}


/**
* Make the call and read the response
*/
private void makeApiCall() throws PayPalException {
      caller = new CallerServices();
      caller.initialize();
      caller.setAPIProfile(profile);
      System.out.println("CallerServices setup OK");
      System.out.println("Making API call...");
      DoDirectPaymentResponseType paymentResponse =
         (DoDirectPaymentResponseType)caller.call("DoDirectPayment", paymentRequest);
      System.out.println("Ack: "+paymentResponse.getAck());
      
      if (paymentResponse.getErrors()!=null) {
         ErrorType er = paymentResponse.getErrors(0);
         System.out.println("Error Code: "+er.getErrorCode());
         System.out.println("Error Message: "+er.getLongMessage());
      }
      System.out.println("Transaction ID: "+paymentResponse.getTransactionID());
}



public static void main(String[] args) {
   System.out.println("Lab 506 DirectPayment Example");
   System.out.println("-----------------------------\n");
   
      DirectPaymentSample sample = new DirectPaymentSample();

      try {
         sample.setupProfile();
         System.out.println("Profile setup OK");
         sample.setupRequest();
         System.out.println("Request setup OK");
         sample.makeApiCall();
         System.out.println("Done.");
      }
      catch (PayPalException e) {
         System.err.println(e);
         e.printStackTrace();
      }
}

}

Comments

There are no comments for this entry.













Editor Login ›