Example #1
0
    def setUp(self) -> None:
        fixture_dir = "src/hub/tests/unit/fixtures/"
        with open(f"{fixture_dir}stripe_cust_test1.json") as fh:
            cust_test1 = json.loads(fh.read())
        self.customer = convert_to_stripe_object(cust_test1)

        with open(f"{fixture_dir}stripe_cust_test2.json") as fh:
            cust_test2 = json.loads(fh.read())
        self.customer_active_sub = convert_to_stripe_object(cust_test2)

        with open(f"{fixture_dir}stripe_cust_no_metadata.json") as fh:
            cust_no_metadata = json.loads(fh.read())
        self.customer_missing_user = convert_to_stripe_object(cust_no_metadata)

        with open(f"{fixture_dir}stripe_cust_test1_deleted.json") as fh:
            cust_test1_deleted = json.loads(fh.read())
        self.deleted_customer = convert_to_stripe_object(cust_test1_deleted)

        with open(f"{fixture_dir}stripe_sub_deleted_event.json") as fh:
            self.subscription_deleted_event = json.loads(fh.read())

        with open(f"{fixture_dir}stripe_sub_test3.json") as fh:
            self.sub_to_delete = json.loads(fh.read())

        customer_patcher = patch("stripe.Customer.retrieve")
        delete_customer_patcher = patch("stripe.Customer.delete")
        run_pipeline_patcher = patch("hub.routes.pipeline.RoutesPipeline.run")

        self.addCleanup(customer_patcher.stop)
        self.addCleanup(run_pipeline_patcher.stop)
        self.addCleanup(delete_customer_patcher.stop)

        self.mock_customer = customer_patcher.start()
        self.mock_run_pipeline = run_pipeline_patcher.start()
        self.mock_delete_customer = delete_customer_patcher.start()
Example #2
0
    def setUp(self):
        with open(os.path.join(DIRECTORY, "fixtures/stripe_cust_test1.json")) as fh:
            customer = json.loads(fh.read())
        self.customer = convert_to_stripe_object(customer)

        with open(os.path.join(DIRECTORY, "fixtures/stripe_deleted_cust.json")) as fh:
            deleted_customer = json.loads(fh.read())
        self.deleted_customer = convert_to_stripe_object(deleted_customer)

        create_customer_patcher = patch("stripe.Customer.create")
        retrieve_customer_patcher = patch("stripe.Customer.retrieve")
        list_customer_patcher = patch("stripe.Customer.list")
        modify_customer_patcher = patch("stripe.Customer.modify")
        delete_customer_patcher = patch("stripe.Customer.delete")

        self.addCleanup(create_customer_patcher.stop)
        self.addCleanup(retrieve_customer_patcher.stop)
        self.addCleanup(list_customer_patcher.stop)
        self.addCleanup(modify_customer_patcher.stop)
        self.addCleanup(delete_customer_patcher.stop)

        self.create_customer_mock = create_customer_patcher.start()
        self.retrieve_customer_mock = retrieve_customer_patcher.start()
        self.list_customer_mock = list_customer_patcher.start()
        self.modify_customer_mock = modify_customer_patcher.start()
        self.delete_customer_mock = delete_customer_patcher.start()
Example #3
0
    def setUp(self) -> None:
        fixture_dir = "src/hub/tests/unit/fixtures/"
        with open(f"{fixture_dir}stripe_cust_test1.json") as fh:
            cust_test1 = json.loads(fh.read())
        self.customer = convert_to_stripe_object(cust_test1)

        with open(f"{fixture_dir}stripe_sub_test1.json") as fh:
            self.subscription = json.loads(fh.read())

        with open(f"{fixture_dir}stripe_sub_test2.json") as fh:
            self.subscription2 = json.loads(fh.read())

        with open(f"{fixture_dir}stripe_plan_test1.json") as fh:
            self.plan = json.loads(fh.read())

        with open(f"{fixture_dir}stripe_prod_test1.json") as fh:
            prod_test1 = json.loads(fh.read())
        self.product = convert_to_stripe_object(prod_test1)

        with open(f"{fixture_dir}stripe_source_expiring_event.json") as fh:
            self.source_expiring_event = json.loads(fh.read())

        customer_patcher = patch("stripe.Customer.retrieve")
        product_patcher = patch("stripe.Product.retrieve")
        run_pipeline_patcher = patch("hub.routes.pipeline.RoutesPipeline.run")

        self.addCleanup(customer_patcher.stop)
        self.addCleanup(product_patcher.stop)
        self.addCleanup(run_pipeline_patcher.stop)

        self.mock_customer = customer_patcher.start()
        self.mock_product = product_patcher.start()
        self.mock_run_pipeline = run_pipeline_patcher.start()
Example #4
0
def test_subscribe_success(
    mock_new_customer,
    mock_has_plan,
    mock_subscription,
    mock_fetch_customer,
    mock_new_sub,
    mock_product,
    app,
):
    """
    GIVEN a route that attempts to make a subscribe a customer
    WHEN valid data is provided
    THEN a success status of 201 will be returned
    """

    client = app.app.test_client()
    fh = open(os.path.join(DIRECTORY, "fixtures/stripe_cust_test1.json"))
    cust_test1 = json.loads(fh.read())
    fh.close()

    mock_new_customer.return_value = convert_to_stripe_object(cust_test1)
    mock_has_plan.return_value = False
    mock_fetch_customer.return_value = convert_to_stripe_object(cust_test1)

    with open(os.path.join(DIRECTORY, "fixtures/stripe_sub_test1.json")) as fh:
        sub_test1 = json.loads(fh.read())

    with open(os.path.join(DIRECTORY,
                           "fixtures/stripe_plan_test1.json")) as fh:
        plan_test1 = json.loads(fh.read())

    sub_test1["plan"] = plan_test1
    mock_subscription.return_value = sub_test1
    mock_new_sub.return_value = {"data": [sub_test1]}

    with open(os.path.join(DIRECTORY,
                           "fixtures/stripe_prod_test1.json")) as fh:
        prod_test1 = json.loads(fh.read())

    mock_product.return_value = prod_test1

    path = "v1/sub/customer/subtest/subscriptions"
    data = {
        "pmt_token": "tok_visa",
        "plan_id": "plan",
        "origin_system": "fake_origin1",
        "email": "*****@*****.**",
        "display_name": "John Tester",
    }

    response = client.post(
        path,
        headers={"Authorization": "fake_payment_api_key"},
        data=json.dumps(data),
        content_type="application/json",
    )
    logger.info("response data", data=response.data)
    assert response.status_code == 201
Example #5
0
    def setUp(self) -> None:
        with open(os.path.join(DIRECTORY, "fixtures/stripe_ch_test1.json")) as fh:
            charge = json.loads(fh.read())
        self.charge = convert_to_stripe_object(charge)

        with open(os.path.join(DIRECTORY, "fixtures/stripe_ch_test2.json")) as fh:
            charge2 = json.loads(fh.read())
        self.charge2 = convert_to_stripe_object(charge)

        retrieve_charge_patcher = patch("stripe.Charge.retrieve")
        self.addCleanup(retrieve_charge_patcher.stop)
        self.retrieve_charge_mock = retrieve_charge_patcher.start()
Example #6
0
    def create(cls, api_key=None, idempotency_key=None,
               stripe_version=None, stripe_account=None,
               api_version=None, **params):
        if stripe_version is None:
            if api_version is not None:
                stripe_version = api_version
                warnings.warn(
                    "The `api_version` parameter when creating an ephemeral "
                    "key is deprecated. Please use `stripe_version` instead.",
                    DeprecationWarning)
            else:
                raise ValueError(
                    "stripe_version must be specified to create an ephemeral "
                    "key")

        requestor = api_requestor.APIRequestor(
            api_key,
            api_version=stripe_version,
            account=stripe_account
        )

        url = cls.class_url()
        headers = util.populate_headers(idempotency_key)
        response, api_key = requestor.request('post', url, params, headers)
        return util.convert_to_stripe_object(response, api_key, stripe_version,
                                             stripe_account)
async def _(db=db):
    subscription = await SubscriptionFactory(user_id=1)
    await StripeCustomerFactory(user_id=1,
                                stripe_customer_id="cus_customer_id")

    with time_machine.travel("2021-10-10 12:00:00", tick=False):
        await handle_invoice_paid(
            util.convert_to_stripe_object({
                **RAW_INVOICE_PAID_PAYLOAD,
                "data": {
                    **RAW_INVOICE_PAID_PAYLOAD["data"],
                    "object": {
                        **RAW_INVOICE_PAID_PAYLOAD["data"]["object"],
                        "lines": {
                            "data": [{
                                **RAW_INVOICE_PAID_PAYLOAD["data"]["object"]["lines"]["data"][0],
                                "period": {
                                    "end":
                                    1607979272,  # Mon Dec 14 2020 20:54:32 GMT+0000
                                    "start":
                                    1576356872,  # Sat Dec 14 2019 20:54:32 GMT+0000
                                },
                            }],
                        },
                    },
                },
            }))

    subscription = await Subscription.objects.select_related(
        ["payments", "payments__stripesubscriptionpayments"]).get(user_id=1)
    assert subscription.status == SubscriptionStatus.PENDING
    assert len(subscription.payments) == 1
Example #8
0
    def _static_request(
        cls,
        method_,
        url_,
        api_key=None,
        idempotency_key=None,
        stripe_version=None,
        stripe_account=None,
        params=None,
    ):
        params = None if params is None else params.copy()
        api_key = util.read_special_variable(params, "api_key", api_key)
        idempotency_key = util.read_special_variable(params, "idempotency_key",
                                                     idempotency_key)
        stripe_version = util.read_special_variable(params, "stripe_version",
                                                    stripe_version)
        stripe_account = util.read_special_variable(params, "stripe_account",
                                                    stripe_account)
        headers = util.read_special_variable(params, "headers", None)

        requestor = api_requestor.APIRequestor(api_key,
                                               api_version=stripe_version,
                                               account=stripe_account)

        if idempotency_key is not None:
            headers = {} if headers is None else headers.copy()
            headers.update(util.populate_headers(idempotency_key))

        response, api_key = requestor.request(method_, url_, params, headers)
        return util.convert_to_stripe_object(response, api_key, stripe_version,
                                             stripe_account, params)
Example #9
0
    def refresh_from(self,
                     values,
                     api_key=None,
                     partial=False,
                     stripe_version=None,
                     stripe_account=None,
                     last_response=None):
        self.api_key = api_key or getattr(values, 'api_key', None)
        self.stripe_version = \
            stripe_version or getattr(values, 'stripe_version', None)
        self.stripe_account = \
            stripe_account or getattr(values, 'stripe_account', None)
        self._last_response = last_response

        # Wipe old state before setting new.  This is useful for e.g.
        # updating a customer, where there is no persistent card
        # parameter.  Mark those values which don't persist as transient
        if partial:
            self._unsaved_values = (self._unsaved_values - set(values))
        else:
            removed = set(self.keys()) - set(values)
            self._transient_values = self._transient_values | removed
            self._unsaved_values = set()
            self.clear()

        self._transient_values = self._transient_values - set(values)

        for k, v in six.iteritems(values):
            super(StripeObject, self).__setitem__(
                k,
                util.convert_to_stripe_object(v, api_key, stripe_version,
                                              stripe_account))

        self._previous = values
Example #10
0
    def refresh_from(self, values, api_key=None, partial=False,
                     stripe_version=None, stripe_account=None,
                     last_response=None):
        self.api_key = api_key or getattr(values, 'api_key', None)
        self.stripe_version = \
            stripe_version or getattr(values, 'stripe_version', None)
        self.stripe_account = \
            stripe_account or getattr(values, 'stripe_account', None)
        self._last_response = \
            last_response or getattr(values, '_last_response', None)

        # Wipe old state before setting new.  This is useful for e.g.
        # updating a customer, where there is no persistent card
        # parameter.  Mark those values which don't persist as transient
        if partial:
            self._unsaved_values = (self._unsaved_values - set(values))
        else:
            removed = set(self.keys()) - set(values)
            self._transient_values = self._transient_values | removed
            self._unsaved_values = set()
            self.clear()

        self._transient_values = self._transient_values - set(values)

        for k, v in six.iteritems(values):
            super(StripeObject, self).__setitem__(
                k, util.convert_to_stripe_object(v, api_key, stripe_version,
                                                 stripe_account))

        self._previous = values
Example #11
0
    def test_convert_to_stripe_object_and_back(self):
        resp = {
            "object":
            "balance",
            "available": [{
                "amount": 234,
                "currency": "usd",
                "source_types": {
                    "card": 234
                },
            }],
            "livemode":
            False,
        }

        obj = util.convert_to_stripe_object(resp)
        assert type(obj) == stripe.Balance
        assert type(obj.available) == list
        assert type(obj.available[0]) == stripe.stripe_object.StripeObject

        d = util.convert_to_dict(obj)
        assert type(d) == dict
        assert type(d["available"]) == list
        assert type(d["available"][0]) == dict

        assert d == resp
Example #12
0
def test_subscribe_customer_existing(mock_new_customer, mock_has_plan, app):
    """
    GIVEN a route that attempts to make a subscribe a customer
    WHEN the customer already exists
    THEN an error status of 409 will be returned
    """

    client = app.app.test_client()
    fh = open("tests/unit/fixtures/stripe_cust_test1.json")
    cust_test1 = json.loads(fh.read())
    fh.close()

    mock_new_customer.return_value = convert_to_stripe_object(cust_test1)
    mock_has_plan.return_value = True

    path = "v1/sub/customer/subtest/subscriptions"
    data = {
        "pmt_token": "tok_visa",
        "plan_id": "plan",
        "origin_system": "Test_system",
        "email": "*****@*****.**",
        "display_name": "John Tester",
    }

    response = client.post(
        path,
        headers={"Authorization": "fake_payment_api_key"},
        data=json.dumps(data),
        content_type="application/json",
    )

    assert response.status_code == 409
Example #13
0
    def create(cls,
               api_key=None,
               idempotency_key=None,
               stripe_version=None,
               stripe_account=None,
               api_version=None,
               **params):
        if stripe_version is None:
            if api_version is not None:
                stripe_version = api_version
                warnings.warn(
                    "The `api_version` parameter when creating an ephemeral "
                    "key is deprecated. Please use `stripe_version` instead.",
                    DeprecationWarning)
            else:
                raise ValueError(
                    "stripe_version must be specified to create an ephemeral "
                    "key")

        requestor = api_requestor.APIRequestor(api_key,
                                               api_version=stripe_version,
                                               account=stripe_account)

        url = cls.class_url()
        headers = util.populate_headers(idempotency_key)
        response, api_key = requestor.request('post', url, params, headers)
        return util.convert_to_stripe_object(response, api_key, stripe_version,
                                             stripe_account)
Example #14
0
 def list_line_items(self, idempotency_key=None, **params):
     url = self.instance_url() + "/line_items"
     headers = util.populate_headers(idempotency_key)
     resp = self.request("get", url, params, headers)
     stripe_object = util.convert_to_stripe_object(resp)
     stripe_object._retrieve_params = params
     return stripe_object
Example #15
0
    def setUp(self) -> None:
        with open(os.path.join(DIRECTORY, "fixtures/stripe_sub_test1.json")) as fh:
            sub = json.loads(fh.read())

        with open(os.path.join(DIRECTORY, "fixtures/stripe_plan_test1.json")) as fh:
            plan = json.loads(fh.read())

        sub["plan"] = plan
        sub["items"] = {"object": "list", "data": [{"id": "si_test1", "plan": plan}]}

        self.subscription = convert_to_stripe_object(sub)

        self.list = [self.subscription]

        create_subcription_patcher = patch("stripe.Subscription.create")
        retrieve_subscription_patcher = patch("stripe.Subscription.retrieve")
        list_subscription_patcher = patch("stripe.Subscription.list")
        modify_subscription_patcher = patch("stripe.Subscription.modify")
        delete_subscription_patcher = patch("stripe.Subscription.delete")

        self.addCleanup(create_subcription_patcher.stop)
        self.addCleanup(retrieve_subscription_patcher.stop)
        self.addCleanup(list_subscription_patcher.stop)
        self.addCleanup(modify_subscription_patcher.stop)
        self.addCleanup(delete_subscription_patcher.stop)

        self.mock_create_subscription = create_subcription_patcher.start()
        self.mock_retrieve_subscription = retrieve_subscription_patcher.start()
        self.mock_list_subscription = list_subscription_patcher.start()
        self.mock_modify_subscription = modify_subscription_patcher.start()
        self.mock_delete_subscription = delete_subscription_patcher.start()
Example #16
0
    def setUp(self) -> None:
        with open(os.path.join(DIRECTORY, "fixtures/stripe_prod_test1.json")) as fh:
            prod = json.loads(fh.read())
        self.product = convert_to_stripe_object(prod)

        retrieve_product_patcher = patch("stripe.Product.retrieve")
        self.addCleanup(retrieve_product_patcher.stop)
        self.retrieve_product_mock = retrieve_product_patcher.start()
Example #17
0
 def upcoming(cls, api_key=None, stripe_version=None, stripe_account=None,
              **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     url = cls.class_url() + '/upcoming'
     response, api_key = requestor.request('get', url, params)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
 def _modify(cls, url, api_key=None, idempotency_key=None,
             stripe_version=None, stripe_account=None, **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     headers = util.populate_headers(idempotency_key)
     response, api_key = requestor.request('post', url, params, headers)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
Example #19
0
 def create(cls, api_key=None, idempotency_key=None,
            stripe_version=None, stripe_account=None, **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     url = cls.class_url()
     headers = util.populate_headers(idempotency_key)
     response, api_key = requestor.request('post', url, params, headers)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
Example #20
0
 def nested_resource_request(cls, method, url, api_key=None,
                             idempotency_key=None, stripe_version=None,
                             stripe_account=None, **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     headers = util.populate_headers(idempotency_key)
     response, api_key = requestor.request(method, url, params, headers)
     return util.convert_to_stripe_object(response, api_key,
                                          stripe_version,
                                          stripe_account)
Example #21
0
    def request(self, method, url, params=None, headers=None):
        if params is None:
            params = self._retrieve_params
        requestor = api_requestor.APIRequestor(
            key=self.api_key, api_base=self.api_base(),
            api_version=self.stripe_version, account=self.stripe_account)
        response, api_key = requestor.request(method, url, params, headers)

        return util.convert_to_stripe_object(response, api_key,
                                             self.stripe_version,
                                             self.stripe_account)
Example #22
0
 def upcoming(
     cls, api_key=None, stripe_version=None, stripe_account=None, **params
 ):
     requestor = api_requestor.APIRequestor(
         api_key, api_version=stripe_version, account=stripe_account
     )
     url = cls.class_url() + "/upcoming"
     response, api_key = requestor.request("get", url, params)
     return util.convert_to_stripe_object(
         response, api_key, stripe_version, stripe_account
     )
Example #23
0
    def request(self, method, url, params=None, headers=None):
        if params is None:
            params = self._retrieve_params
        requestor = api_requestor.APIRequestor(
            key=self.api_key, api_base=self.api_base(),
            api_version=self.stripe_version, account=self.stripe_account)
        response, api_key = requestor.request(method, url, params, headers)

        return util.convert_to_stripe_object(response, api_key,
                                             self.stripe_version,
                                             self.stripe_account)
 def _remove(cls,
             url,
             api_key=None,
             stripe_version=None,
             stripe_account=None,
             **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     response, api_key = requestor.request('delete', url, params)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
Example #25
0
 def upcoming(cls, api_key=None, stripe_version=None, stripe_account=None,
              **params):
     if "subscription_items" in params:
         items = util.convert_array_to_dict(params["subscription_items"])
         params["subscription_items"] = items
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     url = cls.class_url() + '/upcoming'
     response, api_key = requestor.request('get', url, params)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
 def preview(cls,
             api_key=None,
             stripe_version=None,
             stripe_account=None,
             **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     url = cls.class_url() + "/preview"
     response, api_key = requestor.request("get", url, params)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
Example #27
0
 def create(cls, api_key=None, api_version=None, stripe_account=None,
            **params):
     requestor = api_requestor.APIRequestor(
         api_key, api_base=stripe.upload_api_base, api_version=api_version,
         account=stripe_account)
     url = cls.class_url()
     supplied_headers = {
         "Content-Type": "multipart/form-data"
     }
     response, api_key = requestor.request(
         'post', url, params=params, headers=supplied_headers)
     return util.convert_to_stripe_object(response, api_key, api_version,
                                          stripe_account)
Example #28
0
    def setUp(self) -> None:
        with open(os.path.join(DIRECTORY, "fixtures/stripe_plan_test1.json")) as fh:
            plan = json.loads(fh.read())
        self.plan = convert_to_stripe_object(plan)

        self.plan_list = [self.plan]

        retrieve_plan_patcher = patch("stripe.Plan.retrieve")
        list_plan_patcher = patch("stripe.Plan.list")
        self.addCleanup(retrieve_plan_patcher.stop)
        self.addCleanup(list_plan_patcher.stop)
        self.retrieve_plan_mock = retrieve_plan_patcher.start()
        self.list_plan_mock = list_plan_patcher.start()
 def list(
     self, api_key=None, stripe_version=None, stripe_account=None, **params
 ):
     requestor = api_requestor.APIRequestor(
         api_key, api_version=stripe_version, account=stripe_account
     )
     url = self.get("url")
     response, api_key = requestor.request("get", url, params)
     stripe_object = util.convert_to_stripe_object(
         response, api_key, stripe_version, stripe_account
     )
     stripe_object._retrieve_params = params
     return stripe_object
Example #30
0
 def list(cls, api_key=None, stripe_version=None, stripe_account=None,
          **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_base=cls.api_base(),
                                            api_version=stripe_version,
                                            account=stripe_account)
     url = cls.class_url()
     response, api_key = requestor.request('get', url, params)
     stripe_object = util.convert_to_stripe_object(response, api_key,
                                                   stripe_version,
                                                   stripe_account)
     stripe_object._retrieve_params = params
     return stripe_object
Example #31
0
    def setUp(self) -> None:
        with open(os.path.join(DIRECTORY, "fixtures/stripe_in_test1.json")) as fh:
            invoice = json.loads(fh.read())
        self.invoice = convert_to_stripe_object(invoice)

        retrieve_invoice_patcher = patch("stripe.Invoice.retrieve")
        preview_invoice_patcher = patch("stripe.Invoice.upcoming")

        self.addCleanup(retrieve_invoice_patcher.stop)
        self.addCleanup(preview_invoice_patcher.stop)

        self.retrieve_invoice_mock = retrieve_invoice_patcher.start()
        self.preview_invoice_mock = preview_invoice_patcher.start()
 def list(cls, api_key=None, stripe_version=None, stripe_account=None,
          **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_base=cls.api_base(),
                                            api_version=stripe_version,
                                            account=stripe_account)
     url = cls.class_url()
     response, api_key = requestor.request('get', url, params)
     stripe_object = util.convert_to_stripe_object(response, api_key,
                                                   stripe_version,
                                                   stripe_account)
     stripe_object._retrieve_params = params
     return stripe_object
Example #33
0
    def test_process_payment_failure(self):
        event = {
            'httpMethod': 'POST', 
            'body': '{"payment_id":"pm_1HGTb2GPqNNATumTCzrTXZ9e"}'
            }
        
        response = {}            

        with mock.patch('stripe.PaymentIntent.create') as mock_process_payment:
            stripe_obj = convert_to_stripe_object(response)
            mock_process_payment.return_value= stripe_obj
            ret = process_payment.handler(event, "")

        self.assertEqual(ret["statusCode"], 400, self.msg_status_code.format(400, ret["statusCode"]))
Example #34
0
 def upcoming(cls,
              api_key=None,
              stripe_version=None,
              stripe_account=None,
              **params):
     if "subscription_items" in params:
         items = util.convert_array_to_dict(params["subscription_items"])
         params["subscription_items"] = items
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     url = cls.class_url() + '/upcoming'
     response, api_key = requestor.request('get', url, params)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
 async def _modify(cls,
                   url,
                   api_key=None,
                   idempotency_key=None,
                   stripe_version=None,
                   stripe_account=None,
                   **params):
     requestor = api_requestor.APIRequestor(api_key,
                                            api_version=stripe_version,
                                            account=stripe_account)
     headers = util.populate_headers(idempotency_key)
     response, api_key = await requestor.request("post", url, params,
                                                 headers)
     return util.convert_to_stripe_object(response, api_key, stripe_version,
                                          stripe_account)
Example #36
0
    def setUp(self) -> None:
        with open("src/hub/tests/unit/fixtures/stripe_sub_test_expanded.json") as fh:
            sub_test1 = json.loads(fh.read())
        self.subscription = convert_to_stripe_object(sub_test1)

        with open(
            "src/hub/tests/unit/fixtures/stripe_invoice_payment_succeeded_new_event.json"
        ) as fh:
            self.payment_succeeded_new_event = json.loads(fh.read())

        with open("src/hub/tests/unit/fixtures/stripe_in_test1.json") as fh:
            self.invoice = json.loads(fh.read())

        with open("src/hub/tests/unit/fixtures/stripe_sub_test4.json") as fh:
            self.subscription4 = json.loads(fh.read())

        with open("src/hub/tests/unit/fixtures/stripe_sub_test5.json") as fh:
            self.subscription5 = json.loads(fh.read())

        with open("src/hub/tests/unit/fixtures/stripe_sub_test6.json") as fh:
            self.subscription6 = json.loads(fh.read())

        with open("src/hub/tests/unit/fixtures/stripe_sub_test7.json") as fh:
            self.subscription7 = json.loads(fh.read())

        with open("src/hub/tests/unit/fixtures/stripe_sub_test8.json") as fh:
            self.subscription8 = json.loads(fh.read())

        with open("src/hub/tests/unit/fixtures/stripe_ch_test1.json") as fh:
            self.charge = json.loads(fh.read())

        subscription_patcher = mock.patch("stripe.Subscription.retrieve")
        invoice_patcher = mock.patch("stripe.Invoice.upcoming")
        invoice_retrieve_patcher = mock.patch("stripe.Invoice.retrieve")
        charge_retrieve_patcher = mock.patch("stripe.Charge.retrieve")
        run_pipeline_patcher = mock.patch("hub.routes.pipeline.RoutesPipeline.run")

        self.addCleanup(subscription_patcher.stop)
        self.addCleanup(invoice_patcher.stop)
        self.addCleanup(run_pipeline_patcher.stop)
        self.addCleanup(invoice_retrieve_patcher.stop)
        self.addCleanup(charge_retrieve_patcher.stop)

        self.mock_subscription = subscription_patcher.start()
        self.mock_invoice = invoice_patcher.start()
        self.mock_run_pipeline = run_pipeline_patcher.start()
        self.mock_retrieve_invoice = invoice_retrieve_patcher.start()
        self.mock_retrieve_charge = charge_retrieve_patcher.start()
 def retrieve(
     self,
     id,
     api_key=None,
     stripe_version=None,
     stripe_account=None,
     **params
 ):
     requestor = api_requestor.APIRequestor(
         api_key, api_version=stripe_version, account=stripe_account
     )
     url = "%s/%s" % (self.get("url"), quote_plus(util.utf8(id)))
     response, api_key = requestor.request("get", url, params)
     return util.convert_to_stripe_object(
         response, api_key, stripe_version, stripe_account
     )
Example #38
0
 def create(
     cls, api_key=None, api_version=None, stripe_account=None, **params
 ):
     requestor = api_requestor.APIRequestor(
         api_key,
         api_base=stripe.upload_api_base,
         api_version=api_version,
         account=stripe_account,
     )
     url = cls.class_url()
     supplied_headers = {"Content-Type": "multipart/form-data"}
     response, api_key = requestor.request(
         "post", url, params=params, headers=supplied_headers
     )
     return util.convert_to_stripe_object(
         response, api_key, api_version, stripe_account
     )
    def create(
        cls,
        api_key=None,
        idempotency_key=None,
        stripe_version=None,
        stripe_account=None,
        **params
    ):
        requestor = api_requestor.APIRequestor(
            api_key, api_version=stripe_version, account=stripe_account
        )
        url = cls.class_url()
        headers = util.populate_headers(idempotency_key)
        response, api_key = requestor.request("post", url, params, headers)

        return util.convert_to_stripe_object(
            response, api_key, stripe_version, stripe_account
        )
Example #40
0
    def create(
        cls,
        api_key=None,
        idempotency_key=None,
        stripe_version=None,
        stripe_account=None,
        **params
    ):
        if "subscription_item" not in params:
            raise ValueError("Params must have a subscription_item key")

        subscription_item = params.pop("subscription_item")

        requestor = api_requestor.APIRequestor(
            api_key, api_version=stripe_version, account=stripe_account
        )
        url = "/v1/subscription_items/%s/usage_records" % subscription_item
        headers = util.populate_headers(idempotency_key)
        response, api_key = requestor.request("post", url, params, headers)

        return util.convert_to_stripe_object(
            response, api_key, stripe_version, stripe_account
        )
Example #41
0
    def create(
        cls,
        api_key=None,
        idempotency_key=None,
        stripe_version=None,
        stripe_account=None,
        **params
    ):
        if stripe_version is None:
            raise ValueError(
                "stripe_version must be specified to create an ephemeral "
                "key"
            )

        requestor = api_requestor.APIRequestor(
            api_key, api_version=stripe_version, account=stripe_account
        )

        url = cls.class_url()
        headers = util.populate_headers(idempotency_key)
        response, api_key = requestor.request("post", url, params, headers)
        return util.convert_to_stripe_object(
            response, api_key, stripe_version, stripe_account
        )
Example #42
0
    def test_convert_to_stripe_object_and_back(self):
        resp = {
            "object": "balance",
            "available": [
                {
                    "amount": 234,
                    "currency": "usd",
                    "source_types": {"card": 234},
                }
            ],
            "livemode": False,
        }

        obj = util.convert_to_stripe_object(resp)
        assert type(obj) == stripe.Balance
        assert type(obj.available) == list
        assert type(obj.available[0]) == stripe.stripe_object.StripeObject

        d = util.convert_to_dict(obj)
        assert type(d) == dict
        assert type(d["available"]) == list
        assert type(d["available"][0]) == dict

        assert d == resp