Skip to content

Commit

Permalink
Initial Stripe extension, that implements the standar stripe checkout…
Browse files Browse the repository at this point in the history
… process, assuming an embedded form, and a given stripe price id.

Refactors `storage`, `handlersresponse`, `faucet` and `helpers` to  be new packages.
The min and max number of tokes is defined (should be paremtrized) and just aflat rate is allowed.
Adds the handlers:
- `/createCheckoutSession/{referral}/{to}`
- `/sessionStatus/{session_id}`
- /webhook"
and the following env vars:
-STRIPEKEY
-STRIPEPRICEID
-STRIPEWEBHOOKSECRET
-STRIPEMINQUANTITY
-STRIPEMAXQUANTITY
-STRIPEDEFAULTQUANTITY
  • Loading branch information
emmdim committed Jun 28, 2024
1 parent 08d5712 commit 0f4c4f7
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 115 deletions.
12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ DB_TYPE=pebble
BASE_ROUTE=/v2
# authentication types to use (comma separated). Available: open, oauth
AUTH=open
# stripe secret key
STRIPE_KEY=
# stripe price id
STRIPE_PRICE_ID=
# min number of tokens
STRIPEMINQUANTITY=
# max number of tokens
STRIPEMAXQUANTITY=
# default number of tokens
STRIPEDEFAULTQUANTITY=
# stripe webhook secret
STRIPE_WEBHOOK_SECRET=

RESTART=unless-stopped

Expand Down
48 changes: 0 additions & 48 deletions faucet.go

This file was deleted.

74 changes: 74 additions & 0 deletions faucet/faucet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package faucet

import (
"encoding/json"
"fmt"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/vocdoni/vocfaucet/storage"
"go.vocdoni.io/dvote/api"
vFaucet "go.vocdoni.io/dvote/api/faucet"
"go.vocdoni.io/dvote/crypto/ethereum"
"go.vocdoni.io/dvote/vochain"
)

type Faucet struct {
Signer *ethereum.SignKeys
AuthTypes map[string]uint64
WaitPeriod time.Duration
Storage *storage.Storage
}

// prepareFaucetPackage prepares a Faucet package, including the signature, for the given address.
// Returns the Faucet package as a marshaled json byte array, ready to be sent to the user.
func (f *Faucet) prepareFaucetPackage(toAddr common.Address, authTypeName string) (*vFaucet.FaucetResponse, error) {
// check if the auth type is supported
if _, ok := f.AuthTypes[authTypeName]; !ok {
return nil, fmt.Errorf("auth type %s not supported", authTypeName)
}

// generate Faucet package
fpackage, err := vochain.GenerateFaucetPackage(f.Signer, toAddr, f.AuthTypes[authTypeName])
if err != nil {
return nil, api.ErrCantGenerateFaucetPkg.WithErr(err)
}
fpackageBytes, err := json.Marshal(vFaucet.FaucetPackage{
FaucetPayload: fpackage.Payload,
Signature: fpackage.Signature,
})
if err != nil {
return nil, err
}
// send response
return &vFaucet.FaucetResponse{
Amount: fmt.Sprint(f.AuthTypes[authTypeName]),
FaucetPackage: fpackageBytes,
}, nil
}

// PrepareFaucetPackageWithAmount prepares a Faucet package, including the signature, for the given address.
// Returns the Faucet package as a marshaled json byte array, ready to be sent to the user.
func (f *Faucet) PrepareFaucetPackageWithAmount(toAddr common.Address, amount uint64) (*vFaucet.FaucetResponse, error) {
if amount == 0 {
return nil, fmt.Errorf("invalid requested amount: %d", amount)
}

// generate Faucet package
fpackage, err := vochain.GenerateFaucetPackage(f.Signer, toAddr, amount)
if err != nil {
return nil, api.ErrCantGenerateFaucetPkg.WithErr(err)
}
fpackageBytes, err := json.Marshal(vFaucet.FaucetPackage{
FaucetPayload: fpackage.Payload,
Signature: fpackage.Signature,
})
if err != nil {
return nil, err
}
// send response
return &vFaucet.FaucetResponse{
Amount: fmt.Sprint(amount),
FaucetPackage: fpackageBytes,
}, nil
}
Loading

0 comments on commit 0f4c4f7

Please sign in to comment.