Esempio n. 1
0
 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
Esempio n. 2
0
 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
Esempio n. 3
0
 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)])
Esempio n. 4
0
    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
Esempio n. 5
0
 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
Esempio n. 6
0
 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)])
Esempio n. 7
0
    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
Esempio n. 8
0
 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
Esempio n. 9
0
 def get_cards(self):
     stripe_customer = self.retrieve()
     cards = safe_stripe_call(
         stripe_customer.sources.all,
         **{
             'object' : 'card',
         }
     )
     cards = cards.get('data')
     return cards
Esempio n. 10
0
 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
Esempio n. 11
0
    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
Esempio n. 12
0
 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
Esempio n. 13
0
 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
Esempio n. 14
0
 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
Esempio n. 15
0
    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
Esempio n. 16
0
    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
Esempio n. 17
0
    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
Esempio n. 18
0
 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
Esempio n. 19
0
    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
Esempio n. 20
0
    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
Esempio n. 21
0
 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
Esempio n. 22
0
    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
Esempio n. 23
0
 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
Esempio n. 24
0
    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
Esempio n. 25
0
 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
Esempio n. 26
0
    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
Esempio n. 27
0
 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
Esempio n. 28
0
    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
Esempio n. 29
0
 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
Esempio n. 30
0
 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
Esempio n. 31
0
    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
Esempio n. 32
0
    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
Esempio n. 33
0
 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
Esempio n. 34
0
    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
Esempio n. 35
0
    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
Esempio n. 36
0
 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
Esempio n. 37
0
 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
Esempio n. 38
0
    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
Esempio n. 39
0
    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
Esempio n. 40
0
    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