yavsc/Yavsc/ApiControllers/PaymentApiController.cs

73 lines
2.4 KiB
C#
Raw Normal View History

2017-05-05 23:37:07 +02:00
using System.Threading.Tasks;
2017-05-12 16:29:47 +02:00
using Microsoft.AspNet.Authorization;
2017-05-05 23:37:07 +02:00
using Microsoft.AspNet.Mvc;
2017-05-12 12:15:57 +02:00
using Microsoft.Data.Entity;
2017-05-05 23:37:07 +02:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using PayPal.Api;
using Yavsc.Helpers;
using Yavsc.Models;
2017-05-12 16:29:47 +02:00
using Yavsc.Models.Billing;
2017-05-12 12:15:57 +02:00
using Yavsc.ViewModels.PayPal;
2017-05-05 23:37:07 +02:00
namespace Yavsc.ApiControllers
{
[Route("api/payment")]
public class PaymentApiController : Controller
{
private ApplicationDbContext dbContext;
PayPalSettings paymentSettings;
private SiteSettings siteSettings;
private readonly ILogger _logger;
public PaymentApiController(
ApplicationDbContext dbContext,
IOptions<PayPalSettings> paypalSettingsReceiver,
IOptions<SiteSettings> siteSettingsReceiver,
ILoggerFactory loggerFactory)
{
this.dbContext = dbContext;
paymentSettings = paypalSettingsReceiver.Value;
siteSettings = siteSettingsReceiver.Value;
_logger = loggerFactory.CreateLogger<PaymentApiController>();
}
2017-05-12 12:15:57 +02:00
public async Task<IActionResult> Info(string paymentId)
{
var result = new PaymentInfo {
DbContent = await dbContext.PaypalPayments.SingleAsync(
p=>p.PaypalPaymentId==paymentId)
};
await Task.Run( () => {
2017-05-16 20:58:52 +02:00
var apiContext = PayPalHelpers.CreateAPIContext();
2017-05-12 12:15:57 +02:00
result.FromPaypal = Payment.Get(apiContext,paymentId);
});
2017-05-05 23:37:07 +02:00
2017-05-12 12:15:57 +02:00
return Ok(result);
}
2017-05-05 23:37:07 +02:00
[HttpPost("execute")]
public async Task<IActionResult> Execute(string paymentId, string payerId)
{
Payment result=null;
await Task.Run( () => {
2017-05-16 20:58:52 +02:00
var apiContext = PayPalHelpers.CreateAPIContext();
2017-05-05 23:37:07 +02:00
var payment = Payment.Get(apiContext,paymentId);
var execution = new PaymentExecution();
execution.payer_id = payerId;
execution.transactions = payment.transactions;
result = payment.Execute(apiContext,execution);
});
2017-05-12 12:15:57 +02:00
2017-05-05 23:37:07 +02:00
return Ok(result);
}
2017-05-12 16:29:47 +02:00
[HttpPost("create"),AllowAnonymous]
2017-05-15 13:36:14 +02:00
public IActionResult Create()
2017-05-12 16:29:47 +02:00
{
2017-05-16 20:58:52 +02:00
var apiContext = PayPalHelpers.CreateAPIContext();
2017-05-16 02:16:09 +02:00
Payment result= Request.CreatePayment("Command",apiContext,new Estimate());
2017-05-12 16:29:47 +02:00
return Ok(Payment.Create(apiContext,result));
}
2017-05-05 23:37:07 +02:00
}
}