Ejemplo n.º 1
0
    def test_modify_error(self):
        self.modify_customer_mock.side_effect = APIError("message")

        with self.assertRaises(APIError):
            vendor.modify_customer(  # nosec
                customer_id="cust_123",
                source_token="token",
                idempotency_key=utils.get_indempotency_key(),
            )
Ejemplo n.º 2
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 = vendor.get_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:
                customer_message = "customer email exists but userid mismatch"
                raise ServerError(customer_message)

            customer = possible_customer
            # If we have a mis-match on the source_token, overwrite with the
            # new one.
            if customer.default_source != source_token:
                vendor.modify_customer(
                    customer_id=customer.id,
                    source_token=source_token,
                    idempotency_key=utils.get_indempotency_key(),
                )
            break

    # No existing Stripe customer, create one.
    if not customer:
        customer = vendor.create_stripe_customer(
            source_token=source_token,
            email=email,
            userid=user_id,
            name=display_name,
            idempotency_key=utils.get_indempotency_key(),
        )
    # 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)

    try:
        new_user = subhub_account.save_user(db_account)
        if not new_user:
            # Clean-up the Stripe customer record since we can't link it
            vendor.delete_stripe_customer(customer_id=customer.id)
    except IntermittentError("error saving db record") as e:  # type: ignore
        logger.error("unable to save user or link it", error=e)
        raise e
    return customer
Ejemplo n.º 3
0
def existing_payment_source(existing_customer: Customer,
                            source_token: str) -> Customer:
    """
    If Customer does not have an existing Payment Source and has not been Deleted:
        - Set the Customer's payment source to the new token
        - return the updated Customer
    Else
        - return the provided customer
    :param existing_customer:
    :param source_token:
    :return:
    """
    if not existing_customer.get("sources"):
        if not existing_customer.get("deleted"):
            existing_customer = vendor.modify_customer(
                customer_id=existing_customer["id"],
                source_token=source_token,
                idempotency_key=utils.get_indempotency_key(),
            )
            logger.info("add source", existing_customer=existing_customer)
        else:
            logger.info(
                "stripe customer is marked as deleted. cannot add source.")
    logger.debug("existing payment source",
                 existing_customer=existing_customer)
    return existing_customer
Ejemplo n.º 4
0
    def test_modify_success(self):
        self.modify_customer_mock.side_effect = [APIError("message"), self.customer]

        customer = vendor.modify_customer(  # nosec
            customer_id="cust_123",
            source_token="token",
            idempotency_key=utils.get_indempotency_key(),
        )

        assert customer == self.customer  # nosec
Ejemplo n.º 5
0
def update_payment_method(uid, data) -> FlaskResponse:
    """
    Given a user id and a payment token, update user's payment method
    :param uid:
    :param data:
    :return: Success or failure message.
    """
    customer = fetch_customer(g.subhub_account, uid)
    logger.debug("customer", customer=customer)
    if not customer:
        response_message = dict(message="Customer does not exist.")
        logger.debug(
            "update payment method",
            response_message=response_message,
            response_code=404,
        )
        return response_message, 404

    metadata = customer.get("metadata")
    logger.debug("metadata", metadata=metadata, customer=type(customer))
    if metadata:
        if metadata.get("userid", None) == uid:
            modify_customer(
                customer_id=customer.id,
                source_token=data["pmt_token"],
                idempotency_key=utils.get_indempotency_key(),
            )
            response_message = dict(
                message="Payment method updated successfully.")
            logger.debug(
                "update payment method",
                response_message=response_message,
                response_code=201,
            )
            return response_message, 201
    response_message = dict(message="Customer mismatch.")
    logger.debug("update payment method",
                 response_message=response_message,
                 response_code=400)
    return response_message, 400
Ejemplo n.º 6
0
def existing_payment_source(existing_customer: Customer,
                            source_token: str) -> Customer:
    if not existing_customer.get("sources"):
        if not existing_customer.get("deleted"):
            existing_customer = vendor.modify_customer(
                customer_id=existing_customer["id"],
                source_token=source_token,
                idempotency_key=utils.get_indempotency_key(),
            )
            logger.info("add source", existing_customer=existing_customer)
        else:
            logger.info("existing source deleted")
    return existing_customer
Ejemplo n.º 7
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 = search_customers(email=email, user_id=user_id)

    # If we have a mis-match on the source_token, overwrite with the
    # new one.
    if customer is not None and customer.default_source != source_token:
        customer = vendor.modify_customer(
            customer_id=customer.id,
            source_token=source_token,
            idempotency_key=utils.get_indempotency_key(),
        )

    # No existing Stripe customer, create one.
    if not customer:
        customer = vendor.create_stripe_customer(
            source_token=source_token,
            email=email,
            userid=user_id,
            name=display_name,
            idempotency_key=utils.get_indempotency_key(),
        )
    # 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)

    new_user = subhub_account.save_user(db_account)
    if not new_user:
        # Clean-up the Stripe customer record since we can't link it
        vendor.delete_stripe_customer(customer_id=customer.id)
        logger.error("unable to save user or link it")
        raise IntermittentError("error saving db record")

    logger.debug("create customer", customer=customer)
    return customer
Ejemplo n.º 8
0
def test_modify_customer_error():
    disable_base()
    with pytest.raises(APIError):
        vendor.modify_customer("no_customer", "tok_nothing",
                               utils.get_indempotency_key())
    enable_base()