public ActionResult IPNHandler() { var stream = new StreamReader(_httpContext.Request.InputStream); var invoice = JsonConvert.DeserializeObject <Invoice>(stream.ReadToEnd()); if (invoice == null) { return(Content("")); } var id = invoice.Id; var bitpay = new BitPayAPI.BitPay(envUrl: BitpayHelper.GetEnvironmentUrl(_bitpaySettings)); invoice = bitpay.getInvoice(id); var order = _orderService.GetOrderById(int.Parse(invoice.PosData)); if (order == null) { return(Content("")); } order.OrderNotes.Add(new OrderNote { CreatedOnUtc = DateTime.UtcNow, DisplayToCustomer = false, Note = $"Bitpay IPN handler. Incoming status is: {invoice.Status}" }); _orderService.UpdateOrder(order); switch (invoice.Status) { case "new": break; case "paid": case "confirmed": case "complete": if (_orderProcessingService.CanMarkOrderAsPaid(order)) { _orderProcessingService.MarkOrderAsPaid(order); } break; default: break; } return(Content("")); }
public ActionResult Configure(ConfigurationModel model) { if (!ModelState.IsValid) { return(Configure()); } //load settings for a chosen store scope var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext); var bitpaySettings = _settingService.LoadSetting <BitpayPaymentSettings>(storeScope); // pair client with server if (!model.PairingCode.Equals(bitpaySettings.PairingCode, StringComparison.InvariantCultureIgnoreCase)) { var bitpay = new BitPayAPI.BitPay(envUrl: BitpayHelper.GetEnvironmentUrl(bitpaySettings)); if (!bitpay.clientIsAuthorized(BitPayAPI.BitPay.FACADE_POS)) { bitpay.authorizeClient(model.PairingCode); } } //save settings bitpaySettings.PairingCode = model.PairingCode; bitpaySettings.UseSandbox = model.UseSandbox; bitpaySettings.CustomUrl = model.CustomUrl; bitpaySettings.TransactionSpeed = (TransactionSpeed)model.TransactionSpeedId; /* We do not clear cache after each setting update. * This behavior can increase performance because cached settings will not be cleared * and loaded from database after each update */ _settingService.SaveSettingOverridablePerStore(bitpaySettings, x => x.PairingCode, model.PairingCode_OverrideForStore, storeScope, false); _settingService.SaveSettingOverridablePerStore(bitpaySettings, x => x.CustomUrl, model.CustomUrl_OverrideForStore, storeScope, false); _settingService.SaveSettingOverridablePerStore(bitpaySettings, x => x.UseSandbox, model.UseSandbox_OverrideForStore, storeScope, false); _settingService.SaveSettingOverridablePerStore(bitpaySettings, x => x.TransactionSpeed, model.TransactionSpeed_OverrideForStore, storeScope, false); //now clear settings cache _settingService.ClearCache(); SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved")); return(Configure()); }
/// <summary> /// Post process payment (used by payment gateways that require redirecting to a third-party URL) /// </summary> /// <param name="postProcessPaymentRequest">Payment info required for an order processing</param> public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest) { var invoice = new Invoice { BuyerName = $"{postProcessPaymentRequest.Order.BillingAddress.FirstName} {postProcessPaymentRequest.Order.BillingAddress.LastName}", BuyerEmail = postProcessPaymentRequest.Order.BillingAddress.Email, BuyerAddress1 = postProcessPaymentRequest.Order.BillingAddress.Address1, BuyerAddress2 = postProcessPaymentRequest.Order.BillingAddress.Address2, BuyerCity = postProcessPaymentRequest.Order.BillingAddress.City, TransactionSpeed = _bitpaySettings.TransactionSpeed.ToString().ToLowerInvariant(), OrderId = postProcessPaymentRequest.Order.Id.ToString(), PosData = postProcessPaymentRequest.Order.Id.ToString(), Currency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode, NotificationURL = $"{_webHelper.GetStoreLocation()}Plugins/PaymentBitpay/IPNHandler", RedirectURL = $"{_webHelper.GetStoreLocation()}orderdetails/{postProcessPaymentRequest.Order.Id}", FullNotifications = true, Price = Convert.ToDouble(postProcessPaymentRequest.Order.OrderTotal) }; if (postProcessPaymentRequest.Order.BillingAddress.StateProvince != null) { invoice.BuyerState = postProcessPaymentRequest.Order.BillingAddress.StateProvince.Abbreviation; } if (postProcessPaymentRequest.Order.BillingAddress.Country != null) { invoice.BuyerCountry = postProcessPaymentRequest.Order.BillingAddress.Country.TwoLetterIsoCode; } var bitpay = new BitPayAPI.BitPay(envUrl: BitpayHelper.GetEnvironmentUrl(_bitpaySettings)); try { invoice = bitpay.createInvoice(invoice); if (!string.IsNullOrEmpty(invoice.Id)) { postProcessPaymentRequest.Order.OrderNotes.Add(new OrderNote { CreatedOnUtc = DateTime.UtcNow, DisplayToCustomer = false, Note = $"Invoice initiated successfully. Id: {invoice.Id}, Status: {invoice.Status}" }); postProcessPaymentRequest.Order.AuthorizationTransactionId = invoice.Id; } } catch (Exception ex) { _logger.Error("Error creating invoice", ex); postProcessPaymentRequest.Order.OrderNotes.Add(new OrderNote { CreatedOnUtc = DateTime.UtcNow, DisplayToCustomer = false, Note = $"Error creating invoice: {ex.Message}" }); } _orderService.UpdateOrder(postProcessPaymentRequest.Order); if (!string.IsNullOrEmpty(invoice.Id)) { _httpContext.Response.Redirect(invoice.Url); } }