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')
Ejemplo n.º 2
0
    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)
            except:
                pass
    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