def update(self, data): self.status = data['status'] self.extra = json.dumps(data) self.save() order = self.order if order.status < 40 and data['status'] in ['PA', 'CO']: logging.info("confirmed %s" % order.id) shop = PaymentAPI() shop.confirm_payment(order=order, amount=data['amount'], transaction_id=data['uuid'], payment_method=BACKEND_NAME)
def update(self, data): self.status = data['status'] self.extra = json.dumps(data) self.save() order = self.order if order.status < 40 and data['status'] in ['PA', 'CO']: logging.info("confirmed %s" % order.id) shop = PaymentAPI() shop.confirm_payment(order=order, amount=data['amount'], transaction_id=data['uuid'], payment_method=BACKEND_NAME)
def test_get_order_returns_sensible_nulls(self): class MockRequest(): user = self.user be = ValidMockPaymentBackend(shop=PaymentAPI()) order = be.shop.get_order(MockRequest()) self.assertEqual(order, None)
def test_get_context_gives_correct_order(self): # first send the order through the payment API PaymentAPI().confirm_payment(self.order, 10, 'None', 'magic payment') # then call the view view = ThankYouView(request=self.request) self.assertNotEqual(view, None) res = view.get_context_data() self.assertNotEqual(res, None) # refresh self.order from db (it was saved in the view) self.order = Order.objects.get(pk=self.order.pk) self.assertEqual(self.order.status, Order.COMPLETED) ctx_order = res.get('order', None) self.assertNotEqual(ctx_order, None) self.assertEqual(ctx_order, self.order)
def test01_backend_returns_urls(self): be = PayOnDeliveryBackend(shop=PaymentAPI()) urls = be.get_urls() self.assertNotEqual(urls, None) self.assertEqual(len(urls), 1)
class BackendsPool(object): """ A pool for backends. It handles loading backend modules (both shipping and payment backends), and keeping a cached copy of the classes in-memory (so that the backends aren't loaded from file every time one requests them) """ SHIPPING = 'SHOP_SHIPPING_BACKENDS' PAYMENT = 'SHOP_PAYMENT_BACKENDS' PAYMENT_SHOP_INTERFACE = PaymentAPI() SHIPPING_SHOP_INTERFACE = ShippingAPI() def __init__(self, use_cache=True): """ The use_cache parameter is mostly used for testing, since setting it to false will trigger reloading from disk """ self._payment_backends_list = [] self._shippment_backends_list = [] self.use_cache = use_cache def get_payment_backends_list(self): """ Returns the list of payment backends, as instances, from the list of backends defined in settings.SHOP_PAYMENT_BACKENDS """ if self._payment_backends_list and self.use_cache: return self._payment_backends_list else: self._payment_backends_list = self._load_backends_list( self.PAYMENT, self.PAYMENT_SHOP_INTERFACE) return self._payment_backends_list def get_shipping_backends_list(self): """ Returns the list of shipping backends, as instances, from the list of backends defined in settings.SHOP_SHIPPING_BACKENDS """ if self._shippment_backends_list and self.use_cache: return self._shippment_backends_list else: self._shippment_backends_list = self._load_backends_list( self.SHIPPING, self.SHIPPING_SHOP_INTERFACE) return self._shippment_backends_list def _check_backend_for_validity(self, backend_instance): """ This enforces having a valid name and url namespace defined. Backends, both shipping and payment are namespaced in respectively /pay/ and /ship/ URL spaces, so as to avoid name clashes. "Namespaces are one honking great idea -- let's do more of those!" """ backend_name = getattr(backend_instance, 'backend_name', "") if not backend_name: d_tuple = (str(backend_instance), str(type(backend_instance))) raise NotImplementedError( 'One of your backends ("%s" of type "%s") lacks a name, please define one.' % d_tuple) url_namespace = getattr(backend_instance, 'url_namespace', "") if not url_namespace: raise NotImplementedError( 'Please set a namespace for backend "%s"' % backend_instance.backend_name) def _load_backends_list(self, setting_name, shop_object): """ This actually loads the backends from disk""" result = [] if not getattr(settings, setting_name, None): return result for backend_path in getattr(settings, setting_name, None): # The load_class function takes care of the classloading. It returns # a CLASS, not an INSTANCE! mod_class = load_class(backend_path, setting_name) # Seems like it is a real, valid class - let's instanciate it! # This is where the backends receive their self.shop reference! mod_instance = mod_class(shop=shop_object) self._check_backend_for_validity(mod_instance) # The backend seems valid (nothing raised), let's add it to the # return list. result.append(mod_instance) return result
def setUp(self): self.user = User.objects.create(username="******", email="*****@*****.**", first_name="Test", last_name="User") self.shop = PaymentAPI() self.backend = GenericPayerBackend(shop=self.shop) class Mock(object): pass self.request = Mock() setattr(self.request, 'user', self.user) self.user.save() self.country = Country.objects.create(name='CH') self.address = AddressModel() self.address.client = self.client self.address.address = 'address' self.address.address2 = 'address2' self.address.zip_code = '1234' self.address.state = 'ZH' self.address.country = self.country self.address.is_billing = False self.address.is_shipping = True self.address.save() self.address2 = AddressModel() self.address2.client = self.client self.address2.address = '2address' self.address2.address2 = '2address2' self.address2.zip_code = '21234' self.address2.state = '2ZH' self.address2.country = self.country self.address2.is_billing = True self.address2.is_shipping = False self.address2.save() # The order fixture self.order = Order() self.order.user = self.user self.order.order_subtotal = Decimal('100') # One item worth 100 self.order.order_total = Decimal('120') # plus a test field worth 10 self.order.status = Order.PROCESSING ship_address = self.address bill_address = self.address2 self.order.set_shipping_address(ship_address) self.order.set_billing_address(bill_address) self.order.save() # Orderitems self.orderitem = OrderItem() self.orderitem.order = self.order self.orderitem.product_name = 'Test item' self.orderitem.unit_price = Decimal("100") self.orderitem.quantity = 1 self.orderitem.line_subtotal = Decimal('100') self.orderitem.line_total = Decimal('110') self.orderitem.save() oi = OrderExtraInfo() oi.order = self.order oi.text = "buffalo " * 64 oi.save() eoif = ExtraOrderItemPriceField() eoif.order_item = self.orderitem eoif.label = 'Fake extra field' eoif.value = Decimal("10") eoif.save() eof = ExtraOrderPriceField() eof.order = self.order eof.label = "Fake Taxes" eof.value = Decimal("10") eof.save()