-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added stripe checkout webhook and recognizing paid user
- Loading branch information
Showing
7 changed files
with
135 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module go-spotify/CloudFunctions/StripeCheckout | ||
|
||
go 1.13 | ||
|
||
require ( | ||
cloud.google.com/go/firestore v1.1.1 | ||
firebase.google.com/go v3.12.0+incompatible | ||
github.com/chew-z/spotify v0.0.21 // indirect | ||
github.com/coreos/go-oidc v2.2.1+incompatible // indirect | ||
github.com/gin-contrib/sessions v0.0.3 // indirect | ||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect | ||
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect | ||
github.com/stretchr/testify v1.5.0 // indirect | ||
github.com/stripe/stripe-go v68.20.0+incompatible | ||
github.com/zmb3/spotify v0.0.0-20200112163645-71a4c67d18db // indirect | ||
gopkg.in/square/go-jose.v2 v2.4.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package stripecheckout | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"cloud.google.com/go/firestore" | ||
firebase "firebase.google.com/go" | ||
"github.com/stripe/stripe-go/webhook" | ||
) | ||
|
||
var ( | ||
firestoreClient *firestore.Client | ||
projectID = os.Getenv("GOOGLE_CLOUD_PROJECT") | ||
) | ||
|
||
func main() { | ||
} | ||
func init() { | ||
// Use the application default credentials. | ||
conf := &firebase.Config{ProjectID: projectID} | ||
// Use context.Background() because the app/client should persist across | ||
// invocations. | ||
ctx := context.Background() | ||
app, err := firebase.NewApp(ctx, conf) | ||
if err != nil { | ||
log.Panicf("firebase.NewApp: %v", err) | ||
} | ||
firestoreClient, err = app.Firestore(ctx) | ||
if err != nil { | ||
log.Fatalf("app.Firestore: %v", err) | ||
} | ||
} | ||
|
||
/*StripeCheckout - ... | ||
*/ | ||
func StripeCheckout(w http.ResponseWriter, r *http.Request) { | ||
defer firestoreClient.Close() | ||
|
||
b, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
log.Printf("ioutil.ReadAll: %v", err) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
event, err := webhook.ConstructEvent(b, r.Header.Get("Stripe-Signature"), os.Getenv("STRIPE_WEBHOOK_SECRET")) | ||
if err != nil { | ||
log.Printf("webhook.ConstructEvent: %s", err.Error()) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if event.Type != "checkout.session.completed" { | ||
response := fmt.Sprintf("Wrong event type. Expected: checkout.session.completed") | ||
log.Println(response) | ||
http.Error(w, response, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
user := event.GetObjectValue("client_reference_id") | ||
if user != "" { | ||
ctx := context.Background() | ||
_, err = firestoreClient.Collection("users").Doc(user).Set(ctx, map[string]interface{}{ | ||
"premium_user": true, | ||
"subscription_expires": time.Now().AddDate(1, 0, 15), // a year plus two weeks plus a day | ||
}, firestore.MergeAll) | ||
} else { | ||
log.Println("user ID is missing!") | ||
} | ||
response := fmt.Sprintf("Checkout completed for %s user", user) | ||
log.Println(response) | ||
w.WriteHeader(http.StatusOK) | ||
if err := json.NewEncoder(w).Encode(&response); err != nil { | ||
log.Panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters