Created
November 28, 2017 22:34
-
-
Save pingec/4416359fedffff16763c93d6ae53e41a to your computer and use it in GitHub Desktop.
Quick demo - bare minimum to process a payment through paypal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using PayPal.Api; | |
using System; | |
using System.Collections.Generic; | |
namespace PaypalPayment2 | |
{ | |
/* | |
Instructions: | |
1. Install-Package PayPal | |
2. Set your clientId and clientSecret parameters in GetApiContext() | |
3. Replace parameters like paymentId payerId returnUrl etc. | |
*/ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
//// 1. | |
////CreatePaymentDemo(); | |
//// 2. | |
////ExecutePaymentDemo(); | |
//// Optional | |
//ListPaymentsDemo(); | |
} | |
private static APIContext GetApiContext() | |
{ | |
// Authenticate with PayPal | |
var config = new Dictionary<string, string>() { | |
{"mode","sandbox" }, | |
{"clientId","" }, | |
{"clientSecret","" } | |
}; | |
var accessToken = new OAuthTokenCredential(config).GetAccessToken(); | |
var apiContext = new APIContext(accessToken); | |
return apiContext; | |
} | |
private static void ListPaymentsDemo() | |
{ | |
var apiContext = GetApiContext(); | |
var history = Payment.List(apiContext); | |
foreach (var payment in history.payments) | |
{ | |
PrintPayment(payment); | |
} | |
} | |
private static void PrintPayment(Payment payment) | |
{ | |
var output = string.Format(@"Payment from:{0} {1} state:{3} amount:{4}{5} id{6} PayerId:{7}", | |
payment.payer.payer_info.first_name, | |
payment.payer.payer_info.last_name, | |
payment.state, | |
payment.transactions[0].amount.total, | |
payment.transactions[0].amount.currency, | |
payment.id, | |
payment.payer.payer_info.payer_id | |
); | |
Console.WriteLine(output); | |
} | |
private static void ExecutePaymentDemo() | |
{ | |
// At this point buyer was redirected to approval_url in browser and approved the payment | |
// Buyer's browser is redirected to http://ekjov.dyn.pingec.si:8888/mobileclient.aspx?h_actkey=paypal_return&paymentId=PAY-6Y150393AP3719109LIO5JCY&token=EC-5U115267N62523746&PayerID=XRHV52C9VFUDQ | |
// Execute the payment | |
var paymentId = "PAY-6Y150393AP3719109LIO5JCY"; | |
var payerId = "XRHV52C9VFUDQ"; | |
var apiContext = GetApiContext(); | |
Payment payment = Payment.Get(apiContext, paymentId); | |
var paymentExecution = new PaymentExecution | |
{ | |
payer_id = payerId | |
}; | |
payment.Execute(apiContext, paymentExecution); | |
// Check payment.state, possible options are: | |
//created | |
//approved | |
//failed | |
//canceled | |
//expired | |
// The following should be true: payment.state == "approved" | |
// If not, payment can be refreshed with Payment.Get(apiContext, "PAY-6Y150393AP3719109LIO5JCY"); in order to recheck state | |
} | |
private static void CreatePaymentDemo() | |
{ | |
// Authenticate with PayPal | |
var apiContext = GetApiContext(); | |
// Make an API call | |
var payment = Payment.Create(apiContext, new Payment | |
{ | |
intent = "sale", | |
payer = new Payer | |
{ | |
payment_method = "paypal" | |
}, | |
transactions = new List<Transaction> | |
{ | |
new Transaction | |
{ | |
description = "Transaction description.", | |
invoice_number = "001", | |
amount = new Amount | |
{ | |
currency = "USD", | |
total = "100.00", | |
details = new Details | |
{ | |
tax = "15", | |
shipping = "10", | |
subtotal = "75" | |
} | |
}, | |
item_list = new ItemList | |
{ | |
items = new List<Item> | |
{ | |
new Item | |
{ | |
name = "Item Name", | |
currency = "USD", | |
price = "15", | |
quantity = "5", | |
sku = "sku" | |
} | |
} | |
} | |
} | |
}, | |
redirect_urls = new RedirectUrls | |
{ | |
return_url = "http://ekjov.dyn.pingec.si:8888/mobileclient.aspx?h_actkey=paypal_return", | |
cancel_url = "http://ekjov.dyn.pingec.si:8888/mobileclient.aspx?h_actkey=paypal_cancel" | |
} | |
}); | |
// Get approval_url for buyer | |
var approval_url_for_buyer = ""; | |
var links = payment.links.GetEnumerator(); | |
while (links.MoveNext()) | |
{ | |
var link = links.Current; | |
if (link.rel.ToLower().Trim().Equals("approval_url")) | |
{ | |
approval_url_for_buyer = link.href; | |
} | |
} | |
// Redirect buyer's browser to approval_url to authorize the payment | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment