/// <summary> /// Create offline payment for a ContentItem with a PaymentPart /// </summary> /// <param name="Id">ContentItem Id</param> public ActionResult Create(int Id) { var paymentPart = Services.ContentManager.Get<PaymentPart>(Id); if (paymentPart == null) { return new HttpNotFoundResult(); } decimal OutstandingAmount = paymentPart.PayableAmount - paymentPart.AmountPaid; if (OutstandingAmount <= 0) { Services.Notifier.Information(T("Nothing left to pay on this document.")); return Redirect(Url.ItemDisplayUrl(paymentPart)); } var transaction = new PaymentTransactionRecord() { Method = "Offline", Amount = OutstandingAmount, Date = _clock.UtcNow, Status = TransactionStatus.Pending }; _paymentService.AddTransaction(paymentPart, transaction); Services.Notifier.Information(T("Transaction reference : <b>{0}</b><br/>Transaction amount : <b>{1}</b>", paymentPart.Reference, OutstandingAmount.ToString("C", _currencyProvider.NumberFormat))); var offlinePaymentSettings = Services.WorkContext.CurrentSite.As<OfflinePaymentSettingsPart>(); return Redirect(Url.ItemDisplayUrl(offlinePaymentSettings.Content)); }
/// <summary> /// Create offline payment for a ContentItem with a PaymentPart /// </summary> /// <param name="Id">ContentItem Id</param> public ActionResult Create(int Id) { var paymentPart = Services.ContentManager.Get <PaymentPart>(Id); if (paymentPart == null) { return(new HttpNotFoundResult()); } decimal OutstandingAmount = paymentPart.PayableAmount - paymentPart.AmountPaid; if (OutstandingAmount <= 0) { Services.Notifier.Information(T("Nothing left to pay on this document.")); return(Redirect(Url.ItemDisplayUrl(paymentPart))); } var transaction = new PaymentTransactionRecord() { Method = "Offline", Amount = OutstandingAmount, Date = _clock.UtcNow, Status = TransactionStatus.Pending }; _paymentService.AddTransaction(paymentPart, transaction); Services.Notifier.Information(T("Transaction reference : <b>{0}</b><br/>Transaction amount : <b>{1}</b>", paymentPart.Reference, OutstandingAmount.ToString("C", _currencyProvider.NumberFormat))); var offlinePaymentSettings = Services.WorkContext.CurrentSite.As <OfflinePaymentSettingsPart>(); return(Redirect(Url.ItemDisplayUrl(offlinePaymentSettings.Content))); }
public void UpdateTransaction(PaymentTransactionRecord Transaction) { if (Transaction == null) { throw new ArgumentNullException("Transaction", "Transaction cannot be null."); } _transactionRepository.Update(Transaction); }
public void AddTransaction(PaymentPart Part, PaymentTransactionRecord Transaction) { if (Part == null) { throw new ArgumentNullException("Part", "PaymentPart cannot be null."); } if (Transaction == null) { throw new ArgumentNullException("Transaction", "Transaction cannot be null."); } Transaction.PaymentPartRecord = Part.Record; _transactionRepository.Create(Transaction); }
/// <summary> /// Create PayPal payment for a ContentItem with a PaymentPart /// </summary> /// <param name="Id">ContentItem Id</param> public async Task <ActionResult> Create(int Id) { var paymentPart = Services.ContentManager.Get <PaymentPart>(Id); if (paymentPart == null) { return(new HttpNotFoundResult()); } decimal OutstandingAmount = paymentPart.PayableAmount - paymentPart.AmountPaid; if (OutstandingAmount <= 0) { Services.Notifier.Information(T("Nothing left to pay on this document.")); return(Redirect(Url.ItemDisplayUrl(paymentPart))); } var transaction = new PaymentTransactionRecord() { Method = "PayPal", Amount = OutstandingAmount, Date = _clock.UtcNow, Status = TransactionStatus.Pending }; _paymentService.AddTransaction(paymentPart, transaction); var siteSettings = _siteService.GetSiteSettings(); string baseUrl = siteSettings.BaseUrl.TrimEnd(new char[] { ' ', '/' }); try { // Create web profile string webProfileId = await _apiService.CreateWebProfile(new WebProfile() { // Ensure unique name Name = paymentPart.Reference + "_" + transaction.Id, InputFields = new InputFields() { allow_note = false, NoShipping = 1 }, Presentation = new Presentation() { BrandName = siteSettings.SiteName } }); // Create payment var paymentCtx = await _apiService.CreatePaymentAsync(new Payment() { Intent = PaymentIntent.Sale, Payer = new Payer() { PaymentMethod = PaymentMethod.Paypal }, Transactions = new List <Transaction>() { new Transaction() { Amount = new Amount() { Total = OutstandingAmount, Currency = _currencyProvider.IsoCode }, Description = paymentPart.Reference + " - " + OutstandingAmount.ToString("C", _currencyProvider.NumberFormat) } }, RedirectUrls = new RedirectUrls() { ReturnUrl = baseUrl + Url.Action("Execute", new { id = transaction.Id }), CancelUrl = baseUrl + Url.Action("Cancel", new { id = transaction.Id }) }, ExperienceProfileId = webProfileId }); if (paymentCtx != null && paymentCtx.Payment.State == PaymentState.Created) { string approvalUrl = paymentCtx.Payment.Links.Where(lnk => lnk.Relation == "approval_url").Select(lnk => lnk.Href).FirstOrDefault(); if (string.IsNullOrWhiteSpace(approvalUrl)) { throw new Exception("Missing approval_url from Paypal response."); } transaction.TransactionId = paymentCtx.Payment.Id; transaction.Data = JsonConvert.SerializeObject(paymentCtx); if (paymentCtx.UseSandbox) { transaction.Method += " #SANDBOX#"; } _paymentService.UpdateTransaction(transaction); return(Redirect(approvalUrl)); } else { Services.Notifier.Error(T("Payment creation failed.")); } } catch { Services.Notifier.Error(T("We are encountering issues with PayPal service.")); } return(Redirect(Url.ItemDisplayUrl(paymentPart))); }
/// <summary> /// Create PayPal payment for a ContentItem with a PaymentPart /// </summary> /// <param name="Id">ContentItem Id</param> public async Task<ActionResult> Create(int Id) { var paymentPart = Services.ContentManager.Get<PaymentPart>(Id); if (paymentPart == null) { return new HttpNotFoundResult(); } decimal OutstandingAmount = paymentPart.PayableAmount - paymentPart.AmountPaid; if (OutstandingAmount <= 0) { Services.Notifier.Information(T("Nothing left to pay on this document.")); return Redirect(Url.ItemDisplayUrl(paymentPart)); } var transaction = new PaymentTransactionRecord() { Method = "PayPal", Amount = OutstandingAmount, Date = _clock.UtcNow, Status = TransactionStatus.Pending }; _paymentService.AddTransaction(paymentPart, transaction); var siteSettings = _siteService.GetSiteSettings(); string baseUrl = siteSettings.BaseUrl.TrimEnd(new char[] {' ', '/'}); try { // Create web profile string webProfileId = await _apiService.CreateWebProfile(new WebProfile() { // Ensure unique name Name = paymentPart.Reference + "_" + transaction.Id, InputFields = new InputFields() { allow_note = false, NoShipping = 1 }, Presentation = new Presentation() { BrandName = siteSettings.SiteName } }); // Create payment var paymentCtx = await _apiService.CreatePaymentAsync(new Payment() { Intent = PaymentIntent.Sale, Payer = new Payer() { PaymentMethod = PaymentMethod.Paypal }, Transactions = new List<Transaction>() { new Transaction() { Amount = new Amount(){ Total = OutstandingAmount, Currency = _currencyProvider.IsoCode }, Description = paymentPart.Reference + " - " + OutstandingAmount.ToString("C", _currencyProvider.NumberFormat) } }, RedirectUrls = new RedirectUrls() { ReturnUrl = baseUrl + Url.Action("Execute", new { id = transaction.Id }), CancelUrl = baseUrl + Url.Action("Cancel", new { id = transaction.Id }) }, ExperienceProfileId = webProfileId }); if (paymentCtx != null && paymentCtx.Payment.State == PaymentState.Created) { string approvalUrl = paymentCtx.Payment.Links.Where(lnk => lnk.Relation == "approval_url").Select(lnk => lnk.Href).FirstOrDefault(); if (string.IsNullOrWhiteSpace(approvalUrl)) { throw new Exception("Missing approval_url from Paypal response."); } transaction.TransactionId = paymentCtx.Payment.Id; transaction.Data = JsonConvert.SerializeObject(paymentCtx); if (paymentCtx.UseSandbox) { transaction.Method += " #SANDBOX#"; } _paymentService.UpdateTransaction(transaction); return Redirect(approvalUrl); } else { Services.Notifier.Error(T("Payment creation failed.")); } } catch { Services.Notifier.Error(T("We are encountering issues with PayPal service.")); } return Redirect(Url.ItemDisplayUrl(paymentPart)); }