public async Task <bool> ActivateChargeAsync(long chargeId) { var shopifyCharge = await _shopifyChargeStore.GetShopifyChargeByChargeIdAsync(chargeId); var shopifyStore = shopifyCharge?.ShopifyStore; if (shopifyCharge == null || shopifyStore == null) { _logger.Error($"Shopify charge({chargeId}) was not found."); return(false); } try { if (!shopifyCharge.ChargeType.HasValue) { return(false); } var chargeService = ShopifyChargeServiceFactory.Create(shopifyStore.Shop, shopifyStore.AccessToken, shopifyCharge.ChargeType.Value); var charge = await chargeService.GetChargeByIdAsync(chargeId); switch (charge.Status) { case "accepted": await chargeService.ActivateChargeAsync(chargeId); shopifyCharge.ActivatedOn = charge.ActivatedOn; shopifyCharge.CreatedAt = charge.CreatedAt; await _shopifyChargeStore.AddOrUpdateShopifyChargeAsync(shopifyCharge); var token = await _tokenClient.GetTokenAsync(); _logger.Info($"Sending request to update subscription = {shopifyCharge.NDASubscriptionId} for" + $" account {shopifyCharge.NDAAccountId} with token = {token}"); return(await _accountClient.UpdateSubscriptionAsync(token, shopifyCharge.NDAAccountId, shopifyCharge.NDASubscriptionId)); case "active": return(true); default: return(false); } } catch (ShopifyException e) { _logger.Error( $"Error happened when trying to activate charge({chargeId}) for {shopifyStore.Shop}. Message: {e.Message}"); return(false); } }
public async Task FreezeInactiveChargesAsync() { var charges = await _shopifyChargeStore.GetRecurringChargesAsync(); if (charges == null || Equals(!charges.Any())) { return; } foreach (var shopifyCharge in charges) { try { var chargeService = ShopifyChargeServiceFactory.Create(shopifyCharge.ShopifyStore.Shop, shopifyCharge.ShopifyStore.AccessToken, shopifyCharge.ChargeType ?? ShopifyChargeType.Monthly); var chargeDto = await chargeService.GetChargeByIdAsync(shopifyCharge.ChargeId); switch (chargeDto.Status) { case "declined": case "expired": case "frozen": case "cancelled": var token = await _tokenClient.GetTokenAsync(); await _accountClient.FreezeSubscriptionAsync(token, shopifyCharge.NDAAccountId); shopifyCharge.IsFrozen = true; shopifyCharge.CancelledOn = chargeDto.CancelledOn; await _shopifyChargeStore.AddOrUpdateShopifyChargeAsync(shopifyCharge); break; } } catch (ShopifyException e) { _logger.Error( $"Error happened when trying to freeze charge({shopifyCharge.ChargeId}). Message: {e.Message}"); } } }
public async Task <string> ChargeAsync(int NDAShopId, string NDAAccountId, string NDASubscriptionId, int subscriptionDurationId, string returnUrl) { var shop = await _shopifyStoreService.GetShopifyStoreByNDAShopIdAsync(NDAShopId); if (shop == null) { _logger.Error($"NDA shop({NDAShopId}) was not found."); return(null); } var token = await _tokenClient.GetTokenAsync(); (bool successful, SubscriptionDto subscription) = await _accountClient.GetSubscriptionAsync(token, NDASubscriptionId, subscriptionDurationId); if (!successful) { _logger.Error($"Failed to get subscription({NDASubscriptionId})"); return(null); } returnUrl = GetAppUrl() + returnUrl; if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Absolute)) { return(null); } if (!Enum.IsDefined(typeof(ShopifyChargeType), subscription.Duration)) { _logger.Error($"Unknown charge type '{subscription.Duration}'"); return(null); } try { var chargeType = (ShopifyChargeType)subscription.Duration; var chargeService = ShopifyChargeServiceFactory.Create(shop.Shop, shop.AccessToken, chargeType); var confirmationUrl = await chargeService.CreateAsync(subscription.Name, subscription.Price, returnUrl, _shopifyOptions.UseTestCharge); if (!Uri.IsWellFormedUriString(confirmationUrl, UriKind.Absolute)) { return(null); } var uri = new Uri(confirmationUrl); var chargeId = long.Parse(uri.Segments[3].TrimEnd('/')); ShopifyCharge shopifyCharge; if (shop.Charge == null) { shopifyCharge = new ShopifyCharge { ShopifyStoreId = shop.Id, ChargeId = chargeId, IsRecurringCharge = subscription.IsRecurring, NDASubscriptionId = subscription.Id, ChargeType = chargeType, NDAAccountId = NDAAccountId, }; } else { shopifyCharge = shop.Charge; shopifyCharge.ChargeId = chargeId; shopifyCharge.IsRecurringCharge = subscription.IsRecurring; shopifyCharge.NDASubscriptionId = subscription.Id; shopifyCharge.ChargeType = chargeType; shopifyCharge.NDAAccountId = NDAAccountId; shopifyCharge.ActivatedOn = null; shopifyCharge.CreatedAt = null; shopifyCharge.CancelledOn = null; } await _shopifyChargeStore.AddOrUpdateShopifyChargeAsync(shopifyCharge); return(confirmationUrl); } catch (ShopifyException e) { _logger.Error( $"Error happened when trying to create charge for {shop.Shop}. Message: {e.Message}"); return(null); } }