From 16270379233bc70edb9f841fda2a4ed4287c1389 Mon Sep 17 00:00:00 2001 From: LeafHacker Date: Fri, 16 Apr 2021 10:57:45 +0100 Subject: [PATCH] Initial work on fast_automatic_capture_beta Stripe are asking for feedback on their fast_automatic_capture_beta. This commit attempts to update our code to support the new API. --- src/api/v1/donate.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/api/v1/donate.go b/src/api/v1/donate.go index 7487978..7573a7c 100644 --- a/src/api/v1/donate.go +++ b/src/api/v1/donate.go @@ -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(), @@ -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 { @@ -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") + } + // 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)