github.com/stripe/stripe-go/v76@v76.25.0/oauth/client.go (about)

     1  // Package oauth provides the OAuth APIs
     2  package oauth
     3  
     4  import (
     5  	"fmt"
     6  	"net/http"
     7  
     8  	stripe "github.com/stripe/stripe-go/v76"
     9  	"github.com/stripe/stripe-go/v76/form"
    10  )
    11  
    12  // Client is used to invoke /oauth and related APIs.
    13  type Client struct {
    14  	B   stripe.Backend
    15  	Key string
    16  }
    17  
    18  // AuthorizeURL builds an OAuth authorize URL.
    19  func AuthorizeURL(params *stripe.AuthorizeURLParams) string {
    20  	return getC().AuthorizeURL(params)
    21  }
    22  
    23  // AuthorizeURL builds an OAuth authorize URL.
    24  func (c Client) AuthorizeURL(params *stripe.AuthorizeURLParams) string {
    25  	express := ""
    26  	if stripe.BoolValue(params.Express) {
    27  		express = "/express"
    28  	}
    29  	qs := &form.Values{}
    30  	form.AppendTo(qs, params)
    31  	return fmt.Sprintf(
    32  		"%s%s/oauth/authorize?%s",
    33  		stripe.ConnectURL,
    34  		express,
    35  		qs.Encode(),
    36  	)
    37  }
    38  
    39  // New creates an OAuth token using a code after successful redirection back.
    40  func New(params *stripe.OAuthTokenParams) (*stripe.OAuthToken, error) {
    41  	return getC().New(params)
    42  }
    43  
    44  // New creates an OAuth token using a code after successful redirection back.
    45  func (c Client) New(params *stripe.OAuthTokenParams) (*stripe.OAuthToken, error) {
    46  	// client_secret is sent in the post body for this endpoint.
    47  	if stripe.StringValue(params.ClientSecret) == "" {
    48  		params.ClientSecret = stripe.String(stripe.Key)
    49  	}
    50  
    51  	oauthToken := &stripe.OAuthToken{}
    52  	err := c.B.Call(http.MethodPost, "/oauth/token", c.Key, params, oauthToken)
    53  
    54  	return oauthToken, err
    55  }
    56  
    57  // Del deauthorizes a connected account.
    58  func Del(params *stripe.DeauthorizeParams) (*stripe.Deauthorize, error) {
    59  	return getC().Del(params)
    60  }
    61  
    62  // Del deauthorizes a connected account.
    63  func (c Client) Del(params *stripe.DeauthorizeParams) (*stripe.Deauthorize, error) {
    64  	deauthorization := &stripe.Deauthorize{}
    65  	err := c.B.Call(
    66  		http.MethodPost,
    67  		"/oauth/deauthorize",
    68  		c.Key,
    69  		params,
    70  		deauthorization,
    71  	)
    72  	return deauthorization, err
    73  }
    74  
    75  func getC() Client {
    76  	return Client{stripe.GetBackend(stripe.ConnectBackend), stripe.Key}
    77  }