def create_order_with_token(request): """ Create an order using an existing transaction ID. This is useful for capturing the payment outside of longclaw - e.g. using paypals' express checkout or similar """ # Get the request data try: address = request.data['address'] shipping_option = request.data.get('shipping_option', None) email = request.data['email'] transaction_id = request.data['transaction_id'] except KeyError: return Response( data={"message": "Missing parameters from request data"}, status=status.HTTP_400_BAD_REQUEST) # Create the order order = create_order( email, request, addresses=address, shipping_option=shipping_option, ) order.payment_date = timezone.now() order.transaction_id = transaction_id order.save() # Once the order has been successfully taken, we can empty the basket destroy_basket(request) return Response(data={"order_id": order.id}, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) checkout_form = context['checkout_form'] shipping_form = context['shipping_form'] all_ok = checkout_form.is_valid() and shipping_form.is_valid() if all_ok: email = checkout_form.cleaned_data['email'] shipping_option = checkout_form.cleaned_data.get( 'shipping_option', None) shipping_address = shipping_form.save() if checkout_form.cleaned_data['different_billing_address']: billing_form = context['billing_form'] all_ok = billing_form.is_valid() if all_ok: billing_address = billing_form.save() else: billing_address = shipping_address if all_ok: order = create_order(email, request, shipping_address=shipping_address, billing_address=billing_address, shipping_option=shipping_option, capture_payment=True) return HttpResponseRedirect( reverse('longclaw_checkout_success', kwargs={'pk': order.id})) return super(CheckoutView, self).render_to_response(context)
def test_create_order(self): BasketItemFactory(basket_id=self.basket_id), BasketItemFactory(basket_id=self.basket_id) order = create_order(self.email, self.request, self.addresses, capture_payment=True) self.assertIsNotNone(order) self.assertIsNotNone(order.payment_date) self.assertEqual(self.email, order.email) self.assertEqual(order.items.count(), 2)
def post(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) checkout_form = context['checkout_form'] shipping_form = context['shipping_form'] discount = context['discount'] all_ok = checkout_form.is_valid() and shipping_form.is_valid() if all_ok: email = checkout_form.cleaned_data['email'] shipping_option = checkout_form.cleaned_data.get( 'shipping_option', None) shipping_address = shipping_form.save() if checkout_form.cleaned_data['different_billing_address']: billing_form = context['billing_form'] all_ok = billing_form.is_valid() if all_ok: billing_address = billing_form.save() else: billing_address = shipping_address if all_ok: try: order = create_order( email, request, shipping_address=shipping_address, billing_address=billing_address, shipping_option=shipping_option, delivery_instructions=delivery_instructions, discount=discount, capture_payment=True) except ValueError: # Something went wrong, no items in basket? return super(CheckoutView, self).render_to_response(context) else: # Check for if the payment went through if order.status == order.SUBMITTED: return HttpResponseRedirect( reverse('longclaw_checkout_success', kwargs={'pk': order.id})) else: context['checkout_form'] = checkout_form context['shipping_form'] = shipping_form context['discount'] = discount if order.status == order.FAILURE: context['payment_error'] = order.status_note return super(CheckoutView, self).render_to_response(context) return super(CheckoutView, self).render_to_response(context)
def test_create_order_with_basket_shipping_option(self): amount = 11 rate = ShippingRate.objects.create( name=force_text(uuid.uuid4()), rate=amount, carrier=force_text(uuid.uuid4()), description=force_text(uuid.uuid4()), basket_id=self.basket_id, ) order = create_order( self.email, self.request, shipping_address=self.shipping_address, billing_address=self.billing_address, shipping_option=rate.name, ) self.assertEqual(order.shipping_rate, amount)
def capture_payment(request): """ Capture the payment for a basket and create an order request.data should contain: 'address': Dict with the following fields: shipping_name shipping_address_line1 shipping_address_city shipping_address_zip shipping_address_country billing_name billing_address_line1 billing_address_city billing_address_zip billing_address_country 'email': Email address of the customer 'shipping': The shipping rate (in the sites' currency) """ # get request data address = request.data['address'] email = request.data.get('email', None) shipping_option = request.data.get('shipping_option', None) # Capture the payment order = create_order(email, request, addresses=address, shipping_option=shipping_option, capture_payment=True) response = Response(data={"order_id": order.id}, status=status.HTTP_201_CREATED) return response