コード例 #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
ファイル: payments.py プロジェクト: SvanBoxel/subhub
def delete_customer(uid) -> FlaskResponse:
    """
    Delete an existing customer, cancel active subscriptions
    and delete from payment provider
    :param uid:
    :return: Success of failure message for the deletion
    """
    subscription_user = g.subhub_account.get_user(uid)
    if not subscription_user:
        return dict(message="Customer does not exist."), 404
    deleted_payment_customer = Customer.delete(subscription_user.cust_id)
    if deleted_payment_customer:
        deleted_customer = delete_user_from_db(uid)
        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
コード例 #3
0
ファイル: vendor.py プロジェクト: thelonewolff/subhub
def delete_stripe_customer(customer_id: str) -> Dict[str, Any]:
    """
    Delete a Stripe customer
    :param customer_id:
    :return: object
    """
    try:
        deleted_customer = Customer.delete(sid=customer_id)
        return deleted_customer
    except (
            InvalidRequestError,
            APIConnectionError,
            APIError,
            RateLimitError,
            StripeErrorWithParamCode,
    ) as e:
        logger.error("delete customer error", error=e)
        raise e