added google sheets
This commit is contained in:
parent
37ca207b92
commit
c4b06eb6c4
7 changed files with 304 additions and 28 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/stripe/stripe-go/v82"
|
||||
"github.com/stripe/stripe-go/v82/checkout/session"
|
||||
|
|
@ -24,6 +25,8 @@ func init() {
|
|||
log.Println("the url is: ", siteUrl)
|
||||
stripe.Key = os.Getenv("STRIPE_SECRET_KEY")
|
||||
webhookSecret = os.Getenv("WEBHOOK_SECRET")
|
||||
log.Println(stripe.Key)
|
||||
log.Println(webhookSecret)
|
||||
}
|
||||
|
||||
func startCheckout(w http.ResponseWriter, r *http.Request, o order) {
|
||||
|
|
@ -36,13 +39,14 @@ func startCheckout(w http.ResponseWriter, r *http.Request, o order) {
|
|||
Mode: stripe.String("payment"),
|
||||
ClientReferenceID: stripe.String(o.id),
|
||||
Locale: stripe.String("pl"),
|
||||
ExpiresAt: stripe.Int64(time.Now().Add(time.Hour).Unix()),
|
||||
LineItems: []*stripe.CheckoutSessionLineItemParams{{
|
||||
Quantity: stripe.Int64(1),
|
||||
PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
|
||||
Currency: stripe.String("pln"),
|
||||
UnitAmount: stripe.Int64(retainer),
|
||||
ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{
|
||||
Name: stripe.String("Zaliczka na zamówienie " + o.id),
|
||||
Name: stripe.String("Zaliczka na zamówienie nr " + o.id),
|
||||
},
|
||||
},
|
||||
}},
|
||||
|
|
@ -75,7 +79,10 @@ func WebhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if event.Type != "checkout.session.completed" && event.Type != "checkout.session.async_payment_succeeded" {
|
||||
switch event.Type {
|
||||
case "checkout.session.completed", "checkout.session.async_payment_succeeded",
|
||||
"checkout.session.expired", "checkout.session.async_payment_failed":
|
||||
default:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
|
@ -86,14 +93,22 @@ func WebhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case s.PaymentStatus != stripe.CheckoutSessionPaymentStatusPaid:
|
||||
log.Println("ORDER_NOT_YET_PAID", s.ClientReferenceID, s.ID, s.PaymentStatus)
|
||||
case s.AmountTotal != retainer || s.Currency != stripe.CurrencyPLN:
|
||||
log.Println("ORDER_PAYMENT_MISMATCH", s.ClientReferenceID, s.ID, s.AmountTotal, s.Currency)
|
||||
default:
|
||||
log.Println("ORDER_PAID", s.ClientReferenceID, s.ID)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
id := s.ClientReferenceID
|
||||
|
||||
switch {
|
||||
case event.Type == "checkout.session.expired" || event.Type == "checkout.session.async_payment_failed":
|
||||
log.Println("ORDER_ABANDONED", id, event.Type)
|
||||
if err := clearOrder(id); err != nil {
|
||||
log.Println("ORDER_SHEETS_ERROR", id, err)
|
||||
}
|
||||
case s.PaymentStatus != stripe.CheckoutSessionPaymentStatusPaid:
|
||||
log.Println("ORDER_NOT_YET_PAID", id, s.ID, s.PaymentStatus)
|
||||
case s.AmountTotal != retainer || s.Currency != stripe.CurrencyPLN:
|
||||
log.Println("ORDER_PAYMENT_MISMATCH", id, s.ID, s.AmountTotal, s.Currency)
|
||||
default:
|
||||
log.Println("ORDER_PAID", id, s.ID)
|
||||
if err := confirmRow(id); err != nil {
|
||||
log.Println("ORDER_SHEETS_ERROR", id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue