Example #1
0
def save_transaction(data):
    resolution = CREDIT_RESOLUTION.PENDING
    if data.pop('credited', False):
        resolution = CREDIT_RESOLUTION.CREDITED
    if data.pop('refunded', False):
        resolution = CREDIT_RESOLUTION.REFUNDED

    prisoner_dob = data.pop('prisoner_dob', None)
    prisoner_number = data.pop('prisoner_number', None)
    single_offender_id = data.pop('single_offender_id', None)
    prisoner_name = data.pop('prisoner_name', None)
    prison = data.pop('prison', None)
    reconciled = data.pop('reconciled', False)
    owner = data.pop('owner', None)
    blocked = data.pop('blocked', False)

    if (data['category'] == TRANSACTION_CATEGORY.CREDIT
            and data['source'] == TRANSACTION_SOURCE.BANK_TRANSFER):
        credit = Credit(amount=data['amount'],
                        prisoner_dob=prisoner_dob,
                        prisoner_number=prisoner_number,
                        single_offender_id=single_offender_id,
                        prisoner_name=prisoner_name,
                        prison=prison,
                        reconciled=reconciled,
                        owner=owner,
                        received_at=data['received_at'],
                        resolution=resolution,
                        blocked=blocked)
        credit.save()
        data['credit'] = credit

    return Transaction.objects.create(**data)
Example #2
0
    def create(self, validated_data):
        transactions = []
        user = self.context['request'].user

        for data in validated_data:
            batch = data.pop('batch', None)
            prisoner_number = data.pop('prisoner_number', None)
            prisoner_dob = data.pop('prisoner_dob', None)
            blocked = data.pop('blocked', False)
            if (data['category'] == TRANSACTION_CATEGORY.CREDIT
                    and data['source'] == TRANSACTION_SOURCE.BANK_TRANSFER):
                new_credit = Credit(amount=data['amount'],
                                    prisoner_number=prisoner_number,
                                    prisoner_dob=prisoner_dob,
                                    received_at=data['received_at'],
                                    blocked=blocked)
                new_credit.save()
                data['credit'] = new_credit

                credit_created.send(sender=Credit,
                                    credit=new_credit,
                                    by_user=user)

            transaction = Transaction.objects.create(**data)
            if batch:
                batch.settlement_transaction = transaction
                batch.save()
            transactions.append(transaction)

        return transactions
def save_transaction(data):
    resolution = CREDIT_RESOLUTION.PENDING
    if data.pop('credited', False):
        resolution = CREDIT_RESOLUTION.CREDITED
    if data.pop('refunded', False):
        resolution = CREDIT_RESOLUTION.REFUNDED

    prisoner_dob = data.pop('prisoner_dob', None)
    prisoner_number = data.pop('prisoner_number', None)
    single_offender_id = data.pop('single_offender_id', None)
    prisoner_name = data.pop('prisoner_name', None)
    prison = data.pop('prison', None)
    reconciled = data.pop('reconciled', False)
    owner = data.pop('owner', None)
    blocked = data.pop('blocked', False)

    if (data['category'] == TRANSACTION_CATEGORY.CREDIT and
            data['source'] == TRANSACTION_SOURCE.BANK_TRANSFER):
        credit = Credit(
            amount=data['amount'],
            prisoner_dob=prisoner_dob,
            prisoner_number=prisoner_number,
            single_offender_id=single_offender_id,
            prisoner_name=prisoner_name,
            prison=prison,
            reconciled=reconciled,
            owner=owner,
            received_at=data['received_at'],
            resolution=resolution,
            blocked=blocked
        )
        credit.save()
        data['credit'] = credit

    return Transaction.objects.create(**data)
    def create(self, validated_data):
        transactions = []
        user = self.context['request'].user

        for data in validated_data:
            batch = data.pop('batch', None)
            prisoner_number = data.pop('prisoner_number', None)
            prisoner_dob = data.pop('prisoner_dob', None)
            blocked = data.pop('blocked', False)
            if (data['category'] == TRANSACTION_CATEGORY.CREDIT and
                    data['source'] == TRANSACTION_SOURCE.BANK_TRANSFER):
                new_credit = Credit(
                    amount=data['amount'],
                    prisoner_number=prisoner_number,
                    prisoner_dob=prisoner_dob,
                    received_at=data['received_at'],
                    blocked=blocked
                )
                new_credit.save()
                data['credit'] = new_credit

                credit_created.send(
                    sender=Credit,
                    credit=new_credit,
                    by_user=user
                )

            transaction = Transaction.objects.create(**data)
            if batch:
                batch.settlement_transaction = transaction
                batch.save()
            transactions.append(transaction)

        return transactions
 def create(self, validated_data):
     new_credit = Credit(
         amount=validated_data['amount'],
         prisoner_number=validated_data.pop('prisoner_number'),
         prisoner_dob=validated_data.pop('prisoner_dob'),
         resolution=CREDIT_RESOLUTION.INITIAL)
     new_credit.save()
     validated_data['credit'] = new_credit
     return super().create(validated_data)
Example #6
0
    def post(self, customer_id, amount):
        """
        issue a credit to a customer

        :param customer_id: customer to add credit to
        :param amount: amount of credit to be given
        """
        store = Store.objects.filter(app_id=request.headers.get('APP-ID'),
                                     deleted_at=None).first()

        customer = Customer.objects.filter(customer_id=customer_id,
                                           store_id=store,
                                           deleted_at=None).first()

        if customer is None:
            return jsonify({"error": CUSTOMER_NOT_FOUND}), 404

        try:
            amount = int(amount)
        except ValueError:
            return jsonify({"error": AMOUNT_SHOULD_BE_INT}), 400

        credit = Credit(credit_id=str(uuid.uuid4().int),
                        customer=customer,
                        original_balance_in_cents=amount,
                        current_balance_in_cents=amount).save()

        response = {"result": "ok", "credit": credit_obj(credit)}
        return jsonify(response), 201
Example #7
0
    def apply_credits(cls, customer, invoice):
        credits = Credit.get_active_credits(customer)

        while invoice.get_subtotal_amount() > 0:
            try:
                credit = next(credits)
                credit.redeem(invoice)
            except StopIteration:
                return
def save_payment(data):
    if data['status'] == PAYMENT_STATUS.TAKEN:
        if data.pop('credited', False):
            resolution = CREDIT_RESOLUTION.CREDITED
        else:
            resolution = CREDIT_RESOLUTION.PENDING
    else:
        resolution = CREDIT_RESOLUTION.INITIAL

    prisoner_dob = data.pop('prisoner_dob', None)
    prisoner_number = data.pop('prisoner_number', None)
    single_offender_id = data.pop('single_offender_id', None)
    prisoner_name = data.pop('prisoner_name', None)
    prison = data.pop('prison', None)
    reconciled = data.pop('reconciled', False)
    owner = data.pop('owner', None)

    billing_address = data.pop('billing_address', None)
    if billing_address:
        billing_address = BillingAddress.objects.create(**billing_address)
        data['billing_address'] = billing_address

    credit = Credit(
        amount=data['amount'],
        prisoner_dob=prisoner_dob,
        prisoner_number=prisoner_number,
        single_offender_id=single_offender_id,
        prisoner_name=prisoner_name,
        prison=prison,
        reconciled=reconciled,
        owner=owner,
        received_at=data['created'],
        resolution=resolution
    )
    credit.save()
    data['credit'] = credit

    return Payment.objects.create(**data)
Example #9
0
    def create_credit(self):
        """
        creates a customer credit for the total refund amount.

        :return:
        """
        credit = Credit(
            credit_id=str(uuid.uuid4().int),
            customer=self.invoice.customer,
            original_balance_in_cents=self.get_refund_total(),
            current_balance_in_cents=self.get_refund_total()).save()

        self.credit = credit
        self.save()
Example #10
0
def save_payment(data):
    if data['status'] == PAYMENT_STATUS.TAKEN:
        if data.pop('credited', False):
            resolution = CREDIT_RESOLUTION.CREDITED
        else:
            resolution = CREDIT_RESOLUTION.PENDING
    else:
        resolution = CREDIT_RESOLUTION.INITIAL

    prisoner_dob = data.pop('prisoner_dob', None)
    prisoner_number = data.pop('prisoner_number', None)
    single_offender_id = data.pop('single_offender_id', None)
    prisoner_name = data.pop('prisoner_name', None)
    prison = data.pop('prison', None)
    reconciled = data.pop('reconciled', False)
    owner = data.pop('owner', None)

    billing_address = data.pop('billing_address', None)
    if billing_address:
        billing_address = BillingAddress.objects.create(**billing_address)
        data['billing_address'] = billing_address

    credit = Credit(amount=data['amount'],
                    prisoner_dob=prisoner_dob,
                    prisoner_number=prisoner_number,
                    single_offender_id=single_offender_id,
                    prisoner_name=prisoner_name,
                    prison=prison,
                    reconciled=reconciled,
                    owner=owner,
                    received_at=data['created'],
                    resolution=resolution)
    credit.save()
    data['credit'] = credit

    return Payment.objects.create(**data)
Example #11
0
def save_payment(data,
                 overrides=None,
                 attach_profiles_to_individual_credits=True):
    is_taken = data['status'] == PAYMENT_STATUS.TAKEN
    if is_taken:
        if data.pop('credited', False):
            resolution = CREDIT_RESOLUTION.CREDITED
        else:
            resolution = CREDIT_RESOLUTION.PENDING
    elif data['status'] in (PAYMENT_STATUS.REJECTED, PAYMENT_STATUS.EXPIRED):
        resolution = CREDIT_RESOLUTION.FAILED
    else:
        resolution = CREDIT_RESOLUTION.INITIAL

    prisoner_dob = data.pop('prisoner_dob', None)
    prisoner_number = data.pop('prisoner_number', None)
    prisoner_name = data.pop('prisoner_name', None)
    prison = data.pop('prison', None)
    reconciled = data.pop('reconciled', False)
    owner = data.pop('owner', None)

    billing_address = data.pop('billing_address', None)
    if billing_address:
        billing_address = BillingAddress.objects.create(**billing_address)
        data['billing_address'] = billing_address

    credit_data = dict(
        amount=data['amount'],
        prisoner_dob=prisoner_dob,
        prisoner_number=prisoner_number,
        prisoner_name=prisoner_name,
        prison=prison,
        reconciled=False if not is_taken else reconciled,
        owner=owner,
        received_at=None if not is_taken else data['created'],
        resolution=resolution,
    )
    if overrides:
        credit_data.update(overrides.get('credit', {}))
    credit = Credit(**credit_data)
    credit.save()
    data['credit'] = credit

    payment = Payment.objects.create(**data)
    if attach_profiles_to_individual_credits:
        credit.attach_profiles(ignore_credit_resolution=True)
    return payment