/// <summary> /// Finialize payments /// </summary> /// <param name="payid"></param> /// <param name="tokenn"></param> public void ExecutePayment(string payid, string tokenn) { //validate if (String.IsNullOrEmpty(payid)) { throw new Exceptions.InvalidDataException("Invalid Request"); } if (String.IsNullOrEmpty(paymentid)) { throw new Exceptions.InvalidDataException("Invalid Request"); } if (String.IsNullOrEmpty(accessToken)) { throw new Exceptions.InvalidDataException("Invalid Request"); } var client = new RestClient("https://api.sandbox.paypal.com/v1/payments/payment/" + paymentid + "/execute/"); var request = new RestRequest(Method.POST); request.RequestFormat = DataFormat.Json; //string token=GetAccessToken(); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", accessToken); request.AddBody(new { payer_id = payid.Trim() }); try { var response = client.Execute(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var content = response.Content; accessToken = null; paymentid = null; var payPalResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <DTO.PaypalPayExecuteResponse>(content); var pay = new Payment(this.userId); foreach (DTO.PayPalTransactions transaction in payPalResponse.transactions) { string amount = transaction.amount.total; DateTime createtime = payPalResponse.update_time; pay.SaveUserPayment(Convert.ToDecimal(amount), createtime, Helpers.SiteInfo.MonthlyPaymentId); } } else { throw new Exceptions.InvalidDataException(response.StatusDescription); } } catch (Exception ex) { throw new Exceptions.InvalidDataException(ex.Message); } }
public void MakePayment(string stripeToken, string coupon) { Helpers.SslSecurity.Callback(); if (stripeToken.Trim() == "") { throw new Exceptions.InvalidStripeTokenException("Making a payment requires a stripetoken"); } var customerDetails = GetSavedCustomerStripeDetails(); if (customerDetails == null) { CreateStripeCustomer(stripeToken, coupon); } else if (customerDetails.Item2.Trim() == "") { CreateStripeSubscription(stripeToken, coupon); } var pay = new Payment(this._userId); pay.SaveUserPayment(Helpers.SiteInfo.CurrentMonthlyRate, DateTime.UtcNow, Helpers.SiteInfo.MonthlyPaymentId); }
/// <summary> /// As long as payments continue the users account will not expire /// </summary> private void CheckForNewPayments() { // retrieve all events for the past day and proceed to process // subscription cancellations. using (var db = Setup.DbFactory) { var data = db.Query <LibPoco.DatabaseInfo>("SELECT * FROM DatabaseInfo"); if (data.Count() != 1) { throw new LibLogic.Exceptions.InvalidDataException("Incorrect data in DatabaseInfo table. To many or too few rows."); } Helpers.SslSecurity.Callback(); var eventService = new StripeChargeService(LibLogic.Helpers.SiteInfo.StripeAPISecretKey); var options = new StripeChargeListOptions() { Limit = 1000, Created = new StripeDateFilter { GreaterThanOrEqual = data.First().LastDailyProcess } }; IEnumerable <StripeCharge> response = eventService.List(options); foreach (var evt in response) { //if (evt.LiveMode == false) //{ // continue; //} // A new payment has been made. string stripeCustomerId = evt.CustomerId; //var users = db.Select<LibPoco.Users>(q => q.PaymentExpired == false); var user = db.Query <LibPoco.Users>("SELECT * FROM Users WHERE StripeCustomerAccount=@StripeCustomerAccount", new { StripeCustomerAccount = stripeCustomerId }); if (user == null || user.Count() != 1) { var ex = new LibLogic.Exceptions.InvalidDataException("Cannot find stripe customer data in users table. Stripe Customer Account: " + stripeCustomerId); LibLogic.Helpers.Logging.Log(ex); Setup.Email.SendMail_BackgroundThread("Error running DailyProcessing: " + ex.Message, "Error running DailyProcessing", LibLogic.Helpers.SiteInfo.AdminEmail, true, null, Email.EmailTemplates.Generic); continue; } int userid = user.First().Id; var pay = new Payments.Payment(userid); // amount in cents pay.SaveUserPayment((decimal)(evt.Amount / 100.0m), evt.Created, Helpers.SiteInfo.MonthlyPaymentId); LibLogic.ActionLog.Log_BackgroundThread("Payment made", userid); } data.First().LastDailyProcess = DateTime.UtcNow; db.Update(data.First()); } }