private async Task ExistingSubscriptionPaymentSkippedUpdateSubscription(string payPalAgreement) { //get a user with PayPal agreement. var userSubscription = _dbcontext.UserSubscriptions.Where(x => x.PayPalAgreementId == payPalAgreement && !String.IsNullOrEmpty(x.PayPalAgreementId)).FirstOrDefault(); if (userSubscription != null) { //get user subscription var client = _clientFactory.GetClient(); AgreementGetRequest request = new AgreementGetRequest(userSubscription.PayPalAgreementId); BraintreeHttp.HttpResponse result = await client.Execute(request); Agreement agreement = result.Result <Agreement>(); string expiryDateAsString = agreement.AgreementDetails.NextBillingDate.Substring(0, 10); DateTime expiryDate = DateTime.ParseExact(expiryDateAsString, "yyyy-MM-dd", null); userSubscription.ExpiryDate = expiryDate; userSubscription.State = "Cancelled"; _dbcontext.UserSubscriptions.Update(userSubscription); await _dbcontext.SaveChangesAsync(); var userMessage = $"Your subscription for {userSubscription.Description} has been cancelled due to SKIPPED payment. You will have access until {userSubscription.ExpiryDate}."; await EmailCustomer(userSubscription.PayerEmail, userMessage, "AndyTipster subscription has been cancelled due to SKIPPED payment."); var adminMessage = $"Regular Subscription PAYMENT SKIPPED: {userSubscription.PayerFirstName} {userSubscription.PayerLastName} " + $": {userSubscription.PayerEmail} have SKIPPED PAYMENT {userSubscription.Description}. They will have access until : {userSubscription.ExpiryDate}."; await EmailAdmin(adminMessage, "Regular Subscription SKIPPED PAYMENT, Subscription UPDATED."); } }
private async Task TellPayPalToCancelSubscription(string payPalAgreement) { try { var client = _clientFactory.GetClient(); var requestForPayPalAgreement = new AgreementGetRequest(payPalAgreement); var result = await client.Execute(requestForPayPalAgreement); var agreement = result.Result <Agreement>(); var request = new AgreementCancelRequest(payPalAgreement).RequestBody(new AgreementStateDescriptor() { Note = "Cancelled" }); await client.Execute(request); } catch (Exception ex) { // save error in the database. PaypalErrors payPalReturnedError = new PaypalErrors() { Exception = ex.Message, DateTime = DateTime.Now }; this._dbContext.PaypalErrors.Add(payPalReturnedError); await this._dbContext.SaveChangesAsync(); } }
public async Task <IActionResult> Details(string id) { var client = _clientFactory.GetClient(); var request = new AgreementGetRequest(id); var result = await client.Execute(request); var agreement = result.Result <Agreement>(); return(View(agreement)); }
public async Task <IActionResult> Index() { //get user subscription var client = _clientFactory.GetClient(); AgreementGetRequest request = new AgreementGetRequest("I-GY2WH34CHXR5"); BraintreeHttp.HttpResponse result = await client.Execute(request); Agreement agreement = result.Result <Agreement>(); var payPalCancelSubscription = System.Net.WebRequest.Create("https://api.paypal.com/v1/payments/billing-agreements/I-GY2WH34CHXR5/cancel"); //Set values for the verification request payPalCancelSubscription.Method = "POST"; var response = payPalCancelSubscription.GetResponse(); //await AddEmailtoList("*****@*****.**"); return(View()); }
private async Task TellPayPalToCancelSubscription(string payPalAgreement) { try { var client = _clientFactory.GetClient(); var requestForPayPalAgreement = new AgreementGetRequest(payPalAgreement); var result = await client.Execute(requestForPayPalAgreement); var agreement = result.Result <Agreement>(); var request = new AgreementCancelRequest(payPalAgreement).RequestBody(new AgreementStateDescriptor() { Note = "Cancelled" }); await client.Execute(request); var message = $"PayPal has been notified to cancel Subscription :{agreement.Id} for the package : {agreement.Description} under {agreement.Name}."; var subject = $"PayPal has been notified to Cancel Subscription :{agreement.Id}"; await EmailAdmin(message, subject); //await EmailSuperAdmin("Notify PayPal to Cancel Subscription SUCCESS", "Notify PayPal to Cancel Subscription SUCCESS"); } catch (Exception ex) { // save error in the database. PaypalErrors payPalReturnedError = new PaypalErrors() { Exception = ex.Message, DateTime = DateTime.Now }; _dbcontext.PaypalErrors.Add(payPalReturnedError); await _dbcontext.SaveChangesAsync(); await EmailSuperAdmin($"Notify PayPal ({payPalAgreement}) to Cancel Subscription Failed", "Notify PayPal to Cancel Subscription Failed"); } }
private async Task CheckUpdateUserSubscriptionDetails(ApplicationUser user) { List <UserSubscriptions> userSubs = _dbContext.UserSubscriptions .Where(x => x.UserId == user.Id && x.PayPalAgreementId != null) .ToList(); if (userSubs.Count() > 0) { List <string> userSubscriptionIds = _dbContext.UserSubscriptions .Where(x => x.UserId == user.Id && x.PayPalAgreementId != null) .Select(x => x.PayPalAgreementId) .ToList(); foreach (var Id in userSubscriptionIds) { //get user subscription var client = _clientFactory.GetClient(); AgreementGetRequest request = new AgreementGetRequest(Id); BraintreeHttp.HttpResponse result = await client.Execute(request); Agreement agreement = result.Result <Agreement>(); //get user subscription UserSubscriptions userExistingSubscription = user.Subscriptions.Where(x => x.PayPalAgreementId == Id).FirstOrDefault(); //first time user is cancelling. if (agreement.State == "Cancelled" && userExistingSubscription.State != "Cancelled") { //if this is the first cancellation. if (userExistingSubscription.ExpiryDate.Year < 1995) { //set expiry date in the User Subscription to the next billing date. //"last_payment_date": "2020-06-15T17:57:02Z", // "next_billing_date": "2020-07-15T10:00:00Z", string expiryDateAsString = agreement.AgreementDetails.NextBillingDate.Substring(0, 10); DateTime expiryDate = DateTime.ParseExact(expiryDateAsString, "yyyy-MM-dd", null); userExistingSubscription.ExpiryDate = expiryDate; //update user subscription status. userExistingSubscription.State = agreement.State; //only send cancellation email when Paypal first sends cancellation confirmation. var cancellationEmailAlreadyBeenSent = _dbContext.UserSubscriptions.Where(x => x.UserId == user.Id && x.PayPalAgreementId == agreement.Id && x.State != "Cancelled").Any(); if (!cancellationEmailAlreadyBeenSent) { //Send user an email and let them know expiry date. var confirmationHtml = $"<h2>As per your request, your Subscription <strong>{userExistingSubscription.Description}</strong> has been cancelled.</h2> <p>However, you can continue enjoy your access till your paid period expired on {userExistingSubscription.ExpiryDate}.</p>"; var sendGridKey = _configuration.GetValue <string>("SendGridApi"); await Emailer.SendEmail(user.Email, "AndyTipster Subscription, has been cancelled", confirmationHtml, sendGridKey); //Send admin an email and let them know expiry date. var confirmationAdminHtml = $"<h2>User {user.Email} has cancelled their subscription for <strong>{userExistingSubscription.Description}</strong>.</h2> <p>However, user has access till their paid period expired on {userExistingSubscription.ExpiryDate}.</p><p>An email confirmation has been sent to user on {user.Email}</p>"; await Emailer.SendEmail("*****@*****.**", "A user has cancelled a Subscription", confirmationAdminHtml, sendGridKey); } } } if (agreement.State == "Cancelled" && userExistingSubscription.State == "Cancelled") { //check if subscription date has expired. if (userExistingSubscription.ExpiryDate < DateTime.Now) // user subs expired, delete their subscription. { //delete Subscription. var subsTobeDeleted = _dbContext.Subscriptions.Where(x => x.PayPalAgreementId == agreement.Id).FirstOrDefault(); if (subsTobeDeleted != null) { _dbContext.Subscriptions.Remove(subsTobeDeleted); } //delete user Subscription var userSubsToBeDeleted = _dbContext.UserSubscriptions.Where(x => x.UserId == user.Id && x.State == "Cancelled" && x.PayPalAgreementId == agreement.Id).FirstOrDefault(); if (userSubsToBeDeleted != null) { _dbContext.UserSubscriptions.Remove(userSubsToBeDeleted); } _dbContext.SaveChanges(); //Send user an email and let them know subscription now has expired. var expiredHtml = $"<h2>Your Subscription <strong>{userExistingSubscription.Description}</strong> has now expired.</h2>"; var sendGridKey = _configuration.GetValue <string>("SendGridApi"); await Emailer.SendEmail(user.Email, "Andy Tipster Subscription has expired.", expiredHtml, sendGridKey); //Send admin an email and let them know expiry. var expiredAdminHtml = $"<h2>User {user.Email} subscription for <strong>{userExistingSubscription.Description}</strong>. has now expired.</h2><p>An email confirmation has been sent to user on {user.Email}</p>"; await Emailer.SendEmail("*****@*****.**", $"{userExistingSubscription.PayerEmail} : Subscription has expired.", expiredAdminHtml, sendGridKey); } } await userManager.UpdateAsync(user); } } }