forked from uncloud/uncloud
Replace legacy Stripe Charge API by Payment{setup, intent}
This commit is contained in:
parent
47148454f6
commit
bf83b750de
10 changed files with 250 additions and 50 deletions
|
|
@ -119,27 +119,31 @@ class PaymentMethod(models.Model):
|
|||
primary = models.BooleanField(default=True)
|
||||
|
||||
# Only used for "Stripe" source
|
||||
stripe_card_id = models.CharField(max_length=32, blank=True, null=True)
|
||||
stripe_payment_method_id = models.CharField(max_length=32, blank=True, null=True)
|
||||
stripe_setup_intent_id = models.CharField(max_length=32, blank=True, null=True)
|
||||
|
||||
@property
|
||||
def stripe_card_last4(self):
|
||||
if self.source == 'stripe':
|
||||
card_request = uncloud_pay.stripe.get_card(
|
||||
StripeCustomer.objects.get(owner=self.owner).stripe_id,
|
||||
self.stripe_card_id)
|
||||
if card_request['error'] == None:
|
||||
return card_request['response_object']['last4']
|
||||
else:
|
||||
return None
|
||||
if self.source == 'stripe' and self.active:
|
||||
payment_method = uncloud_pay.stripe.get_payment_method(
|
||||
self.stripe_payment_method_id)
|
||||
return payment_method.card.last4
|
||||
else:
|
||||
return None
|
||||
|
||||
@property
|
||||
def active(self):
|
||||
if self.source == 'stripe' and self.stripe_payment_method_id != None:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def charge(self, amount):
|
||||
if amount > 0: # Make sure we don't charge negative amount by errors...
|
||||
if self.source == 'stripe':
|
||||
stripe_customer = StripeCustomer.objects.get(owner=self.owner).stripe_id
|
||||
charge_request = uncloud_pay.stripe.charge_customer(amount, stripe_customer, self.stripe_card_id)
|
||||
charge_request = uncloud_pay.stripe.charge_customer(
|
||||
amount, stripe_customer, self.stripe_payment_method_id)
|
||||
if charge_request['error'] == None:
|
||||
payment = Payment(owner=self.owner, source=self.source, amount=amount)
|
||||
payment.save() # TODO: Check return status
|
||||
|
|
@ -163,7 +167,8 @@ class PaymentMethod(models.Model):
|
|||
return None
|
||||
|
||||
class Meta:
|
||||
unique_together = [['owner', 'primary']]
|
||||
#API_keyunique_together = [['owner', 'primary']]
|
||||
pass
|
||||
|
||||
###
|
||||
# Bills & Payments.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue