Introduction
This is my 2nd post and it is a successor of my 1st article here. I am trying to explain the Recurring Payments using Paypal here via Credit Cards.
Background
A little knowledge is required about the Paypal REST API and the pre-requisites from the 1st article. If you have not read my previous article and you are new to PAYPAL REST API, I highly recommend you to read the 1st article here.
Using the Code
You can setup the code environment as well from the 1st article. This code is just an extension to the previous project only. We just need to add some more functions to the code which will save the Credit Card details of the user in the Paypal Vault and return the token to you.
We need to save this token returned by the Paypal Server at our end. This token will be used to make the recurring payment automatically by your system or website.
PaypalCCModel
class is for storing the Credit Card data in an Object. You need to instantiate this Object and fill details in it and then call the function StoreCreditCardInPaypal
.
public class PaypalCCModel
{
public string nostore { get; set; }
public string Number { get; set; }
public string cvv2 { get; set; }
public string Type { get; set; }
public string ExpireMonth { get; set; }
public string ExpireYear { get; set; }
}
Storing the CreditCard information in the PayPal Server
private bool StoreCreditCardInPaypal(PaypalCCModel ccmodel)
{
CreditCard credtCard = new CreditCard();
credtCard.expire_month = int.Parse(ccmodel.ExpireMonth);
credtCard.expire_year = int.Parse(ccmodel.ExpireYear);
credtCard.number = ccmodel.Number;
credtCard.type = ccmodel.Type.ToLower();
credtCard.cvv2 = ccmodel.cvv2;
try
{
APIContext apiContext = Configuration.GetAPIContext();
CreditCard createdCreditCard = credtCard.Create(apiContext);
SaveCardID(User.Identity.Name,createdCreditCard.id);
}
catch (PayPal.PayPalException ex)
{
Logger.LogError("Error: "+ex.Message);
return false;
}
catch (Exception ex)
{
Logger.LogError("Error: "+ex.Message);
}
return true;
}
In SaveCardID
function, I am just storing the CreditCard ID returned by PayPal in the database. You can write your own function for saving it.
Now, below is the function that makes the payment using the stored credit cards. It uses the ID which was returned in the function StoreCreditCardInPaypal
.
public bool PaymentViaStoredCard()
{
Item item = new Item();
item.name = "Item Name";
item.currency = "USD";
item.price = "1";
item.quantity = "1";
item.sku = "sku-1123324";
List<item> itms = new List<item>();
itms.Add(item);
ItemList itemList = new ItemList();
itemList.items = itms;
CreditCardToken credCardToken = new CreditCardToken();
credCardToken.credit_card_id = "CARD-5MY32504F4899612AKIHAQHY";
Details details = new Details();
details.shipping = "1";
details.subtotal = "1";
details.tax = "1";
Amount amnt = new Amount();
amnt.currency = "USD";
amnt.total = "3";
amnt.details = details;
Transaction tran = new Transaction();
tran.amount = amnt;
tran.description = "This is the recurring payment transaction";
tran.item_list = itemList;
List<transaction> transactions = new List<transaction>();
transactions.Add(tran);
FundingInstrument fundInstrument = new FundingInstrument();
fundInstrument.credit_card_token = credCardToken;
List<fundinginstrument> fundingInstrumentList = new List<fundinginstrument>();
fundingInstrumentList.Add(fundInstrument);
Payer payr = new Payer();
payr.funding_instruments = fundingInstrumentList;
payr.payment_method = "credit_card";
Payment pymnt = new Payment();
pymnt.intent = "sale";
pymnt.payer = payr;
pymnt.transactions = transactions;
try
{
APIContext apiContext = Configuration.GetAPIContext();
Payment createdPayment = pymnt.Create(apiContext);
}
catch (PayPal.PayPalException ex)
{
Logger.LogError("Error: "+ex.Message);
}
}
Now, you can check the payment state in the object createdPayment
as earlier in the 1st article.
For making the recurring payments, you can implement your own logic code and use the above function to make the payment from your clients automatically.
For Showing Credit Card Details to user
If you need to see the credit card or you might need to show it to the users on demand.
For example, if a user wants to see from which credit card of his, recurring payments are being done. Then, you can show his card details with the code snippet below. We will use the Credit Card ID which we have stored at our end for fetching the details.
public void GetCreditCardDetailsFromVault()
{
try
{
APIContext apiContext = Configuration.GetAPIContext();
CreditCard card = CreditCard.Get(apiContext, "CARD-00N04036H5458422MKRIAWHY");
}
catch (PayPal.PayPalException ex)
{
Logger.LogError("Error: "+ex.Message);
}
}
You will get the Card Details in the card
object above.
Deleting a Card from the Vault
There might be a case where user wants to change his card details and add another card for the recurring Payments. Then, you will need to delete the old card details and store new card details in the vault. When we do this, the ID returned by the Paypal is changed for the new card. Because the Previous card details will be deleted completely from the paypal and we will need to create a new entry in the vault because there is no way to replace the details. We have add new.
public void DeleteCreditCardFromVault()
{
try
{
APIContext apiContext = Configuration.GetAPIContext();
CreditCard card = CreditCard.Get(apiContext, "CARD-00N04036H5458422MKRIAWHY");
card.Delete(apiContext);
}
catch (PayPal.PayPalException ex)
{
Logger.LogError("Error: "+ex.Message);
}
}
That's it!! If you have any queries, you can ask me.
Thanks for reading!!
Points of Interest
Recurring Payments is very interesting and widely used by the Services Website for regular payments from their clients in Automated Fashion. It is really an interesting concept of PayPal.
History
- 13th March 2015: Initial version