示例#1
0
def fetch_customer(subhub_account: SubHubAccount, user_id: str) -> Customer:
    customer = None
    db_account = subhub_account.get_user(user_id)
    if db_account:
        customer = vendor.retrieve_stripe_customer(
            customer_id=db_account.cust_id)
    return customer
示例#2
0
def create_return_data(subscriptions) -> JsonDict:
    """
    Create json object subscriptions object
    :param subscriptions:
    :return: JSON data to be consumed by client.
    """
    return_data: Dict[str, Any] = {}
    return_data["subscriptions"] = []

    products = {}  # type: Dict
    for subscription in subscriptions["data"]:
        try:
            product = products[subscription["plan"]["product"]]
        except KeyError:
            product = vendor.retrieve_stripe_product(
                subscription["plan"]["product"])
            products[subscription["plan"]["product"]] = product

        plan_name = format_plan_nickname(
            product_name=product["name"],
            plan_interval=subscription["plan"]["interval"])

        if subscription["status"] == "incomplete":
            invoice = vendor.retrieve_stripe_invoice(
                subscription["latest_invoice"])
            if invoice["charge"]:
                intents = vendor.retrieve_stripe_customer(invoice["charge"])
                logger.debug("intents", intents=intents)

                return_data["subscriptions"].append({
                    "current_period_end":
                    subscription["current_period_end"],
                    "current_period_start":
                    subscription["current_period_start"],
                    "ended_at":
                    subscription["ended_at"],
                    "plan_name":
                    plan_name,
                    "plan_id":
                    subscription["plan"]["id"],
                    "status":
                    subscription["status"],
                    "subscription_id":
                    subscription["id"],
                    "cancel_at_period_end":
                    subscription["cancel_at_period_end"],
                    "failure_code":
                    intents["failure_code"],
                    "failure_message":
                    intents["failure_message"],
                })
                continue

        return_data["subscriptions"].append(
            create_subscription_object_without_failure(subscription,
                                                       plan_name))
    return return_data
示例#3
0
def create_update_data(customer) -> Dict[str, Any]:
    """
    Provide readable data for customer update to display
    :param customer:
    :return: return_data dict
    """
    payment_sources = customer["sources"]["data"]
    return_data: Dict[str, Any] = dict()
    return_data["subscriptions"] = []

    return_data["payment_type"] = ""
    return_data["last4"] = ""
    return_data["exp_month"] = ""
    return_data["exp_year"] = ""

    if len(payment_sources) > 0:
        first_payment_source = payment_sources[0]
        return_data["payment_type"] = first_payment_source.get("funding")
        return_data["last4"] = first_payment_source.get("last4")
        return_data["exp_month"] = first_payment_source.get("exp_month")
        return_data["exp_year"] = first_payment_source.get("exp_year")

    products = {}  # type: Dict
    for subscription in customer["subscriptions"]["data"]:
        try:
            product = products[subscription["plan"]["product"]]
        except KeyError:
            product = vendor.retrieve_stripe_product(
                subscription["plan"]["product"])
            products[subscription["plan"]["product"]] = product

        plan_name = format_plan_nickname(
            product_name=product["name"],
            plan_interval=subscription["plan"]["interval"])

        if subscription["status"] == "incomplete":
            invoice = vendor.retrieve_stripe_invoice(
                subscription["latest_invoice"])
            if invoice["charge"]:
                intents = vendor.retrieve_stripe_customer(invoice["charge"])
                intents = intents.to_dict()
                return_data["subscriptions"].append({
                    "current_period_end":
                    subscription["current_period_end"],
                    "current_period_start":
                    subscription["current_period_start"],
                    "ended_at":
                    subscription["ended_at"],
                    "plan_name":
                    plan_name,
                    "plan_id":
                    subscription["plan"]["id"],
                    "status":
                    subscription["status"],
                    "cancel_at_period_end":
                    subscription["cancel_at_period_end"],
                    "subscription_id":
                    subscription["id"],
                    "failure_code":
                    intents["failure_code"],
                    "failure_message":
                    intents["failure_message"],
                })
                continue

        return_data["cancel_at_period_end"] = subscription[
            "cancel_at_period_end"]
        return_data["subscriptions"].append(
            create_subscription_object_without_failure(subscription,
                                                       plan_name))

    return return_data
示例#4
0
def delete_customer(uid: str) -> FlaskResponse:
    """
    Delete an existing customer, cancel active subscriptions
    and delete from payment provider
    :param uid:
    :return: Success of failure message for the deletion
    """
    logger.info("delete customer", uid=uid)
    subscription_user = g.subhub_account.get_user(uid)
    logger.info("delete customer", subscription_user=subscription_user)
    if subscription_user is not None:
        origin = subscription_user.origin_system
        logger.info("delete origin", origin=origin)
        if not subscription_user:
            return dict(message="Customer does not exist."), 404
        subscribed_customer = vendor.retrieve_stripe_customer(
            subscription_user.cust_id)
        subscribed_customer = subscribed_customer.to_dict()
        subscription_info: List = []
        logger.info(
            "subscribed customer",
            subscribed_customer=subscribed_customer,
            data_type=type(subscribed_customer),
        )

        products = {}  # type: Dict
        for subs in subscribed_customer["subscriptions"]["data"]:
            try:
                product = products[subs.plan.product]
            except KeyError:
                product = Product.retrieve(subs.plan.product)
                products[subs.plan.product] = product
            plan_id = subs.plan.product

            sub = dict(
                plan_amount=subs.plan.amount,
                nickname=format_plan_nickname(subs.plan.nickname,
                                              subs.plan.interval),
                productId=plan_id,
                current_period_end=subs.current_period_end,
                current_period_start=subs.current_period_start,
                subscription_id=subs.id,
            )
            subscription_info.append(sub)
            vendor.cancel_stripe_subscription_immediately(
                subs.id, utils.get_indempotency_key())
            data = dict(
                uid=subscribed_customer["metadata"]["userid"],
                active=False,
                subscriptionId=subs.id,
                productId=plan_id,
                eventId=utils.get_indempotency_key(),
                eventCreatedAt=int(time.time()),
                messageCreatedAt=int(time.time()),
            )
            sns_message = Message(json.dumps(data)).route()
            logger.info("delete message", sns_message=sns_message)
        else:
            deleted_payment_customer = vendor.delete_stripe_customer(
                subscription_user.cust_id)
            if deleted_payment_customer:
                deleted_customer = delete_user(
                    user_id=subscribed_customer["metadata"]["userid"],
                    cust_id=subscribed_customer["id"],
                    origin_system=origin,
                    subscription_info=subscription_info,
                )
                user = g.subhub_account.get_user(uid)
                if deleted_customer and user is None:
                    return dict(message="Customer deleted successfully"), 200
    return dict(message="Customer not available"), 400
示例#5
0
    def test_retrieve_error(self):
        self.retrieve_customer_mock.side_effect = APIError("message")

        with self.assertRaises(APIError):
            vendor.retrieve_stripe_customer(customer_id="cust_123")
示例#6
0
    def test_retrieve_success(self):
        self.retrieve_customer_mock.side_effect = [APIError("message"), self.customer]

        customer = vendor.retrieve_stripe_customer(customer_id="cust_123")

        assert customer == self.customer  # nosec
示例#7
0
def test_retrieve_stripe_customer_error():
    disable_base()
    with pytest.raises(APIError):
        vendor.retrieve_stripe_customer("no_one")
    enable_base()