Пример #1
0
    def handle_patch_request_refund(self, nr_model: Request):
        """
        Can the NR and request a refund for ALL associated Name Request payments.
        :param nr_model:
        :return:
        """
        nr_svc = self.nr_service

        # This handles updates if the NR state is 'patchable'
        nr_model = self.update_nr(nr_model, State.REFUND_REQUESTED,
                                  self.handle_nr_patch)

        # Handle the payments
        valid_states = [
            PaymentState.APPROVED.value, PaymentState.COMPLETED.value,
            PaymentState.PARTIAL.value
        ]
        # Cancel any payments associated with the NR
        for payment in nr_model.payments.all():
            if payment.payment_status_code in valid_states:
                # refund_payment(payment.payment_token, {'reason': 'Name Request user requested refund'})
                refund_payment(payment.payment_token, {})
                payment.payment_status_code = PaymentState.REFUND_REQUESTED.value
                payment.save_to_db()

        # This handles the updates for NRO and Solr, if necessary
        nr_model = self.update_records_in_network_services(nr_model,
                                                           update_solr=True)

        # Record the event
        EventRecorder.record(nr_svc.user, Event.PATCH + ' [request-refund]',
                             nr_model, nr_svc.request_data)

        return nr_model
Пример #2
0
    def request_refund(self, nr_model: RequestDAO, payment_id: int):
        """
        Processes a SINGLE refund request.
        This is different from the 'refund' in the NameRequest resource PATCH namerequests/{nrId}/REQUEST_REFUND
        which cancels the NR and refunds any associated payments.
        :param nr_model:
        :param payment_id:
        :return:
        """
        # Handle the payments
        valid_states = [
            PaymentState.COMPLETED.value,
            PaymentState.PARTIAL.value
        ]
        if nr_model.stateCd not in [State.DRAFT]:
            raise PaymentServiceError(message='Invalid NR state for cancel and refund')
        # Cancel any payments associated with the NR
        for payment in nr_model.payments.all():
            if payment.payment_status_code in valid_states and payment.id == payment_id:
                # refund_payment(payment.payment_token, {'reason': 'Name Request user requested refund'})
                refund_payment(payment.payment_token, {})
                payment.payment_status_code = PaymentState.REFUND_REQUESTED.value
                payment.save_to_db()

        return nr_model
Пример #3
0
    def handle_patch_request_refund(self, nr_model: Request):
        """
        Can the NR and request a refund for ALL associated Name Request payments.
        :param nr_model:
        :return:
        """
        nr_svc = self.nr_service

        # This handles updates if the NR state is 'patchable'
        nr_model = self.update_nr(nr_model, State.REFUND_REQUESTED,
                                  self.handle_nr_patch)

        # Handle the payments
        valid_states = [
            PaymentState.APPROVED.value, PaymentState.COMPLETED.value,
            PaymentState.PARTIAL.value
        ]

        refund_value = 0

        # Check for NR that has been renewed - do not refund any payments.
        # UI should not order refund for an NR renewed/reapplied it.
        if not any(
                payment.payment_action == Payment.PaymentActions.REAPPLY.value
                for payment in nr_model.payments.all()):
            # Try to refund all payments associated with the NR
            for payment in nr_model.payments.all():
                if payment.payment_status_code in valid_states:
                    # Some refunds may fail. Some payment methods are not refundable and return HTTP 400 at the refund.
                    # The refund status is checked from the payment_response and a appropriate message is displayed by the UI.
                    refund_payment(payment.payment_token, {})
                    payment_response = get_payment(payment.payment_token)
                    payment.payment_status_code = PaymentState.REFUND_REQUESTED.value
                    payment.save_to_db()
                    refund_value += payment_response.receipts[0][
                        'receiptAmount'] if len(
                            payment_response.receipts) else 0

        publish_email_notification(nr_model.nrNum, 'refund',
                                   '{:.2f}'.format(refund_value))

        # This handles the updates for NRO and Solr, if necessary
        nr_model = self.update_records_in_network_services(nr_model,
                                                           update_solr=True)

        # Record the event
        EventRecorder.record(nr_svc.user, Event.PATCH + ' [request-refund]',
                             nr_model, nr_model.json())

        return nr_model