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

Stripe's fast_automatic_capture_beta API changes #74

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/api/v1/donate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var donationLock sync.Mutex

func getStripeInfo(c echo.Context) error {
return c.JSON(http.StatusOK, &stripeInfoReqponse{
Version: upstreamstripe.APIVersion,
Version: upstreamstripe.APIVersion + "; fast_automatic_capture_beta=v1",
PubKey: stripe.PublicKey,
DefaultCurrency: defaultCurrency,
Currencies: stripe.GetCurrencyMap(),
Expand Down Expand Up @@ -183,12 +183,14 @@ func handleStripeWebhook(c echo.Context) error {
return err
}
return handlePaymentSucceeded(c, event, &paymentIntent)
case "charge.updated":
fallthrough
case "charge.succeeded":
var charge upstreamstripe.Charge
if err := unmarshal(event, &charge); err != nil {
return err
}
return handleChargeSucceeded(c, event, &charge)
return handleChargeUpdated(c, event, &charge)
case "charge.refunded":
var refund upstreamstripe.Charge
if err := unmarshal(event, &refund); err != nil {
Expand Down Expand Up @@ -232,7 +234,23 @@ func handlePaymentSucceeded(c echo.Context, event *stripe.WebhookEvent, payment
return c.NoContent(http.StatusOK)
}

func handleChargeSucceeded(c echo.Context, event *stripe.WebhookEvent, charge *upstreamstripe.Charge) error {
func handleChargeUpdated(c echo.Context, event *stripe.WebhookEvent, charge *upstreamstripe.Charge) error {
// Filter for an event where the charge has succeeded and the balance_transaction is present
if charge.BalanceTransaction == nil {
// The new stripe fast_automatic_capture_beta doesn't necessarily include the balance_transaction until later,
// check if this is the event that added it to the charge or if we are handling too early.
return c.String(http.StatusUnprocessableEntity, "Cannot handle a charge without a balance_transaction")
}
if charge.Status != "succeeded" {
return c.String(http.StatusUnprocessableEntity, "Cannot handle a charge that has not succeeded")
}
if charge.Refunded {
return c.String(http.StatusUnprocessableEntity, "Cannot handle a charge that has been refunded")
}
if charge.TransferGroup != "" {
return c.String(http.StatusUnprocessableEntity, "Cannot handle a charge that has already been distributed")
Copy link
Contributor Author

@LeafHacker LeafHacker Apr 16, 2021

Choose a reason for hiding this comment

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

Is this reliable? This check assumes that calling transfer.New() with SourceTransaction set to the charge's ID will update the charge's transfer group.

This is intended as a way to check if our DistributeDonation func has already been run against this charge, since we don't want to run it twice for a given charge.

}

// Distribute charge amount between connected accounts
// We do this on charge succeeded instead of payment succeeded so we don't have to sort through successful and failed charges
err := stripe.DistributeDonation(charge)
Expand Down