def test_product_acquired(self): # Include order and contract info offering1 = self._get_offering_mock() offering2 = self._get_offering_mock() offering3 = self._get_offering_mock(bundle_asset=True) decorators.Offering = MagicMock() decorators.Offering.objects.get.side_effect = [ offering1, offering1, offering2, offering2, offering3, offering3 ] decorators.Resource = MagicMock() asset1 = MagicMock(resource_type='asset3') asset2 = MagicMock(resource_type='asset4') decorators.Resource.objects.get.side_effect = [asset1, asset2] self._contract.offering.bundled_offerings = ['1', '2', '3'] decorators.on_product_acquired(self._order, self._contract) # Check calls self.assertEquals( [call('asset'), call('asset'), call('asset3'), call('asset4')], decorators.load_plugin_module.call_args_list) self.assertEquals([ call(offering1.asset, self._contract, self._order), call(offering2.asset, self._contract, self._order), call(asset1, self._contract, self._order), call(asset2, self._contract, self._order) ], self._module.on_product_acquisition.call_args_list)
def create(self, request): try: event = json.loads(request.body) except: return build_response( request, 400, 'The provided data is not a valid JSON object') if event['eventType'] != 'ProductCreationNotification': return build_response(request, 200, 'OK') product = event['event']['product'] # Extract order id order_id = product['name'].split('=')[1] # Get order order = Order.objects.get(order_id=order_id) contract = None # Search contract for cont in order.contracts: if product['productOffering']['id'] == cont.offering.off_id: contract = cont if contract is None: return build_response( request, 404, 'There is not a contract for the specified product') # Save contract id contract.product_id = product['id'] order.save() # Activate asset try: on_product_acquired(order, contract) except: return build_response(request, 400, 'The asset has failed to be activated') # Change product state to active inventory_client = InventoryClient() inventory_client.activate_product(product['id']) # Create the initial charge in the billing API if len(contract.charges) == 1: billing_client = BillingClient() valid_to = None # If the initial charge was a subscription is needed to determine the expiration date if 'subscription' in contract.pricing_model: valid_to = contract.pricing_model['subscription'][0][ 'renovation_date'] billing_client.create_charge(contract.charges[0], contract.product_id, start_date=None, end_date=valid_to) return build_response(request, 200, 'OK')
def _set_renovation_states(self, transactions, raw_order, order): inventory_client = InventoryClient() for transaction in transactions: try: contract = order.get_item_contract(transaction['item']) inventory_client.activate_product(contract.product_id) # Activate the product on_product_acquired(order, contract) except: pass
def process_product_payment(self, request, task, order, contract): # Refresh accounting information on_usage_refreshed(order, contract) # Build charging engine charging_engine = ChargingEngine(order) redirect_url = None try: redirect_url = charging_engine.resolve_charging( type_=task['priceType'].lower(), related_contracts=[contract]) except ValueError as e: return None, build_response(request, 400, str(e)) except OrderingError as e: # The error might be raised because renewing a suspended product not expired if str( e ) == 'OrderingError: There is not recurring payments to renovate' and contract.suspended: try: on_product_acquired(order, contract) # Change product state to active contract.suspended = False order.save() inventory_client = InventoryClient() inventory_client.activate_product(contract.product_id) except: return None, build_response( request, 400, 'The asset has failed to be activated') else: return None, build_response(request, 422, str(e)) except: return None, build_response( request, 500, 'An unexpected event prevented your payment to be created') return redirect_url, None