Process payments with credit cards

To process a credit card through PayPal, pass the credit card information directly in the transaction request to PayPal, or use a card identifier for a credit card that was previously stored in the PayPal vault. To complete a transaction follow these steps:

1. Optional Store a credit card securely in the PayPal vault. You can do this at any time prior to the transaction.
2. Required Set payment details to define the details about the transaction.
3. Required Make the payment request.

Optional: Store a credit card securely in the PayPal vault

To provide a secure way to store a credit card for your users, which doesn't reside on your servers, is to use the PayPal vault.

To vault a card, set up an object that contains the credit card details that you intend to store, then call the credit card creation method of the SDK.

var cc_details = {
  type: 'visa',
  number: '4417119669820331',
  expire_month: '11',
  expire_year: '2018',
  first_name: 'John',
  last_name: 'Doe',
  payer_id: '123456'
};

paypal.credit_card.create(cc_details, function(error, credit_card){
  var credit_card_id;
  var payer_id;

  if(error){
    console.error(JSON.stringify(error));
  } else {
    // Card information needed to process payment
    credit_card_id = credit_card.id;
    payer_id = credit_card.payer_id;
  }
});
use PayPal\Api\CreditCard;

// Create new credit card
$card = new CreditCard();
$card->setType("visa")
  ->setNumber("4917912523797702")
  ->setExpireMonth("11")
  ->setExpireYear("2019")
  ->setCvv2("012")
  ->setFirstName("Joe")
  ->setLastName("Shopper");

// Create new vaulted card
try {
  $card->create($apiContext);

  // Card information needed to process payment
  $credit_card_id = $card->getId()
  $payer_id = $card->getPayerId()
} catch (PayPal\Exception\PayPalConnectionException $ex) {
  echo $ex->getCode();
  echo $ex->getData();
  die($ex);
} catch (Exception $ex) {
  die($ex);
}
# Set credit card details
credit_card = CreditCard({
  "type": "visa",
  "number": "4417119669820331",
  "expire_month": "11",
  "expire_year": "2018",
  "cvv2": "874",
  "first_name": "Joe",
  "last_name": "Shopper"
})

# Store credit card in vault
if credit_card.create():
  credit_card_id = credit_card.id
  payer_id = credit_card.payer_id
else:
  print("Error while creating CreditCard:")
  print(credit_card.error)
# Create credit card object
credit_card = CreditCard.new({
  :type => "visa",
  :number => "4567516310777851",
  :expire_month => "11",
  :expire_year => "2018",
  :cvv2 => "874",
  :first_name => "Joe",
  :last_name => "Shopper",
  :billing_address => {
    :line1 => "52 N Main ST",
    :city => "Johnstown",
    :state => "OH",
    :postal_code => "43210",
    :country_code => "US"
  }
})

# Make credit card payment
if credit_card.create
  credit_card_id = credit_card.id
else
  logger.error "Error while creating CreditCard:"
  logger.error credit_card.error.inspect
end
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;

import java.util.List;
import java.util.ArrayList;

import com.paypal.api.payments.Address;
import com.paypal.api.payments.Amount;
import com.paypal.api.payments.CreditCard;
import com.paypal.api.payments.Details;
import com.paypal.api.payments.FundingInstrument;
import com.paypal.api.payments.Payer;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.Sale;
import com.paypal.api.payments.Transaction;

// Create a Credit Card
CreditCard card = new CreditCard()
  .setType("visa")
  .setNumber("4417119669820331")
  .setExpireMonth(11)
  .setExpireYear(2019)
  .setCvv2(012)
  .setFirstName("Joe")
  .setLastName("Shopper");

try {
  card.create(context);
  System.out.println(card);
} catch (PayPalRESTException e) {
  System.err.println(e.getDetails());
}
var apiContext = Configuration.GetAPIContext();

var card = new CreditCard(){
  type = "visa",
  number = "4877274905927862",
  expire_month = 11,
  expire_year = 2018,
  cvv2 = "874"
};

var createdCard = card.Create(apiContext);

Once the credit card is securely stored, capture the credit card ID to process payments from the secured card.

Set payment details

The next step is to set up the payment object that will contain all transaction information needed to process a payment.

var payment_data = {
  intent: 'sale',
  payer: {
    payment_method: 'credit_card',
    funding_instruments: [{
      payment_card: {
        type: 'visa',
        number: '4417119669820331',
        expire_month: '11',
        expire_year: '2018',
        cvv2: '874',
        first_name: 'Joe',
        last_name: 'Shopper',
        billing_country: 'US',
        billing_address: {
          line1: '52 N Main ST',
          city: 'Johnstown',
          state: 'OH',
          postal_code: '43210',
          country_code: 'US'
        }
      }
    }]
  },
  transactions: [{
    amount: {
      total: '7.47',
      currency: 'USD',
      details: {
        subtotal: '7.41',
        tax: '0.03',
        shipping: '0.03'
      }
    },
    description: 'This is the payment transaction description.'
  }]
};
use PayPal\Api\Amount;
use PayPal\Api\CreditCard;
use PayPal\Api\Details;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;

// Create credit card object and set funding instrument
$card = new CreditCard();
$card->setType("visa")
  ->setNumber("4669424246660779")
  ->setExpireMonth("11")
  ->setExpireYear("2019")
  ->setCvv2("012")
  ->setFirstName("Joe")
  ->setLastName("Shopper");

$fi = new FundingInstrument();
$fi->setCreditCard($card);

// Set payer to process credit card
$payer = new Payer();
$payer->setPaymentMethod("credit_card")
  ->setFundingInstruments(array($fi));

// Set payment detail breakdown
$details = new Details();
$details->setShipping(0.03)
  ->setTax(0.03)
  ->setSubtotal(7.41);

// Set total amount
$amount = new Amount();
$amount->setCurrency("USD")
  ->setTotal(7.47)
  ->setDetails($details);

// Create transaction object with required data
$transaction = new Transaction();
$transaction->setAmount($amount)
  ->setDescription("Payment description");

// Create payment object with required data
$payment = new Payment();
$payment->setIntent("sale")
  ->setPayer($payer)
  ->setTransactions(array($transaction));
# Create new payment object
from paypalrestsdk import Payment

payment = Payment({
  "intent": "sale",
  "payer": {
    "payment_method": "credit_card",
    "funding_instruments": [{
      "credit_card": {
        "type": "visa",
        "number": "4417119669820331",
        "expire_month": "11",
        "expire_year": "2018",
        "cvv2": "874",
        "first_name": "Joe",
        "last_name": "Shopper",
        "billing_address": {
          "line1": "52 N Main ST",
          "city": "Johnstown",
          "state": "OH",
          "postal_code": "43210",
          "country_code": "US"
        }
      }
    }]
  },
  "transactions": [{
    "amount": {
      "total": "7.47",
      "currency": "USD"
    },
    "description": "This is the payment transaction description."
  }]
})
# Create credit card object
payment = Payment.new({
  :intent => "sale",
  :payer => {
    :payment_method => "credit_card",
    :funding_instruments => [{
      :credit_card => {
        :type => "visa",
        :number => "4567516310777851",
        :expire_month => "11",
        :expire_year => "2018",
        :cvv2 => "874",
        :first_name => "Joe",
        :last_name => "Shopper",
        :billing_address => {
          :line1 => "52 N Main ST",
          :city => "Johnstown",
          :state => "OH",
          :postal_code => "43210",
          :country_code => "US"
        }
      }
    }]
  },
  :transactions => [{
    :amount => {
      :total => "1.00",
      :currency => "USD"
    },
    :description => "This is the payment transaction description."
  }]
})
import java.util.List;
import java.util.ArrayList;

import com.paypal.api.payments.Address;
import com.paypal.api.payments.Amount;
import com.paypal.api.payments.CreditCard;
import com.paypal.api.payments.Details;
import com.paypal.api.payments.FundingInstrument;
import com.paypal.api.payments.Payer;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.Transaction;

// Set address info
Address billingAddress = new Address();
billingAddress.setCity("Johnstown");
billingAddress.setCountryCode("US");
billingAddress.setLine1("52 N Main ST");
billingAddress.setPostalCode("43210");
billingAddress.setState("OH");

// Credit card info
CreditCard creditCard = new CreditCard();
creditCard.setBillingAddress(billingAddress);
creditCard.setExpireMonth(11);
creditCard.setExpireYear(2018);
creditCard.setFirstName("Joe");
creditCard.setLastName("Shopper");
creditCard.setNumber("4669424246660779");
creditCard.setType("visa");

// Payment details
Details details = new Details();
details.setShipping("1");
details.setSubtotal("5");
details.setTax("1");

// Total amount
Amount amount = new Amount();
amount.setCurrency("USD");
amount.setTotal("7");
amount.setDetails(details);

// Transaction details
Transaction transaction = new Transaction();
transaction.setAmount(amount);
transaction
  .setDescription("This is the payment transaction description.");

ListTransaction transactions = new ArrayListTransaction();
transactions.add(transaction);

// Set funding instrument
FundingInstrument fundingInstrument = new FundingInstrument();
fundingInstrument.setCreditCard(creditCard);

ListFundingInstrument fundingInstrumentList = new ArrayListFundingInstrument();
fundingInstrumentList.add(fundingInstrument);

// Set payer details
Payer payer = new Payer();
payer.setFundingInstruments(fundingInstrumentList);
payer.setPaymentMethod("credit_card");

// Set payment details
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
payment.setTransactions(transactions);
Payment createdPayment = null;
var transaction = new Transaction(){
  amount = new Amount(){
    currency = "USD",
    total = "7",
    details = new Details(){
      shipping = "1",
      subtotal = "5",
      tax = "1"
    }
  },
  description = "This is the payment transaction description.",
  item_list = new ItemList(){
    items = new ListItem(){
      new Item(){
        name = "Item Name",
        currency = "USD",
        price = "1",
        quantity = "5",
        sku = "sku"
      }
    },
    shipping_address = new ShippingAddress{
      city = "Johnstown",
      country_code = "US",
      line1 = "52 N Main ST",
      postal_code = "43210",
      state = "OH",
      recipient_name = "Joe Buyer"
    }
  },
  invoice_number = Common.GetRandomInvoiceNumber()
};

var payer = new Payer()
{
  payment_method = "credit_card",
  funding_instruments = new ListFundingInstrument(){
    new FundingInstrument(){
      credit_card = new CreditCard(){
        billing_address = new Address(){
          city = "Johnstown",
          country_code = "US",
          line1 = "52 N Main ST",
          postal_code = "43210",
          state = "OH"
        },
        cvv2 = "874",
        expire_month = 11,
        expire_year = 2018,
        first_name = "Joe",
        last_name = "Shopper",
        number = "4877274905927862",
        type = "visa"
      }
    }
  },
  payer_info = new PayerInfo{
    email = "test@email.com"
  }
};

var payment = new Payment(){
  intent = "sale",
  payer = payer,
  transactions = new ListTransaction() { transaction }
};

Much like with a PayPal payment, set the intent to sale, then set up the funding information and transaction details.

To use a vaulted card for the payment method, replace the credit_card object under funding_instrument with a credit_card_token object, containing the credit card and payer IDs obtained when the card was stored in the vault.

funding_instruments: [{
  credit_card_token: {
    credit_card_id: credit_card.id,
    payer_id: credit_card.payer_id
  }
}]

Make the payment request

With the payment object set up, you can finalize payment. Pass in the payment object to the payment creation method to complete the transaction.

paypal.payment.create(payment_data, function(error, payment){
  if(error){
    console.error(JSON.stringify(error));
  } else {
    console.log(JSON.stringify(payment));
  }
});
// Create payment
try {
  $payment->create($apiContext);
  echo($payment);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
  echo $ex->getCode();
  echo $ex->getData();
  die($ex);
} catch (Exception $ex) {
  die($ex);
}
# Create payment
if payment.create():
  print("Payment[%s] created successfully" % (payment.id))
else:
  # Display Error message
  print("Error while creating payment:")
  print(payment.error)
# Process payment with credit card
if payment.create
  logger.info "Payment[#{payment.id}] created successfully"
else
  logger.error "Error while creating payment:"
  logger.error payment.error.inspect
end
try {
  // Create payment
  createdPayment = payment.create(apiContext);
  System.out.println("Created payment with id = " + createdPayment.getId());
} catch (PayPalRESTException e) {
  System.err.println(e.getDetails());
}
var createdPayment = payment.Create(apiContext);

Additional information