Exemple #1
0
def save_and_notify_payment(self, result, registration_id, logger_context=None):
    """
    Saves a users registration with new payment details and sends the stripe payment_intent
    client_secret to the client.
    Result is a stripe payment_intent
    """
    self.setup_logger(logger_context)

    try:
        registration = Registration.objects.get(id=registration_id)
        registration.payment_intent_id = result["id"]
        registration.payment_amount = result["amount"]
        registration.payment_intent_status = result["status"]
        registration.save()

    except IntegrityError as e:
        log.error(
            "registration_save_error", exception=e, registration_id=registration_id
        )
        raise self.retry(exc=e)

    notify_user_payment_initiated(
        constants.SOCKET_INITIATE_PAYMENT_SUCCESS,
        registration,
        success_message="Betaling påbegynt",
        client_secret=result["client_secret"],
    )
Exemple #2
0
def async_retrieve_payment(self, registration_id, logger_context=None):
    """
    Task that retrieves an existing payment intents client_secret from Stripe.
    If the client_secret is provided, this is returned directly
    """
    self.registration = Registration.objects.get(id=registration_id)

    if self.registration.payment_intent_id is None:
        log.error(
            "Attempted to retrieve a non-existing payment intent",
            registration=self.registration.id,
        )
        raise ValueError("The payment intent does not exist")

    try:
        payment_intent = stripe.PaymentIntent.retrieve(
            self.registration.payment_intent_id
        )
        client_secret = payment_intent["client_secret"]
    except stripe.error.InvalidRequestError as e:
        log.error("invalid_request", exception=e, registration_id=self.registration.id)
        self.registration.payment_status = e.json_body["error"]["type"]
        self.registration.save()
    except stripe.error.StripeError as e:
        log.error("stripe_error", exception=e, registration_id=self.registration.id)
        raise self.retry(exc=e, max_retries=3)
    except stripe.error.APIConnectionError as e:
        log.error(
            "stripe_APIConnectionError",
            exception=e,
            registration_id=self.registration.id,
        )
        raise self.retry(exc=e, max_retries=3)
    except Exception as e:
        log.error(
            "Exception on creating a payment intent",
            exception=e,
            registration=self.registration.id,
        )
        raise self.retry(exc=e)

    # Check that the payment intent is not already confirmed
    # If so, update the payment status to reflect reality
    # See https://stripe.com/docs/api/payment_intents/object
    if payment_intent["status"] == constants.STRIPE_INTENT_SUCCEEDED:
        self.registration.payment_status = constants.PAYMENT_SUCCESS
        self.registration.payment_amount = payment_intent["amount"]
        self.registration.save()
        notify_user_payment_error(
            constants.SOCKET_PAYMENT_FAILURE,
            self.registration,
            success_message="Betaling feilet",
            payment_error="The payment is already successful",
        )
        return

    # If the payment is canceled in stripe and the webhook for some reason
    # did not go through, we update the registration to match this, and then
    # initiate a new payment.
    if payment_intent["status"] == constants.STRIPE_INTENT_CANCELED:
        self.registration.payment_status = constants.PAYMENT_CANCELED
        self.registration.payment_intent_id = None
        self.registration.payment_idempotency_key = None
        self.registration.save()
        chain(
            async_initiate_payment.s(self.registration.id),
            save_and_notify_payment.s(self.registration.id),
        ).delay()
        return

    notify_user_payment_initiated(
        constants.SOCKET_INITIATE_PAYMENT_SUCCESS,
        self.registration,
        success_message="Betaling påbegynt",
        client_secret=client_secret,
    )