def send_cancel_mail(self): subject = u"Reservation Cancelled " + unicode(self.date) from_email = "*****@*****.**" to = [self.user.email] template = "email/cancelation.html" data = {"reservation": self} send_html_email(subject, from_email, to, template, data)
def post_save_student(sender, instance, created, **kwargs): if created: message = get_template('email.html').render({'student': instance}) send_html_email("New Student", message, ["*****@*****.**", "*****@*****.**"]) message = get_template('email_welcome.html').render({'user': instance}) send_html_email("Welcome", message, [instance.email], "*****@*****.**")
def send_payment_mail(self): subject = u"Payment Success " + unicode(self.date) from_email = "*****@*****.**" to = [self.user.email] template = "email/payment_confirmation.html" payment_info = json.loads(self.payment_confirmation) data = {"reservation": self, "card": payment_info["source"]["brand"], "last4": payment_info["source"]["last4"]} send_html_email(subject, from_email, to, template, data)
def post_save_orderlineitem(sender, instance, created, **kwargs): if created: if instance.product.name in OCEAN_COURAGE_PRODUCTS: # send out an email when the order is made for the first time # also send it out when they ordered it earlier, but it expired and they are ordering it again send_message = True renewal = False # retrieve all order line items for this student # filter out the instance just saved lis = [ li for li in OrderLineItem.objects.filter( order__student=instance.order.student). with_ocean_courage_subscription_information() if li.pk != instance.pk ] if lis: # if the latest expiration is before today, send out a message since it is a renewal latest_expiration = max([li.expiration_date for li in lis]) if timezone.now().date() > latest_expiration: renewal = True else: send_message = False if send_message: message = get_template('email_oceancourage.html').render({ 'order': instance.order, 'renewal': renewal }) email_list = list( Group.objects.get( name="oceancouragegroup").user_set.all().values_list( 'email', flat=True)) email_list += list(get_user_model().objects.filter( is_superuser=True).values_list('email', flat=True)) send_html_email("New Ocean Courage Student", message, email_list, "*****@*****.**")
def process_payment(request): form = OrderForm(request.POST or None, user=request.user) if request.POST: # Set your secret key: remember to change this to your live secret key in production # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = settings.STRIPE_SECRET_API_KEY # Get the payment token ID submitted by the form: token = request.POST.get('stripeToken') try: if token and form.is_valid(): grand_total = form.grand_total if grand_total > 0: stripe.Charge.create( amount=int(grand_total * 100), # stripe amounts are in pennies currency='usd', description='Ocean Ink', source=token, metadata={'student_id': request.user.pk}, ) try: # save_invoice(order) pass except: pass # send email message after everything is saved order = form.save() message = render_to_string('email_receipt.html', {'order': order}) send_html_email( "New Payment", message, ["*****@*****.**", "*****@*****.**"] ) send_html_email( "Thank you for your payment.", message, [order.student.email], "*****@*****.**" ) products = ', '.join(order.orderlineitem_set.values_list('product__name', flat=True)) # send_sms.send(f"{order.student} bought ${order.grand_total}: {products}") return redirect(reverse('profile:receipt', kwargs={'pk': order.pk})) except stripe.error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught form.card_errors = e.user_message except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly form.card_errors = "Server error: Stripe rate limit reached. Please try again." except stripe.error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API print("User: %s" % request.user) print("Invalid parameters sent to Stripe. %s" % e) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) print("User: %s" % request.user) print("Invalid Stripe API key.") except stripe.error.APIConnectionError as e: print("User: %s" % request.user) print("Network communication with Stripe failed.") except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email print("User: %s" % request.user) print("Generic Stripe error: %s" % e) return render(request, template_name="pay.html", context={ 'form': form, 'STRIPE_API_KEY': settings.STRIPE_PUBLISHABLE_API_KEY} )