yavsc/Yavsc/Helpers/PayPalHelpers.cs

150 lines
5.7 KiB
C#
Raw Normal View History

2017-05-05 23:37:07 +02:00
using System.Collections.Generic;
2017-05-12 12:15:57 +02:00
using Microsoft.Extensions.Logging;
2017-05-12 16:29:47 +02:00
using Newtonsoft.Json;
2017-05-05 23:37:07 +02:00
using PayPal.Api;
2017-05-12 16:29:47 +02:00
using PayPal.Exception;
2017-05-05 23:37:07 +02:00
using Yavsc.Models.Billing;
namespace Yavsc.Helpers
{
public static class PayPalHelpers
{
public static APIContext CreateAPIContext(this PayPalSettings settings)
{
Dictionary<string,string> config = new Dictionary<string,string>();
config.Add("mode",settings.Mode);
config.Add("clientId",settings.ClientId);
config.Add("clientSecret",settings.Secret);
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
var apiContext = new APIContext(accessToken);
return apiContext;
}
2017-05-15 13:36:14 +02:00
public static Payment CreatePayment(this APIContext apiContext, NominativeServiceCommand query, string controllerName, string intent = "sale", ILogger logger=null)
2017-05-05 23:37:07 +02:00
{
var queryType = query.GetType().Name;
var transaction = new Transaction
{
2017-05-12 16:29:47 +02:00
description = query.Description,
invoice_number = query.Id.ToString(),
custom = query.GetType().Name + "/"+ query.Id.ToString()
2017-05-05 23:37:07 +02:00
};
2017-05-12 12:15:57 +02:00
// transaction.item_list.shipping_address.city
// country_code default_address id
// line1 line2 preferred_address recipient_name state status type
2017-05-12 16:29:47 +02:00
/* transaction.item_list = new ItemList();
2017-05-12 12:15:57 +02:00
if (query.Client.PostalAddress!=null) {
2017-05-12 16:29:47 +02:00
var address = query.Client.PostalAddress?.Address;
if (address!=null) {
var parts = new Stack<string> ( address.Split(',') );
var country = parts.Pop().Trim();
var city = parts.Pop().Trim().Split(' ');
var line1 = parts.First().Trim();
var line2 = string.Join(" - ",parts.Skip(1));
transaction.item_list.shipping_address = new ShippingAddress {
line1 = line1,
line2 = line2,
city = city[1],
postal_code = city[0],
country_code = country == "France" ? "fr" : country
};
}
2017-05-12 12:15:57 +02:00
}
2017-05-05 23:37:07 +02:00
transaction.item_list.shipping_phone_number = query.Client.PhoneNumber;
2017-05-12 16:29:47 +02:00
var items = query.GetBillItems();
transaction.item_list.items = items.Select(i => new Item {
2017-05-12 12:15:57 +02:00
name = i.Name,
description = i.Description,
2017-05-12 16:29:47 +02:00
quantity = i.Count.ToString(),
2017-05-12 12:15:57 +02:00
price = i.UnitaryCost.ToString("F2"),
currency = "EUR",
2017-05-12 16:29:47 +02:00
sku = "sku"
// postback_data=
// supplementary_data=
2017-05-12 12:15:57 +02:00
}).ToList();
2017-05-12 16:29:47 +02:00
*/ var total = query.GetBillItems().Addition().ToString("F2");
transaction.amount = new Amount {
currency = "EUR",
total = total
};
var payment = new Payment
2017-05-05 23:37:07 +02:00
{
2017-05-12 16:29:47 +02:00
intent = intent, // "sale", "order", "authorize"
payer = new Payer
{
payment_method = "paypal"
},
transactions = new List<Transaction> { transaction },
redirect_urls = new RedirectUrls
{
2017-05-15 13:36:14 +02:00
// FIXME s/HairCutCommand/{query.DedicatedCommandController}/{query.Id}
return_url = Startup.Audience+ $"/{controllerName}/Detail/"+query.Id.ToString(),
cancel_url = Startup.Audience+ $"/{controllerName}/ClientCancel/"+query.Id.ToString()
2017-05-12 16:29:47 +02:00
}
};
logger.LogWarning("Sending: "+JsonConvert.SerializeObject(payment));
logger.LogWarning("Using: " + JsonConvert.SerializeObject(apiContext));
Payment result = null;
try {
result = Payment.Create(apiContext,payment);
}
catch (PaymentsException ex) {
logger.LogError (ex.Message);
}
return result;
2017-05-05 23:37:07 +02:00
}
2017-05-12 16:29:47 +02:00
public static Payment CreatePayment(this APIContext apiContext, Estimate estimation)
2017-05-05 23:37:07 +02:00
{
var payment = Payment.Create(apiContext,
new Payment
{
intent = "order", // "sale", "order", "authorize"
payer = new Payer
{
payment_method = "paypal"
},
transactions = new List<Transaction>
{
new Transaction
{
description = "Transaction description.",
invoice_number = estimation.Id.ToString(),
amount = new Amount
{
currency = "EUR",
total = "0.11",
details = new Details
{
tax = "0.01",
shipping = "0.02",
subtotal = "0.08"
}
},
item_list = new ItemList
{
items = new List<Item>
{
new Item
{
name = "nah",
currency = "EUR",
price = "0.02",
quantity = "4",
sku = "sku"
}
}
}
}
},
redirect_urls = new RedirectUrls
{
return_url = Startup.Audience+ "/Manage/Credit/Return",
cancel_url = Startup.Audience+ "/Manage/Credit/Cancel"
}
});
return payment;
}
}
}