def get_charges(self): _initialize_stripe(live_mode=self.live_mode) charges = safe_stripe_call(stripe.Charge.all, **{ 'customer': self.stripe_id, }) charges = charges.get('data') return charges
def retrieve(self): """Retrieves an existing Plan """ _initialize_stripe(live_mode=self.live_mode) stripe_plan = safe_stripe_call(stripe.Plan.retrieve, *(self.stripe_id, )) return stripe_plan
def test_create_recipient_with_card(self): recipient = self._create_recipient() card_dict = self._get_card_dict(card_type='visa_debit') card = safe_stripe_call(recipient.cards.create, **{ 'card': card_dict, }) self.assertEqual(STRIPE_ID_PREFIX_CARD, card.id[:len(STRIPE_ID_PREFIX_CARD)])
def retrieve(self): """Retrieves an existing customer https://stripe.com/docs/api/python#retrieve_customer """ _initialize_stripe(live_mode=self.live_mode) stripe_customer = safe_stripe_call(stripe.Customer.retrieve, *(self.stripe_id, )) return stripe_customer
def get_cards(self): stripe_customer = self.retrieve() if stripe_customer: cards = safe_stripe_call(stripe_customer.sources.all, **{ 'object': 'card', }) cards = cards.get('data') else: cards = [] return cards
def test_create_recipient_with_card(self): recipient = self._create_recipient() card_dict = self._get_card_dict(card_type='visa_debit') card = safe_stripe_call( recipient.cards.create, **{ 'card' : card_dict, } ) self.assertEqual(STRIPE_ID_PREFIX_CARD, card.id[:len(STRIPE_ID_PREFIX_CARD)])
def create_invoice(self): """Create an Invoice for this Customer to pay any outstanding invoice items such as when upgrading plans https://stripe.com/docs/api#create_invoice """ _initialize_stripe(live_mode=self.live_mode) invoice = safe_stripe_call(stripe.Invoice.create, **{ 'customer': self.stripe_id, }) return invoice
def get_charges(self): _initialize_stripe(live_mode=self.live_mode) charges = safe_stripe_call( stripe.Charge.all, **{ 'customer' : self.stripe_id, } ) charges = charges.get('data') return charges
def get_cards(self): stripe_customer = self.retrieve() cards = safe_stripe_call( stripe_customer.sources.all, **{ 'object' : 'card', } ) cards = cards.get('data') return cards
def _create_recipient(self): stripe = _initialize_stripe() recipient = safe_stripe_call( stripe.Recipient.create, **{ 'name': 'John Doe', 'type': 'individual', }) self.assertEqual(STRIPE_ID_PREFIX_RECIPIENT, recipient.id[:len(STRIPE_ID_PREFIX_RECIPIENT)]) return recipient
def create_subscription(self, plan): """Creates a new Subscription for this Customer https://stripe.com/docs/api#create_subscription """ stripe_customer = self.retrieve() subscription = safe_stripe_call(stripe_customer.subscriptions.create, **{ 'plan': plan, }) return subscription
def _create_recipient(self): stripe = _initialize_stripe() recipient = safe_stripe_call( stripe.Recipient.create, **{ 'name' : 'John Doe', 'type' : 'individual', } ) self.assertEqual(STRIPE_ID_PREFIX_RECIPIENT, recipient.id[:len(STRIPE_ID_PREFIX_RECIPIENT)]) return recipient
def retrieve(self): """Retrieves an existing Plan """ _initialize_stripe(live_mode=self.live_mode) stripe_plan = safe_stripe_call( stripe.Plan.retrieve, *( self.stripe_id, ) ) return stripe_plan
def has_card(self): """Determines whether this StripeCustomer has a card """ stripe_customer = self.retrieve() cards = safe_stripe_call( stripe_customer.cards.all, **{ 'limit' : 1, } ) value = len(cards) > 0 return value
def delete(self): """Deletes a customer https://stripe.com/docs/api/python#delete_customer """ stripe_customer = self.retrieve() obj = safe_stripe_call( stripe_customer.delete ) if obj: super(BaseStripeCustomer, self).delete() else: pass
def retrieve(self): """Retrieves an existing customer https://stripe.com/docs/api/python#retrieve_customer """ _initialize_stripe(live_mode=self.live_mode) stripe_customer = safe_stripe_call( stripe.Customer.retrieve, *( self.stripe_id, ) ) return stripe_customer
def retrieve_subscription(self, subscription_id): """Retrieves a Subscription for this Customer https://stripe.com/docs/api#retrieve_subscription """ stripe_customer = self.retrieve() subscription = safe_stripe_call( stripe_customer.subscriptions.retrieve, **{ 'id' : subscription_id, } ) return subscription
def charge(self, amount=0, currency=DEFAULT_STRIPE_CURRENCY): """Charges a Customer """ _initialize_stripe(live_mode=self.live_mode) ch = safe_stripe_call( stripe.Charge.create, **{ 'amount' : amount, 'currency' : currency, 'customer' : self.stripe_id, } ) return ch
def create_subscription(self, plan): """Creates a new Subscription for this Customer https://stripe.com/docs/api#create_subscription """ stripe_customer = self.retrieve() subscription = safe_stripe_call( stripe_customer.subscriptions.create, **{ 'plan' : plan, } ) return subscription
def create_invoice(self): """Create an Invoice for this Customer to pay any outstanding invoice items such as when upgrading plans https://stripe.com/docs/api#create_invoice """ _initialize_stripe(live_mode=self.live_mode) invoice = safe_stripe_call( stripe.Invoice.create, **{ 'customer' : self.stripe_id, } ) return invoice
def has_card(self): """Determines whether this StripeCustomer has a card """ stripe_customer = self.retrieve() if stripe_customer: cards = safe_stripe_call(stripe_customer.sources.all, **{ 'limit': 1, 'object': 'card', }) value = len(cards) > 0 else: value = False return value
def add_card(self, card): """Add an additional credit card to the customer https://stripe.com/docs/api/python#create_card """ stripe_customer = self.retrieve() if stripe_customer: stripe_card = safe_stripe_call(stripe_customer.sources.create, **{ 'source': card, }) else: stripe_card = None was_added = stripe_card is not None return was_added
def get_card(self): stripe_customer = self.retrieve() cards = safe_stripe_call( stripe_customer.cards.all, **{ 'limit' : 1, } ) cards = cards.get('data') if len(cards) > 0: card = cards[0] else: card = None return card
def create(self): """Tries to create a product Will fail if product with the same Stripe id already exists """ _initialize_stripe(live_mode=self.live_mode) stripe_product = safe_stripe_call( stripe.Product.create, **{ 'id': self.stripe_id, 'name': self.name, 'type': StripeProductType(self.product_type).name, 'active': self.active, 'statement_descriptor': self.statement_descriptor, }) return stripe_product
def update_email(self, email=None): """Updates the email for this Customer """ stripe_customer = None if email is not None: stripe_customer = self.retrieve() if stripe_customer.email != email: # TODO: it might make sense to implement some kind of update-if-changed, or perhaps the Stripe library is already smart about it? stripe_customer.email = email stripe_customer = safe_stripe_call(stripe_customer.save, **{}) else: pass else: pass return stripe_customer
def replace_card(self, card): """Adds a new credit card and delete this Customer's old one WARNING: This deletes the old card. Use `add_card` instead to just add a card without deleting https://stripe.com/docs/api/python#update_customer """ stripe_customer = self.retrieve() if stripe_customer: stripe_customer.source = card cu = safe_stripe_call(stripe_customer.save, **{}) else: cu = None was_replaced = cu is not None return was_replaced
def charge(self, amount=0, currency=DEFAULT_STRIPE_CURRENCY, metadata=None): """Charges a Customer """ if metadata is None: metadata = {} _initialize_stripe(live_mode=self.live_mode) ch = safe_stripe_call( stripe.Charge.create, **{ 'amount' : amount, 'currency' : currency, 'customer' : self.stripe_id, 'metadata' : metadata } ) return ch
def cancel_subscription(self, subscription_id): """Cancels a Subscription for this Customer https://stripe.com/docs/api#cancel_subscription Returns: - True if `subscription_id` was canceled - False if `subscription_id` was not found """ subscription = self.retrieve_subscription(subscription_id) if subscription: _ = safe_stripe_call(subscription.delete) was_deleted = True else: was_deleted = False return was_deleted
def charge(self, amount=0, currency=DEFAULT_STRIPE_CURRENCY, metadata=None): """Charges a Customer """ if metadata is None: metadata = {} _initialize_stripe(live_mode=self.live_mode) ch = safe_stripe_call( stripe.Charge.create, **{ 'amount' : amount, 'currency' : currency, 'customer' : self.stripe_id, 'metadata' : metadata } ) return ch
def has_card(self): """Determines whether this StripeCustomer has a card """ stripe_customer = self.retrieve() if stripe_customer: cards = safe_stripe_call( stripe_customer.sources.all, **{ 'limit' : 1, 'object' : 'card', } ) value = len(cards) > 0 else: value = False return value
def add_card(self, card): """Add an additional credit card to the customer https://stripe.com/docs/api/python#create_card """ stripe_customer = self.retrieve() if stripe_customer: stripe_card = safe_stripe_call( stripe_customer.sources.create, **{ 'source' : card, } ) else: stripe_card = None was_added = stripe_card is not None return was_added
def create(self): """Tries to create a product Will fail if product with the same Stripe id already exists """ _initialize_stripe(live_mode=self.live_mode) stripe_product = safe_stripe_call( stripe.Product.create, **{ 'id' : self.stripe_id, 'name' : self.name, 'type' : StripeProductType(self.product_type).name, 'active' : self.active, 'statement_descriptor' : self.statement_descriptor, } ) return stripe_product
def update_email(self, email=None): """Updates the email for this Customer """ stripe_customer = None if email is not None: stripe_customer = self.retrieve() if stripe_customer.email != email: # TODO: it might make sense to implement some kind of update-if-changed, or perhaps the Stripe library is already smart about it? stripe_customer.email = email stripe_customer = safe_stripe_call( stripe_customer.save, **{} ) else: pass else: pass return stripe_customer
def replace_card(self, card): """Adds a new credit card and delete this Customer's old one WARNING: This deletes the old card. Use `add_card` instead to just add a card without deleting https://stripe.com/docs/api/python#update_customer """ stripe_customer = self.retrieve() if stripe_customer: stripe_customer.source = card cu = safe_stripe_call( stripe_customer.save, **{} ) else: cu = None was_replaced = cu is not None return was_replaced
def create(self): """Tries to create a plan Will fail if plan with the same Stripe id already exists """ _initialize_stripe(live_mode=self.live_mode) stripe_plan = safe_stripe_call( stripe.Plan.create, **{ 'id': self.stripe_id, 'amount': self.amount, 'currency': self.currency, 'interval': StripePlanInterval(self.interval).name, 'interval_count': self.interval_count, 'name': self.name, 'trial_period_days': self.trial_period_days, 'statement_description': self.statement_description, }) return stripe_plan
def get_card(self): """ https://stripe.com/docs/api/python#list_cards """ stripe_customer = self.retrieve() cards = safe_stripe_call( stripe_customer.sources.all, **{ 'limit' : 1, 'object' : 'card', } ) cards = cards.get('data') if len(cards) > 0: card = cards[0] else: card = None return card
def get_card(self): """ https://stripe.com/docs/api/python#list_cards """ stripe_customer = self.retrieve() if stripe_customer: cards = safe_stripe_call(stripe_customer.sources.all, **{ 'limit': 1, 'object': 'card', }) cards = cards.get('data') if len(cards) > 0: card = cards[0] else: card = None else: card = None return card
def create(self): """Tries to create a plan Will fail if plan with the same Stripe id already exists """ _initialize_stripe(live_mode=self.live_mode) stripe_plan = safe_stripe_call( stripe.Plan.create, **{ 'id' : self.stripe_id, 'amount' : self.amount, 'currency' : self.currency, 'interval' : StripePlanInterval(self.interval).name, 'interval_count' : self.interval_count, 'name' : self.name, 'trial_period_days' : self.trial_period_days, 'statement_description' : self.statement_description, } ) return stripe_plan
def retrieve_subscription(self, subscription_id): """Retrieves a Subscription for this Customer https://stripe.com/docs/api#retrieve_subscription """ subscription = None if subscription_id: stripe_customer = self.retrieve() if stripe_customer: subscription = safe_stripe_call( stripe_customer.subscriptions.retrieve, **{ 'id': subscription_id, }) else: # missing Stripe customer pass else: # missing subscription id pass return subscription
def retrieve_subscription(self, subscription_id): """Retrieves a Subscription for this Customer https://stripe.com/docs/api#retrieve_subscription """ subscription = None if subscription_id: stripe_customer = self.retrieve() if stripe_customer: subscription = safe_stripe_call( stripe_customer.subscriptions.retrieve, **{ 'id' : subscription_id, } ) else: # missing Stripe customer pass else: # missing subscription id pass return subscription