Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pay with bat skus #1789

Merged
merged 29 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
18e6087
adding skus for pay with bat vpn premium
husobee Mar 13, 2023
41d97d4
handle radom webhooks for order purchases
husobee Mar 13, 2023
7238153
wip: update create order
husobee Mar 13, 2023
0073a15
correcting errors and linting, and making mocks/instrumented clients
husobee Mar 14, 2023
c5a73a0
fixing unused vars
husobee Mar 14, 2023
9db4f3b
typo
husobee Mar 14, 2023
6fc3482
merge up master
husobee May 22, 2023
0e0ef0d
merge up master
husobee Jul 12, 2023
00261cb
refactor to build correctly
husobee Jul 12, 2023
147ef55
remove unused variables
husobee Jul 12, 2023
12bd1ea
Merge remote-tracking branch 'origin/master' into patches-1789
pavelbrm Jul 14, 2023
51c23aa
Run go mod tidy
pavelbrm Jul 14, 2023
61710be
Clean up HandleRadomWebhook
pavelbrm Jul 14, 2023
8a2dc52
Clean up AppendOrderMetadataInt64 and update repository
pavelbrm Jul 14, 2023
97fa1fc
Update comments on flags
pavelbrm Jul 14, 2023
760fe1a
Update IsRadomPayable
pavelbrm Jul 14, 2023
20f0d07
Update Radom Client
pavelbrm Jul 14, 2023
fc972e2
Update CreateOrderFromRequest
pavelbrm Jul 14, 2023
7f076b6
Add tests for CreateRadomCheckoutSessionWithTime
pavelbrm Jul 14, 2023
3478b43
Add more tests
pavelbrm Jul 14, 2023
aef7a33
Merge pull request #1892 from brave-intl/patches-1789
husobee Jul 17, 2023
807d9eb
updates from feedback and webhook structure changes
husobee Jul 19, 2023
80aa597
linting issues
husobee Jul 19, 2023
97df680
peer review updates
husobee Jul 19, 2023
52fc187
review feedback
husobee Jul 19, 2023
240ffcf
Merge branch 'master' of git+ssh://github.com/brave-intl/bat-go into …
husobee Jul 24, 2023
5fd9b51
fix merge with master
husobee Aug 18, 2023
00e49a9
update go deps
husobee Aug 18, 2023
0506cea
Remove deleted code and refactor gateway setting
pavelbrm Aug 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions libs/clients/radom/instrumented.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package radom

import (
"context"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

type InstrumentedClient struct {
name string
cl *Client
vec *prometheus.SummaryVec
}

// newInstrucmentedClient returns an instance of the Client decorated with prometheus summary metric.
func newInstrucmentedClient(name string, cl *Client) *InstrumentedClient {
result := &InstrumentedClient{
name: name,
cl: cl,
vec: promauto.NewSummaryVec(prometheus.SummaryOpts{
Name: "client_duration_seconds",
Help: "client runtime duration and result",
MaxAge: time.Minute,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"instance_name", "method", "result"},
),
}

return result
}

func (_d *InstrumentedClient) CreateCheckoutSession(ctx context.Context, cp1 *CheckoutSessionRequest) (cp2 *CheckoutSessionResponse, err error) {
_since := time.Now()
defer func() {
result := "ok"
if err != nil {
result = "error"
}

_d.vec.WithLabelValues(_d.name, "CreateCheckoutSession", result).Observe(time.Since(_since).Seconds())
}()

return _d.cl.CreateCheckoutSession(ctx, cp1)
}
20 changes: 20 additions & 0 deletions libs/clients/radom/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package radom

import (
"context"
)

type MockClient struct {
FnCreateCheckoutSession func(ctx context.Context, req *CheckoutSessionRequest) (*CheckoutSessionResponse, error)
}

func (c *MockClient) CreateCheckoutSession(
ctx context.Context,
req *CheckoutSessionRequest,
) (*CheckoutSessionResponse, error) {
if c.FnCreateCheckoutSession == nil {
return &CheckoutSessionResponse{}, nil
}

return c.FnCreateCheckoutSession(ctx, req)
}
212 changes: 212 additions & 0 deletions libs/clients/radom/radom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package radom

import (
"context"
"crypto/subtle"
"errors"
"time"

"github.com/shopspring/decimal"

"github.com/brave-intl/bat-go/libs/clients"
appctx "github.com/brave-intl/bat-go/libs/context"
)

var (
ErrInvalidMetadataKey = errors.New("invalid metadata key")
)

// CheckoutSessionRequest represents a request to create a checkout session.
type CheckoutSessionRequest struct {
SuccessURL string `json:"successUrl"`
CancelURL string `json:"cancelUrl"`
Currency string `json:"currency"`
ExpiresAt int64 `json:"expiresAt"` // in unix seconds
husobee marked this conversation as resolved.
Show resolved Hide resolved
LineItems []LineItem `json:"lineItems"`
Metadata Metadata `json:"metadata"`
Customizations map[string]interface{} `json:"customizations"`
Total decimal.Decimal `json:"total"`
Gateway Gateway `json:"gateway"`
}

// Gateway provides access to managed services configurations
type Gateway struct {
Managed Managed `json:"managed"`
}

// Managed is the Radom managed services configuration
type Managed struct {
Methods []Method `json:"methods"`
}

// Method is a Radom payment method type
type Method struct {
Network string `json:"network"`
Token string `json:"token"`
}

// CheckoutSessionResponse represents the result of submission of a checkout session.
type CheckoutSessionResponse struct {
SessionID string `json:"checkoutSessionId"`
SessionURL string `json:"checkoutSessionUrl"`
}

// LineItem is a line item for a checkout session request.
type LineItem struct {
ProductID string `json:"productId"`
ItemData map[string]interface{} `json:"itemData"`
}

// Metadata represents metaadata in a checkout session request.
type Metadata []KeyValue

// Get allows returns a value based on the key from the Radom metadata.
func (m Metadata) Get(key string) (string, error) {
for _, v := range m {
if subtle.ConstantTimeCompare([]byte(key), []byte(v.Key)) == 1 {
return v.Value, nil
}
}

return "", ErrInvalidMetadataKey
}

// KeyValue represents a key-value metadata pair.
type KeyValue struct {
Key string `json:"key"`
Value string `json:"value"`
}

// AutomatedEVMSubscripton defines an automated subscription
type AutomatedEVMSubscription struct {
BuyerAddress string `json:"buyerAddress"`
SubscriptionContractAddress string `json:"subscriptionContractAddress"`
}

// Subscription is a radom subscription
type Subscription struct {
AutomatedEVMSubscription AutomatedEVMSubscription `json:"automatedEVMSubscription"`
}

// NewSubscriptionData provides details about the new subscription
type NewSubscriptionData struct {
SubscriptionID string `json:"subscriptionId"`
Subscription Subscription `json:"subscriptionType"`
Network string `json:"network"`
Token string `json:"token"`
Amount decimal.Decimal `json:"amount"`
Currency string `json:"currency"`
Period string `json:"period"`
PeriodCustomDuration string `json:"periodCustomDuration"`
CreatedAt *time.Time `json:"createdAt"`
Tags map[string]string `json:"tags"`
}

// Data is radom specific data attached to webhook calls
type Data struct {
CheckoutSession CheckoutSession `json:"checkoutSession"`
}

// CheckoutSession describes a radom checkout session
type CheckoutSession struct {
CheckoutSessionID string `json:"checkoutSessionId"`
Metadata Metadata `json:"metadata"`
}

// ManagedRecurringPayment provides details about the recurring payment from webhook
type ManagedRecurringPayment struct {
PaymentMethod Method `json:"paymentMethod"`
Amount decimal.Decimal `json:"amount"`
}

// EventData encapsulates the webhook event
type EventData struct {
ManagedRecurringPayment *ManagedRecurringPayment `json:"managedRecurringPayment"`
NewSubscription *NewSubscriptionData `json:"newSubscription"`
}

// WebhookRequest represents a radom webhook submission
type WebhookRequest struct {
EventType string `json:"eventType"`
EventData EventData `json:"eventData"`
Data Data `json:"radomData"`
}

// Client communicates with Radom.
type Client struct {
client *clients.SimpleHTTPClient
gwMethodsProd []Method
gwMethods []Method
}

// New returns a ready to use Client.
func New(srvURL, secret, proxyAddr string) (*Client, error) {
return newClient(srvURL, secret, proxyAddr)
}

func NewInstrumented(srvURL, secret, proxyAddr string) (*InstrumentedClient, error) {
cl, err := newClient(srvURL, secret, proxyAddr)
if err != nil {
return nil, err
}

return newInstrucmentedClient("radom_client", cl), nil
}

func newClient(srvURL, secret, proxyAddr string) (*Client, error) {
client, err := clients.NewWithProxy("radom", srvURL, secret, proxyAddr)
if err != nil {
return nil, err
}

result := &Client{
client: client,
gwMethodsProd: []Method{
{
Network: "Polygon",
Token: "0x3cef98bb43d732e2f285ee605a8158cde967d219",
},

{
Network: "Ethereum",
Token: "0x0d8775f648430679a709e98d2b0cb6250d2887ef",
},
},
gwMethods: []Method{
{
Network: "SepoliaTestnet",
Token: "0x5D684d37922dAf7Aa2013E65A22880a11C475e25",
},
{
Network: "PolygonTestnet",
Token: "0xd445cAAbb9eA6685D3A512439256866563a16E93",
},
},
}

return result, nil
}

// CreateCheckoutSession creates a Radom checkout session.
func (c *Client) CreateCheckoutSession(
ctx context.Context,
req *CheckoutSessionRequest,
) (*CheckoutSessionResponse, error) {
// Get the environment so we know what is acceptable chain/tokens.
methods := c.methodsForEnv(ctx)

req.Gateway = Gateway{
Managed: Managed{Methods: methods},
}

return nil, errors.New("not implemented")
}

func (c *Client) methodsForEnv(ctx context.Context) []Method {
env, ok := ctx.Value(appctx.EnvironmentCTXKey).(string)
if !ok || env != "production" {
return c.gwMethods
}

return c.gwMethodsProd
}
11 changes: 11 additions & 0 deletions libs/context/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ const (
// DisableBitflyerLinkingCTXKey - this informs if bitflyer linking is enabled
DisableBitflyerLinkingCTXKey CTXKey = "disable_bitflyer_linking"

// RadomWebhookSecretCTXKey - the webhook secret key for radom integration
RadomWebhookSecretCTXKey CTXKey = "radom_webhook_secret"
// RadomEnabledCTXKey - this informs if radom is enabled
RadomEnabledCTXKey CTXKey = "radom_enabled"
// RadomSellerAddressCTXKey is the seller address on radom
RadomSellerAddressCTXKey CTXKey = "radom_seller_address"
// RadomServerCTXKey is the server address on radom
RadomServerCTXKey CTXKey = "radom_server"
// RadomSecretCTXKey is the server secret on radom
RadomSecretCTXKey CTXKey = "radom_secret"

// stripe related keys

// StripeEnabledCTXKey - this informs if stripe is enabled
Expand Down
4 changes: 2 additions & 2 deletions main/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,14 @@ require (
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/api v0.114.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
Expand Down
8 changes: 4 additions & 4 deletions main/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1685,8 +1685,8 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -2053,8 +2053,8 @@ golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
1 change: 1 addition & 0 deletions services/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/shengdoushi/base58 v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
Expand Down
8 changes: 5 additions & 3 deletions services/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFu
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -1145,7 +1146,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.2 h1:aIihoIOHCiLZHxyoNQ+ABL4NKhFTgKLBdMLyEAh98m0=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand Down Expand Up @@ -1446,7 +1448,7 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1796,7 +1798,7 @@ golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
Loading
Loading