コード例 #1
0
def create_customer(
    subhub_account: SubHubAccount,
    user_id: str,
    email: str,
    source_token: str,
    origin_system: str,
    display_name: str,
) -> Customer:
    _validate_origin_system(origin_system)
    # First search Stripe to ensure we don't have an unlinked Stripe record
    # already in Stripe
    customer = None
    customers = Customer.list(email=email)
    for possible_customer in customers.data:
        if possible_customer.email == email:
            # If the userid doesn't match, the system is damaged.
            if possible_customer.metadata.get("userid") != user_id:
                raise ServerError("customer email exists but userid mismatch")

            customer = possible_customer
            # If we have a mis-match on the source_token, overwrite with the
            # new one.
            if customer.default_source != source_token:
                Customer.modify(customer.id, source=source_token)
            break

    # No existing Stripe customer, create one.
    if not customer:
        try:
            customer = Customer.create(
                source=source_token,
                email=email,
                description=user_id,
                name=display_name,
                metadata={"userid": user_id},
            )

        except InvalidRequestError as e:
            logger.error("create customer error", error=e)
            raise InvalidRequestError(
                message="Unable to create customer.", param=str(e)
            )
    # Link the Stripe customer to the origin system id
    db_account = subhub_account.new_user(
        uid=user_id, origin_system=origin_system, cust_id=customer.id
    )

    if not subhub_account.save_user(db_account):
        # Clean-up the Stripe customer record since we can't link it
        Customer.delete(customer.id)
        e = IntermittentError("error saving db record")
        logger.error("unable to save user or link it", error=e)
        raise e
    return customer
コード例 #2
0
ファイル: vendor.py プロジェクト: thelonewolff/subhub
def get_customer_list(email: str) -> Optional[List[Customer]]:
    try:
        customer_list = Customer.list(email=email)
        return customer_list
    except (
            APIConnectionError,
            APIError,
            RateLimitError,
            StripeErrorWithParamCode,
    ) as e:
        logger.error("get customer list error", error=e)
        raise e