def test_make_payment_form_invalid(self): form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'ccv': 'test_user1', 'expiry_month': self.month, 'expiry_year': self.year, }) self.assertFalse(form.is_valid())
def test_payment_form_with_missing_field(self): form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'expiry_month': '10', 'expiry_year': '2025', }) self.assertFalse(form.is_valid())
def test_payment_form_fails_invalid_data(self): form = MakePaymentForm({ "credit_card_number": "", "cvv": "876", "expiry_month": "07", "expiry_year": "2021", "stripe_id": "Test", }) self.assertFalse(form.is_valid())
def test_payment_form_passes_valid_data(self): form = MakePaymentForm({ "credit_card_number": "4242424242424242", "cvv": "876", "expiry_month": "7", "expiry_year": "2021", "stripe_id": "Test", }) self.assertTrue(form.is_valid())
def test_can_make_payment(self): form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'cvv': '235', 'expiry_month': 12, 'expiry_year': 2022, 'stripe_id': 'stripe_id_enald3kLDD' }) self.assertTrue(form.is_valid())
def test_make_payment_form_success(self): form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'ccv': 'test_user1', 'expiry_month': self.month, 'expiry_year': self.year, 'stripe_id': STRIPE_SECRET, }) self.assertTrue(form.is_valid())
def test_make_payment_form_is_valid(self): form = MakePaymentForm({ "credit_card_number": 4242424242424242, "cvv": 111, "expiry_month": 11, "expiry_year": 2024, "stripe_id": 1 }) self.assertTrue(form.is_valid())
def test_payment_form_not_valid(self): #test if the form is not valid form = MakePaymentForm( data={ # expirY_month and 'expiry_year' already have its values. 'credit_card_number': '', }) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 1)
def test_cannot_payment_be_made_required_values(self): form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'cvv': 222, 'expiry_month': 5, 'expiry_year': 2021, }) self.assertFalse(form.is_valid())
def test_valid_payment_form(self): form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'cvv': '123', 'expiry_month': '6', 'expiry_year': '2025', 'stripe_id': 'xyz555', }) self.assertTrue(form.is_valid())
def test_payment_form(self): #test if the form is valid form = MakePaymentForm( data={ 'credit_card_number': '4242424242424242', 'cvv': '123', 'expiry_month': 3, 'expiry_year': 2022, 'stripe_id': 'tok_visa', }) self.assertTrue(form.is_valid())
def test_can_payment_be_made_required_values(self): form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'cvv': 222, 'expiry_month': 5, 'expiry_year': 2021, 'stripe_id': 'test' }) self.assertTrue(form.is_valid())
def checkout(request): if request.method == "POST": order_form = OrderForm(request.POST) payment_form = MakePaymentForm(request.POST) if order_form.is_valid() and payment_form.is_valid(): order = order_form.save(commit=False) order.date = timezone.now() order.save() cart = request.session.get('cart', {}) total = 0 for id, quantity in cart.items(): product = get_object_or_404(Product, pk=id) total += quantity * product.price order_line_item = OrderLineItem(order=order, product=product, quantity=quantity) order_line_item.save() try: customer = stripe.Charge.create( amount=int(total * 1000), currency="EUR", description=request.user.email, card=payment_form.cleaned_data['stripe_id'], ) except stripe.error.CardError: messages.error(request, "Your card was declined!") if customer.paid: messages.error(request, "You have successfuly paid") request.session['cart'] = {} return redirect(reverse('products')) else: messages.error(request, "Unable to take payment") else: print(payment_form.errors) messages.error(request, "We are unable to take a payment with that card!") else: payment_form = MakePaymentForm() order_form = OrderForm() return render( request, "checkout.html", { 'order_form': order_form, 'payment_form': payment_form, 'publishable': settings.STRIPE_PUBLISHABLE })
def go_to_checkout(request): if request.method == "POST": #pdb.set_trace() order_form = OrderForm(request.POST) payment_form = MakePaymentForm(request.POST) if order_form.is_valid(): # Save The Order order = order_form.save(commit=False) order.date = timezone.now() order.save() # Save the Order Line Items userid = request.user.id cart = request.session.get('cart', {}) save_order_items(userid, order, cart) # Charge the Card items_and_total = get_cart_items_and_total(cart) total = items_and_total['cart_total'] #stripe_token=payment_form.cleaned_data['stripe_id'] try: #customer = charge_card(stripe_token, total) complete = True except stripe.error.CardError: messages.error(request, "Your card was declined!") if complete: messages.success(request, "Thanks, will deliver ypu soon.") # Send Email # send_confirmation_email(request.user.email, request.user, items_and_total) #Clear the Cart del request.session['cart'] return redirect("home") else: order_form = OrderForm() #payment_form = MakePaymentForm() context = { 'order_form': order_form, 'publishable': settings.STRIPE_PUBLISHABLE } cart = request.session.get('cart', {}) cart_items_and_total = get_cart_items_and_total(cart) context.update(cart_items_and_total) return render(request, "checkout/checkout.html", context)
def pay_deposit(request): """ Page to pay the deposit. """ user = request.user commission = get_object_or_404(Commission, user=user) price = commission.price deposit = float(commission.price) * 0.15 """ Add the code from all the checkout page. This should be its own checkout page and then up date deposit.paid to true """ if request.method=='POST': payment_form = MakePaymentForm(request.POST) if payment_form.is_valid(): try: customer = stripe.Charge.create( amount = int(deposit * 100), currency = "EUR", description = request.user.email, source = payment_form.cleaned_data['stripe_id'], ) except stripe.error.CardError: messages.error(request, "Your card was declined!") if customer.paid: messages.success('You have paid! Thank you') cart = {} return redirect(reverse('pay_deposit')) else: messages.error(request, "We were unable to take payment") else: messages.error(request, payment_form.errors) else: payment_form = MakePaymentForm() return render(request, 'pay_deposit.html', {'pay_deposit':pay_deposit, 'payment_form':payment_form, 'publishable':settings.STRIPE_PUBLISHABLE })
def test_make_payment_form(self): form = MakePaymentForm({'credit_card_number': '', 'cvv': ''}) self.assertFalse(form.is_valid()) form = MakePaymentForm({ 'credit_card_number': '4242424242424242', 'cvv': '123', 'expiry_month': '2', 'expiry_year': '2023', 'stripe_id': 'tok_visa' }) self.assertTrue(form.is_valid())
def checkout(request): ''' function to load items currently in the cart and load the payment forms if there are donations in the cart. Stripe will check payments and process them accordingly ''' # checking if anything is in the cart cartcontent = request.session.get('cart', {}) # if nothing is in the cart redirect to donation page if not cartcontent: messages.warning(request, f'Your Cart has no donations!! ') return redirect(reverse('donations')) user = request.user # if user fills in forms on checkout if request.method == "POST": order_form = OrderForm(request.POST) payment_form = MakePaymentForm(request.POST) # check if forms are valid if order_form.is_valid() and payment_form.is_valid(): # add current date and user to form and save order = order_form.save(commit=False) order.date = timezone.now() order.user = user order.save() # get cart content once checkout details are submitted correctly cart = request.session.get('cart', {}) total = 0 # finding cart donation types and calculating overall price for id, quantity in cart.items(): donation = get_object_or_404(Donation, pk=id) total += quantity * donation.price # adding order to db order_line_item = OrderLineItem(order=order, donation=donation, quantity=quantity) order_line_item.save() try: # processing stripe payment customer = stripe.Charge.create( amount=int(total * 100), currency="EUR", description=request.user.email, card=payment_form.cleaned_data['stripe_id'], ) # display card errors if any except stripe.error.CardError: messages.error(request, "Your card was declined!") # if payment is successful empty cart content and inform user if customer.paid: messages.success(request, f'You have successfully paid') request.session['cart'] = {} return redirect(reverse('donations')) else: messages.error(request, "Unable to take payment") else: print(payment_form.errors) messages.error(request, "We were unable to take a payment with that card!") else: # loading payment forms payment_form = MakePaymentForm() order_form = OrderForm() return render( request, "checkout/checkout.html", { 'order_form': order_form, 'payment_form': payment_form, 'publishable': settings.STRIPE_PUBLISHABLE })
def setUp(self): self.form = MakePaymentForm()