2👍
✅
your views.py will be better if you have form_valid
and form_invalid
methods and also you have to edit your success_url
similar to this.
class BraintreePaymentProcessFormView(FormView):
template_name = 'startupconfort/cart.html'
success_url = '/'
form_class = BraintreeSaleForm
http_method_names = ['post']
success_url = reverse_lazy('my_shopping_cart')
def get_context_data(self, **kwargs):
# import ipdb; ipdb.set_trace()
context = super().get_context_data(**kwargs)
context['client_token'] = get_braintree_client_token()
return context
def form_valid(self, form):
# import ipdb; ipdb.set_trace()
user = self.request.user
nonce = form.cleaned_data['payment_method_nonce']
result = braintree.Transaction.sale({
"amount": get_total_price_of_the_shipping_cart(user),
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
reverse_lazy('my_shopping_cart')
# import ipdb; ipdb.set_trace()
if result.is_success or result.transaction:
print(result.transaction)
messages.success(self.request, 'Payment proceed successfully')
# Add Total into db with User Name and Product + Quantity
#send email
user = self.request.user
billing_email = user.shipping_address.billing_email
customer_name = user.shipping_address.customer_name
#total values
#msg
# Email with mailjet..
msg = EmailMultiAlternatives(
subject="Please activate your account",
body="Click to activate your account: http://localhost:8000/",
from_email="Example <admin@example.com>",
to=[
"New User <user1@example.com>"
],
reply_to=["Helpdesk <support@example.com>"]
)
# Send it:
# msg.send()
#Clear Cart
CartItem.objects.filter(customer=user).all().delete()
# return super().form_valid(form)
import ipdb; ipdb.set_trace()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self,form):
# Add action to invalid form phase
messages.success(self.request, 'An error occured while processing the payment')
return self.render_to_response(self.get_context_data(form=form))
Source:stackexchange.com