def pay_for_a_plan(request,plan_name): context = {} #check if the plan is valid. valid_plans = ["L1","L2","L5","U1","U3","U6"] if plan_name not in valid_plans : raise Http404("Data transfer selected is not valid.") # get the plan the user has selected all_plans = Data_Transfer_Plan.objects.all() plan = utility_functions.get_plan_by_name(all_plans,plan_name) # get the user's coupons user_profile = UserProfile.objects.get( user = request.user ) user_existing_coupons = Coupon.objects.filter( user_profile = user_profile ) # create the temp plan for the plan selected by user selected_plan = utility_functions.create_temp_plan(plan, user_existing_coupons) context['selected_plan'] = selected_plan # does the user have any coupons? if not user_existing_coupons: context['coupon_available'] = False else: # if the customer has some coupons context['coupon_available'] = True context['existing_coupons'] = user_existing_coupons # get the best coupon best_coupon = utility_functions.get_best_coupon(user_existing_coupons) return render(request,'payment/pay_for_a_plan.html',context)
def pay_for_a_plan_success(request,pending_purchase,context,user_existing_coupons,selected_plan): # add the purchase to the database new_purchase = Purchase() new_purchase.user = pending_purchase.user new_purchase.data_transfer_plan = pending_purchase.data_transfer_plan if user_existing_coupons: new_purchase.amount_paid = selected_plan.discounted_price else: new_purchase.amount_paid = selected_plan.original_price new_purchase.remaining_allowance_frequency = pending_purchase.data_transfer_plan.freq new_purchase.save() # save follow_up number using hash follow_up_number = generate_md5_hash(str(new_purchase.id)) new_purchase.follow_up_number = follow_up_number new_purchase.save() context['follow_up_number'] = follow_up_number # if necessary, remove user's best coupon if user_existing_coupons: best_coupon = utility_functions.get_best_coupon(user_existing_coupons) best_coupon.delete() # send an email plaintext = loader.get_template('payment/pay_for_a_plan_complete_email.txt') htmly = loader.get_template('payment/pay_for_a_plan_complete_email.html') subject = loader.get_template('payment/pay_for_a_plan_complete_email_subject.html') subject_content = subject.render(context).replace('\n',' ') text_content = plaintext.render(context) html_content = htmly.render(context) from_email = '*****@*****.**' recipient_list = [new_purchase.user.email] msg = EmailMultiAlternatives(subject_content, text_content, from_email, recipient_list) msg.attach_alternative(html_content, "text/html") msg.send() # return response to the user. return render(request,'payment/successful_payment.html',context)