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 result_payline(request,pending_purchase_hashcode): trans_id = request.POST['trans_id'] id_get = request.POST['id_get'] final_result = get_result(PAYLINE_DOTIR_API_FINAL, trans_id, id_get) context = {} # retrieve the pending purchase pending_purchase = PendingPurchase.objects.get(hashcode = pending_purchase_hashcode) # get the user's coupons user_profile = UserProfile.objects.get( user = pending_purchase.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(pending_purchase.data_transfer_plan, user_existing_coupons) context['selected_plan'] = selected_plan response = None if final_result is None: response = pay_for_a_plan_failure(request,context) else: if int(final_result) == 1: response = pay_for_a_plan_success(request,pending_purchase,context,user_existing_coupons,selected_plan) else: response = pay_for_a_plan_failure(request,context) # remove pending purchase pending_purchase.delete() return response
def get_alternative_plans(all_plans, selected_plan, coupons): alt_plans = [] ordered_names = ["L1","L2","L5","U1","U3","U6"] for name in ordered_names: if name != selected_plan.plan_name: alternative_plan = utility_functions.get_plan_by_name(all_plans, name) alternative_temp_plan = utility_functions.create_temp_plan(alternative_plan,coupons) alt_plans.append(alternative_temp_plan) return alt_plans
def initialise_payment_payline(request,plan_name): #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) # create a pending purchase pending_purchase = PendingPurchase() pending_purchase.data_transfer_plan = plan pending_purchase.user = request.user pending_purchase.save() # prepare amount if user_existing_coupons: amount = selected_plan.discounted_price else: amount = selected_plan.original_price # get gateway_url # integrate pending purchase hashcode in redirect url redirect_url = 'http://gooshibegooshi.com/payment/result_payline/'+pending_purchase.hashcode+'/' gateway_url = send_url(amount, redirect_url,SEND_URL_FINAL, PAYLINE_DOTIR_API_FINAL) # redirect to payline.ir return redirect(gateway_url)
def activate_plan(request,plan_name): # check that the model chosen is correct. valid_plans = ["L1","L2","L5","U1","U3","U6"] user_profile = UserProfile.objects.get( user = request.user ) context={} context.update(csrf(request)) context['coupon_code_entered'] = False if plan_name not in valid_plans : raise Http404("Data transfer selected is not valid.") if request.method == "POST": # a new coupon code has been entered. Check if the coupon is valid and enter the code. context['coupon_code_entered'] = True form = AddCouponForm(request.POST) if form.is_valid(): hashcode = form.cleaned_data['hashcode'] # associate the code to the customer profile try: coupon = Coupon.objects.get( hashcode = hashcode ) coupon.user_profile = user_profile coupon.save() context['wrong_coupon_entered'] = False except Coupon.DoesNotExist: # the coupon entered does not exist in the database context['wrong_coupon_entered'] = True else: # form is not valid context['wrong_coupon_entered'] = True # request.method is not POST # do the pricing given the customer's coupons all_plans = Data_Transfer_Plan.objects.all() plan = utility_functions.get_plan_by_name(all_plans,plan_name) # check if the user has any discount coupon. user_existing_coupons = Coupon.objects.filter( user_profile = user_profile ) selected_plan = utility_functions.create_temp_plan(plan, user_existing_coupons) alternative_plans = get_alternative_plans(all_plans,selected_plan, user_existing_coupons) # set up the context context['selected_plan'] = selected_plan context['alt_plan_0'] = alternative_plans[0] context['alt_plan_1'] = alternative_plans[1] context['alt_plan_2'] = alternative_plans[2] context['alt_plan_3'] = alternative_plans[3] context['alt_plan_4'] = alternative_plans[4] # setting the context, depending on whether the customer has any coupons available 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 add_coupon_form = AddCouponForm() context['add_coupon_form'] = add_coupon_form return render(request,'bitasync_site/payment_with_coupons.html',context)