# Copyright (c) 2022, JINGROW
# For license information, please see license.txt

import jingrow
from jingrow.model.page import Page

from jcloud.utils.billing import get_stripe


class StripeMicroChargeRecord(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF

		has_been_refunded: DF.Check
		stripe_payment_intent_id: DF.Data
		stripe_payment_method: DF.Link | None
		team: DF.Link | None
	# end: auto-generated types

	def after_insert(self):
		# Auto-refund
		self.refund()

	@jingrow.whitelist()
	def refund(self):
		stripe = get_stripe()
		refund = stripe.Refund.create(payment_intent=self.stripe_payment_intent_id)

		if refund.status == "succeeded":
			self.has_been_refunded = True
			self.save()
