respond['payment'] = "wrong" return self.render_to_json_response(respond) except stripe.error.StripeError, e: # Display a very generic error to the user, and maybe send # yourself an email saveConsultation.delete() body = e.json_body err = body['error'] stripeError = Stripe_Error(user=request.user,json_data=body,status=e.http_status,type=err.get('type',""),code=err.get('code',""),param=err.get('param',""),message=err.get('message',"")) stripeError.save() respond['payment'] = "wrong" return self.render_to_json_response(respond) except Exception, e: # Something else happened, completely unrelated to Stripe saveConsultation.delete() stripeError = Stripe_Error(user=request.user,json_data="Something else happened, completely unrelated to Stripe") stripeError.save() respond['payment'] = "wrong" return self.render_to_json_response(respond) else: respond['payment'] = "gone" return self.render_to_json_response(respond) else: pass return HttpResponse("done") def get_context_data(self, **kwargs): context = super(ConsultationPageView, self).get_context_data(**kwargs) self.object.hit() context['form'] = self.get_form(self.form_class) context['consultationCategories']= self.object.consultation_category_set.all()
def post(self, request, *args, **kwargs): duration = request.POST.get('duration', None) method = request.POST.get('method', None) promoCode = request.POST.get('promoCode', None) appointmentTime = request.POST.get('time', None) appointmentDate = request.POST.get('date', None) obj = self.get_object().consultation_professional practitionerTimeZone = self.getPractitionerTimeZone(obj) practitionerDateTime = self.getPractitionerDateTime(practitionerTimeZone) token = request.POST.get('stripeToken', None) if settings.STRIPE_MODE == "TEST": stripe.api_key = settings.STRIPE_TEST_SECRET_KEY elif settings.STRIPE_MODE == "LIVE": stripe.api_key = settings.STRIPE_LIVE_SECRET_KEY if self.request.is_ajax(): if token and duration and method and appointmentTime and appointmentDate: dateTimeStillAvailable = False dateObj = self.getDateObj(obj,appointmentDate) timeObj = self.getTimeObj(dateObj,appointmentTime).consultation_24hour_start_time methodObj = obj.consultation_method_set.get(consultation_method_name=method) consultationObj = self.get_object() price = str(self.getPrice(method,duration,obj)) firstPrice = price if promoCode: if self.isPromoCodeValid(obj,promoCode): promoObj = self.getPromoObj(obj,promoCode) percent = promoObj.promo_code_percent price = self.calculatePromoCode(price,percent) stripePrice = price else: stripePrice = price else: stripePrice = price description = consultationObj.consultation_title+" "+"Consultation" if self.isDateTimeAvailable(obj,appointmentDate,appointmentTime): if self.doesDateTimeMeetTimeCondition(dateObj,timeObj,practitionerDateTime): dateTimeStillAvailable = True else: dateTimeStillAvailable = False else: dateTimeStillAvailable = False respond = {} #Professional_Purchased_Consultation, Consultation_Order_Detail saveConsultation = Professional_Purchased_Consultation( user=request.user, professional=obj, consultation=self.get_object(), consultation_day=dateObj, consultation_time=self.getTimeObj(dateObj,appointmentTime), consultation_method=methodObj ) if dateTimeStillAvailable: try: try: saveConsultation.save() except IntegrityError: respond['payment'] = "gone" return self.render_to_json_response(respond) customer = stripe.Customer.create( card=token, email=request.user.username, description="GoNaturalistic Customer" ) charge = stripe.Charge.create( amount=int(Decimal(stripePrice)*100), currency="usd", description=description, customer=customer.id ) #Consultation_Order_Detail saveConsultation.consultation_time_status = 's' saveConsultation.stripe_customer = customer.id saveConsultation.stripe_token = token saveConsultation.stripe_charge = charge.id saveConsultation.payment = Decimal(stripePrice) saveConsultation.save() if not promoCode: percent = 0 consultation_order_detail = Consultation_Order_Detail( professional_purchased_consultation = saveConsultation, user_name = request.user.username, professional_name = obj.professional_full_name, consultation_name = self.get_object().consultation_title, consultation_method_name = method, consultation_date = dateObj.available_day.strftime("%A %b %d, %Y"), consultation_time_period = appointmentTime, consultation_time_zone = practitionerTimeZone, consultation_duration = duration, full_price = str(firstPrice), promo_code_discount = str(percent), final_price = str(stripePrice) ) consultation_order_detail.save() user_consultation_purchase = User_Consultation_Purchase( user = request.user, professional_purchased_consultation = saveConsultation, consultation_order_detail = consultation_order_detail ) user_consultation_purchase.save() #Send Emails########################## serverTZ = pytz.timezone(settings.TIME_ZONE) serverToday = serverTZ.localize(datetime.datetime.now()) emailObj = Activity.objects.all()[0] to = request.user.username from_email = emailObj.consultation_purchase_email_from c = { 'username': request.user.first_name.title(), 'user_email': request.user.username, 'professional_email': obj.professional_email, 'user_phone': request.user.user_account_set.all()[0].user_phone, 'from_email': from_email, 'consultation': self.get_object().consultation_title, 'professional': obj.professional_full_name, 'method': self.getMethodName(method), 'duration': self.getDurationName(duration), 'date': dateObj.available_day.strftime("%A %b %d, %Y"), 'time': appointmentTime, 'timezone': practitionerTimeZone, 'link': reverse('users:consultations', args=[request.user.pk]), 'initial_price': str(firstPrice), 'promo_code_percent': str(percent), 'final_price': str(stripePrice), 'purchase_date': serverToday } html = emailObj.consultation_purchase_email_message_to_user #msg = loader.render_to_string(html, c) t = Template(html) con = Context(c) message = t.render(con) subject = emailObj.consultation_purchase_email_subject msg = EmailMessage(subject,message,from_email,[to]) msg.content_subtype = "html" msg.send() #send Email from Professional########## professional_html = obj.professional_consultation_purchase_email_message professional_subject = obj.professional_consultation_purchase_email_subject professional_from_email = obj.professional_consultation_purchase_email_from c2 = { 'username': request.user.first_name.title(), 'methodList': obj.consultation_method_set.filter(consultation_method_status="y") } t2 = Template(professional_html) con2 = Context(c2) professional_message = t2.render(con2) msg2 = EmailMessage(professional_subject,professional_message,professional_from_email,[to]) msg2.content_subtype = "html" msg2.send() ############################## #Send Email to Professional####### html3 = emailObj.consultation_purchase_email_message_to_professional t3 = Template(html3) con3 = Context(c) message3 = t3.render(con3) professional_email = obj.professional_email professional_gn_email = obj.professional_gn_email msg3 = EmailMessage(subject,message3,from_email,[professional_email,professional_gn_email]) msg3.content_subtype = "html" msg3.send() #Send Activity Email to GoNaturalistic activity_subject = "GoNaturalistic Consultation Purchase" activity_to = emailObj.activity_email html4 = emailObj.activity_purchase t4 = Template(html4) con4 = Context(c) activity_message = t4.render(con4) msg4 = EmailMessage(activity_subject,activity_message,from_email,[activity_to]) msg4.content_subtype = "html" msg4.send() ############################# respond['payment'] = "ok" return self.render_to_json_response(respond) except stripe.error.CardError, e: saveConsultation.delete() body = e.json_body err = body['error'] stripeError = Stripe_Error(user=request.user,json_data=body,status=e.http_status,type=err.get('type',""),code=err.get('code',""),param=err.get('param',""),message=err.get('message',"")) stripeError.save() respond['payment'] = "fail" return self.render_to_json_response(respond) except stripe.error.InvalidRequestError, e: # Invalid parameters were supplied to Stripe's API saveConsultation.delete() body = e.json_body err = body['error'] stripeError = Stripe_Error(user=request.user,json_data=body,status=e.http_status,type=err.get('type',""),code=err.get('code',""),param=err.get('param',""),message=err.get('message',"")) stripeError.save() respond['payment'] = "wrong" return self.render_to_json_response(respond) except stripe.error.AuthenticationError, e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) saveConsultation.delete() body = e.json_body err = body['error'] stripeError = Stripe_Error(user=request.user,json_data=body,status=e.http_status,type=err.get('type',""),code=err.get('code',""),param=err.get('param',""),message=err.get('message',"")) stripeError.save() respond['payment'] = "wrong" return self.render_to_json_response(respond) except stripe.error.APIConnectionError, e: # Network communication with Stripe failed saveConsultation.delete() body = e.json_body err = body['error'] stripeError = Stripe_Error(user=request.user,json_data=body,status=e.http_status,type=err.get('type',""),code=err.get('code',""),param=err.get('param',""),message=err.get('message',"")) stripeError.save() respond['payment'] = "wrong" return self.render_to_json_response(respond)
def saveStripeError(self,user,e): body = e.json_body err = body['error'] stripeError = Stripe_Error(user=user,json_data=body,status=e.http_status,type=err.get('type',""),code=err.get('code',""),param=err.get('param',""),message=err.get('message',"")) stripeError.save()