Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use proper expiry time in getorder endpoint #2612

Merged
merged 1 commit into from
Aug 9, 2024

Conversation

pavelbrm
Copy link
Contributor

@pavelbrm pavelbrm commented Jul 22, 2024

Summary

This PR fixes code that could update an order associated with Stripe purchases while getting the order. This could happen in a situation where:

  • either an existing session has expired, and a new had been created;
  • or when the existing or new session has been paid, which would then result in marking the order as paid, and setting it's expiry time as a consequence.

That last path is important because it used to extend an order by the hardcoded one month. This is wrong for a few reasons, mainly because:

  • it can be set precisely using the end of a subscription from Stripe;
  • it prevented us from sully supporting annual products in Stripe.

This PR also fixes a bug where a new session could be ignored to be checked for payment status.

This PR also refactors interactions with Stripe. The Stripe client code has been wrapped to enable testing. Some places interacting with Stripe have been updated and unified (such as session creation).

Additionally, this set of changes includes vast test coverage for the changed code (which was not covered before), as well as coverage for places which have not been possible to test before as well.

Type of Change

  • Product feature
  • Bug fix
  • Performance improvement
  • Refactor
  • Other

Tested Environments

  • Development
  • Staging
  • Production

Before Requesting Review

  • Does your code build cleanly without any errors or warnings?
  • Have you used auto closing keywords?
  • Have you added tests for new functionality?
  • Have validated query efficiency for new database queries?
  • Have documented new functionality in README or in comments?
  • Have you squashed all intermediate commits?
  • Is there a clear title that explains what the PR does?
  • Have you used intuitive function, variable and other naming?
  • Have you requested security and/or privacy review if needed
  • Have you performed a self review of this PR?

Manual Test Plan

@pavelbrm pavelbrm force-pushed the fix-order-expiry-03 branch 2 times, most recently from a2d6740 to 419d218 Compare July 23, 2024 10:41
@pavelbrm pavelbrm self-assigned this Jul 26, 2024
@pavelbrm pavelbrm changed the title fix: use proper expiry time when fixing up order while getting fix: use proper expiry time in getorder endpoint Jul 26, 2024
@pavelbrm pavelbrm force-pushed the fix-order-expiry-03 branch 9 times, most recently from 9d40f5a to 86686de Compare August 1, 2024 10:09
@pavelbrm pavelbrm force-pushed the fix-order-expiry-03 branch 2 times, most recently from f2afdfe to 77cba2f Compare August 6, 2024 11:03
@pavelbrm pavelbrm requested a review from clD11 August 6, 2024 11:21
@@ -0,0 +1,60 @@
package xstripe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Metadata: datastore.Metadata{
"stripeSubscriptionId": "wrong_sub_id",
},
ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

Copy link
Contributor

@clD11 clD11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, approved with some questions and minor nit

services/skus/service.go Outdated Show resolved Hide resolved
services/skus/service.go Show resolved Hide resolved
Copy link

github-actions bot commented Aug 9, 2024

[puLL-Merge] - brave-intl/bat-go@2612

Description

This PR refactors the Stripe integration in the SKUs service, improving code organization, testability, and error handling. It moves Stripe-related logic into a separate package, introduces a new stripeClient interface, and updates the order processing flow.

Changes

Changes

  1. services/skus/controllers.go:

    • Renamed GetOrder to handleGetOrder for consistency.
    • Updated error handling in handleGetOrder.
  2. services/skus/model/model.go:

    • Removed Stripe-specific methods and structs from the Order model.
    • Added new methods like StripeSessID() and IsStripe() to Order.
    • Added StripeItemID() method to OrderItem.
  3. services/skus/service.go:

    • Introduced stripeClient interface.
    • Refactored InitService to use the new Stripe client.
    • Updated CreateOrderFromRequest to remove Stripe-specific logic.
    • Implemented getTransformOrder and updateOrderStripeSession methods.
    • Added renewOrderStripe method for handling Stripe renewals.
    • Moved Stripe session creation logic to createStripeSession.
  4. services/skus/xstripe/xstripe.go:

    • Created a new package for Stripe-related functionality.
    • Implemented Client struct wrapping Stripe's API client.
    • Added methods for interacting with Stripe API (Session, CreateSession, Subscription, FindCustomer).
  5. services/skus/xstripe/mock.go:

    • Added a mock Stripe client for testing purposes.
  6. Various test files:

    • Updated existing tests and added new ones to cover the refactored code.

Possible Issues

  1. The refactoring may have introduced subtle changes in behavior that could affect existing integrations or workflows.
  2. The new error handling in handleGetOrder might return different status codes in some cases, which could impact API consumers.

Security Hotspots

No significant security issues were introduced in this refactoring. However, care should be taken to ensure that sensitive information (like Stripe API keys) is properly handled and not exposed in logs or error messages.

@pavelbrm pavelbrm merged commit 2193450 into master Aug 9, 2024
12 checks passed
@pavelbrm pavelbrm deleted the fix-order-expiry-03 branch August 9, 2024 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants