Преглед на файлове

Setup invoice.paid webhook handler

master
Immanuel Onyeka преди 8 месеца
родител
ревизия
4511f2cb39
променени са 2 файла, в които са добавени 27 реда и са изтрити 5 реда
  1. +1
    -1
      migrations/0_29092022_setup_tables.sql
  2. +26
    -4
      skouter.go

+ 1
- 1
migrations/0_29092022_setup_tables.sql Целия файл

@@ -64,7 +64,7 @@ CREATE TABLE subscription (
user_id INT,
customer_id VARCHAR(255) NOT NULL,
price_id VARCHAR(255) NOT NULL,
// Key used by stripejs
/* Key used by stripejs */
client_secret VARCHAR(255) NOT NULL,
payment_status VARCHAR(50) NOT NULL,
current_period_end INT DEFAULT 0,


+ 26
- 4
skouter.go Целия файл

@@ -233,6 +233,11 @@ type Password struct {

type Endpoint func(http.ResponseWriter, *sql.DB, *http.Request)

type HookKeys struct {
InvoicePaid string
InvoiceFailed string
}

var (
regexen = make(map[string]*regexp.Regexp)
relock sync.Mutex
@@ -275,6 +280,11 @@ var feeTypes = []string{
"Other",
}

var hookKeys = HookKeys{
InvoicePaid: "",
InvoiceFailed: "",
}

var standardPriceId = "price_1OZLK9BPMoXn2pf9kuTAf8rs"

// Used to validate claim in JWT token body. Checks if user id is greater than
@@ -3036,15 +3046,23 @@ func invoicePaid(w http.ResponseWriter, db *sql.DB, r *http.Request) {
return
}
event, err := webhook.ConstructEvent(b, r.Header.Get("Stripe-Signature"),
os.Getenv("STRIPE_SECRET_KEY"))
event, err := webhook.ConstructEvent(b,
r.Header.Get("Stripe-Signature"),
hookKeys.InvoicePaid)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("webhook.ConstructEvent: %v", err)
return
}

log.Println(event.Data)
w.WriteHeader(http.StatusOK)
if event.Type != "invoice.paid" {
log.Println("Invalid event type sent to invoice-paid.")
return
}
log.Println(event.Type)
// json.Unmarshal(event.Data.Raw, &
}

func invoiceFailed(w http.ResponseWriter, db *sql.DB, r *http.Request) {
@@ -3523,6 +3541,10 @@ func dev(args []string) {
os.Setenv("DBPass", "test123")
stripe.Key = os.Getenv("STRIPE_SECRET_KEY")
standardPriceId = "price_1OZLK9BPMoXn2pf9kuTAf8rs"
hookKeys = HookKeys{
InvoicePaid: os.Getenv("DEV_WEBHOOK_KEY"),
InvoiceFailed: os.Getenv("DEV_WEBHOOK_KEY"),
}

db, err := sql.Open("mysql",
fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/%s?multiStatements=true",


Loading…
Отказ
Запис