/// <summary> /// Approve purchase gift card /// </summary> /// <param name="adminMember">Admin member</param> /// <param name="purchasedGiftCardId">Purchased gift card identifier</param> /// <returns></returns> async public Task ApprovePurchaseGiftCard(FamilyMember adminMember, int purchasedGiftCardId) { var purchasedGiftCard = Repository.Table <PurchasedGiftCard>().Include(p => p.FamilyMember).SingleOrDefault(p => p.Id == purchasedGiftCardId); var giftPurchaseRequest = new GyftPurchaseRequest { ShopCardId = purchasedGiftCard.CardId, ToEmail = adminMember.User.Email, ResellerReference = "", Notes = "Child purchase", FirstName = purchasedGiftCard.FamilyMember.Firstname, LastName = purchasedGiftCard.FamilyMember.Lastname, Gender = purchasedGiftCard.FamilyMember.Gender?.ToString() ?? string.Empty, Birthday = purchasedGiftCard.FamilyMember.DateOfBirth?.ToString("dd/MM/yyyy") ?? string.Empty }; try { if (!purchasedGiftCard.BankTransactionID.HasValue) { if (!_bankService.IsBankLinked(adminMember.Id)) { throw new InvalidOperationException("Bank is not linked or verified!"); } // Tranfer amount from customer account to program account var transactionResult = _transactionService.Transfer(adminMember.Id, purchasedGiftCard.Amount, PaymentType.GiftCard, TransferType.InternalToBusyKidInternalAccount); if (!transactionResult.HasValue) { throw new InvalidOperationException("Unable to process the transaction. Please contact bank or mail to us!"); } // Update purchased gift card purchasedGiftCard.BankTransactionID = transactionResult; UpdatePurchasedGiftCard(purchasedGiftCard); } // Purchase gift card from gyft api, then update status var giftCardUrl = await _gyftService.PurchaseGiftCard(giftPurchaseRequest); purchasedGiftCard.GiftCardUrl = giftCardUrl; purchasedGiftCard.Status = ApprovalStatus.Completed; UpdatePurchasedGiftCard(purchasedGiftCard); } catch (Exception ex) { _transactionService.SaveTransactionLog(adminMember.Id, ex.Message, purchasedGiftCard.Amount); throw ex; } }
/// <summary> /// Purchase gift card /// </summary> /// <param name="giftPurchaseRequest">Gift purchase request</param> /// <returns>Gift card url</returns> async public Task <string> PurchaseGiftCard(GyftPurchaseRequest giftPurchaseRequest) { var jsonPostData = JsonConvert.SerializeObject(giftPurchaseRequest); var url = string.Format("{0}{1}?api_key={2}", _gyftApiBaseUrl, _purchaseCardUrl, _gyftApiKey); var response = await CallApi(url, WebRequestMethods.Http.Post, jsonPostData); if (response != null) { var responseString = await response.Content.ReadAsStringAsync(); var deserializeResult = JsonConvert.DeserializeObject <dynamic>(responseString); if (deserializeResult != null) { return(deserializeResult.url); } } else { throw new InvalidOperationException("Unable to complete the purchase. Please try again later."); } return(string.Empty); }