Skip to content

Instantly share code, notes, and snippets.

@itorian
Created June 7, 2019 13:14
Show Gist options
  • Save itorian/ac3fd3ec231d58404af1ace16ccda18e to your computer and use it in GitHub Desktop.
Save itorian/ac3fd3ec231d58404af1ace16ccda18e to your computer and use it in GitHub Desktop.
PayPal REST API calls to get access token and process payment
using System;
using System.Text;
using System.Net.Http;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace ConsoleApp14
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
string PayPalClientId = "xxxx";
string PayPalClientSecret = "xxxx";
var payPalTokenModel = new PayPalTokenModel();
using (var client = new HttpClient())
{
// -h
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Accept-Language", "en_US");
// -u
var byteArray = Encoding.UTF8.GetBytes(PayPalClientId + ":" + PayPalClientSecret);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
// -d
var requestParams = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials")
};
var content = new FormUrlEncodedContent(requestParams);
// post and response
var webresponse = await client.PostAsync(new Uri("https://api.sandbox.paypal.com/v1/oauth2/token"), content);
var jsonString = await webresponse.Content.ReadAsStringAsync();
// deserialize
payPalTokenModel = JsonConvert.DeserializeObject<PayPalTokenModel>(jsonString);
}
if (payPalTokenModel.access_token != null)
{
var listItems = new ItemList() { items = new List<Item>() };
listItems.items.Add(new Item()
{
name = "product_name_here",
currency = "INR",
price = "100.00",
quantity = "1"
});
var paymentDetails = new Details()
{
tax = "0",
shipping = "0",
subtotal = "100.00"
};
var amount = new Amount()
{
currency = "INR",
total = "100.00",
details = paymentDetails
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
invoice_number = "INOVICE_" + Guid.NewGuid().ToString(),
amount = amount,
item_list = listItems
});
var paypalPaymentRequest = new PayPalPaymentRequestModel();
paypalPaymentRequest.intent = "sale";
paypalPaymentRequest.payer = new Payer { payment_method = "paypal" };
paypalPaymentRequest.transactions = transactionList;
paypalPaymentRequest.note_to_payer = "note here";
paypalPaymentRequest.redirect_urls = new RedirectUrls { return_url = "https://example.com/returnurl", cancel_url = "https://example.com/cancelurl" };
using (var client = new HttpClient())
{
// -h
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + payPalTokenModel.access_token);
// -d
var requestParams = new JavaScriptSerializer().Serialize(paypalPaymentRequest);
var content = new StringContent(requestParams, Encoding.UTF8, "application/json");
// post and response
var webresponse = await client.PostAsync(new Uri("https://api.sandbox.paypal.com/v1/payments/payment"), content);
var jsonString = await webresponse.Content.ReadAsStringAsync();
}
}
}
}
public class PayPalTokenModel
{
public string scope { get; set; }
public string nonce { get; set; }
public string access_token { get; set; }
public string token_type { get; set; }
public string app_id { get; set; }
public int expires_in { get; set; }
}
public class PayPalPaymentRequestModel
{
public string intent { get; set; }
public Payer payer { get; set; }
public List<Transaction> transactions { get; set; }
public string note_to_payer { get; set; }
public RedirectUrls redirect_urls { get; set; }
}
public class Payer
{
public string payment_method { get; set; }
}
public class Details
{
public string subtotal { get; set; }
public string tax { get; set; }
public string shipping { get; set; }
public string handling_fee { get; set; }
public string shipping_discount { get; set; }
public string insurance { get; set; }
}
public class Amount
{
public string total { get; set; }
public string currency { get; set; }
public Details details { get; set; }
}
public class PaymentOptions
{
public string allowed_payment_method { get; set; }
}
public class Item
{
public string name { get; set; }
public string description { get; set; }
public string quantity { get; set; }
public string price { get; set; }
public string tax { get; set; }
public string sku { get; set; }
public string currency { get; set; }
}
public class ShippingAddress
{
public string recipient_name { get; set; }
public string line1 { get; set; }
public string line2 { get; set; }
public string city { get; set; }
public string country_code { get; set; }
public string postal_code { get; set; }
public string phone { get; set; }
public string state { get; set; }
}
public class ItemList
{
public List<Item> items { get; set; }
public ShippingAddress shipping_address { get; set; }
}
public class Transaction
{
public Amount amount { get; set; }
public string description { get; set; }
public string custom { get; set; }
public string invoice_number { get; set; }
public PaymentOptions payment_options { get; set; }
public string soft_descriptor { get; set; }
public ItemList item_list { get; set; }
}
public class RedirectUrls
{
public string return_url { get; set; }
public string cancel_url { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment