gr4vy

package module
v0.33.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 18 Imported by: 0

README

Gr4vy SDK for Go

Gr4vy provides any of your payment integrations through one unified API. For more details, visit gr4vy.com.

Installation

To add Gr4vy to your project, add the github.com/gr4vy/gr4vy-go package to your project.

go get github.com/gr4vy/gr4vy-go

Add import:

import "github.com/gr4vy/gr4vy-go"

Getting Started

To make your first API call, you will need to request a Gr4vy instance to be set up. Please contact our sales team for a demo.

Once you have been set up with a Gr4vy account you will need to head over to the Integrations panel and generate a private key. We recommend storing this key in a secure location but in this code sample we simply read the file from disk.

package main

import (
  "io"
  "io/ioutil"
  "fmt"
  "github.com/gr4vy/gr4vy-go"
)

func main() {
  key, err := gr4vy.GetKeyFromFile(PRIVATE_KEY_FILENAME)
  if err != nil {
    fmt.Println(err)
    return
  }
  client := gr4vy.NewGr4vyClient("demo", key, "sandbox")
  client.Debug = true

  var response *gr4vy.Gr4vyBuyers
  response, _, err = client.ListBuyers(gr4vy.Int32(2))
  if err != nil {
    fmt.Println(err.Error())
    return;
  }
  fmt.Printf("%+v\n", (*response.Items)[0].GetId())
}

Multi merchant

In a multi-merchant environment, the merchant account ID can be set by using NewGr4vyClientWithMid:

  client := gr4vy.NewGr4vyClientWithMid("demo", key, "sandbox", "my_merchant_account_id")

Gr4vy Embed

To create a token for Gr4vy Embed, call the client.GetEmbedToken(embed) function with the amount, currency, and optional buyer information for Gr4vy Embed.

embed := gr4vy.EmbedParams{
  Amount:   200,
  Currency: "USD",
  BuyerID:  "d757c76a-cbd7-4b56-95a3-40125b51b29c",
}
token, err = client.GetEmbedToken(embed)

You can now pass this token to your frontend where it can be used to authenticate Gr4vy Embed.

The buyer_id and/or buyer_external_identifier fields can be used to allow the token to pull in previously stored payment methods for a user. A buyer needs to be created before it can be used in this way.

  key, err := gr4vy.GetKeyFromFile(PRIVATE_KEY)
  if err != nil {
    fmt.Println(err)
    return
  }
  client := gr4vy.NewGr4vyClient("demo", key, "sandbox")
  client.Debug = true
  req := gr4vy.Gr4vyBuyerRequest{
    DisplayName: gr4vy.String("Jane Smith"),
  }
  var response *gr4vy.Gr4vyBuyer
  response, _, err = client.AddBuyer(req)
  if err != nil {
    fmt.Println(err)
    return
  }

  embed := gr4vy.EmbedParams{
    Amount:   200,
    Currency: "USD",
    BuyerID:  (*response.Id),
  }
  client = gr4vy.NewGr4vyClient("demo", key, "sandbox")
  client.Debug = true
  var responseStr string
  responseStr, err = client.GetEmbedToken(embed)

  if err != nil {
    fmt.Println(err)
    return;
  }

  fmt.Println("embed token: " + responseStr)

Initialization

The client can be initialized with the Gr4vy ID (gr4vyId), the private key string and the environment (sandbox or production).

  client := gr4vy.NewGr4vyClient("acme", key, "sandbox")

Alternatively, instead of the gr4vyId it can be initialized with the baseUrl of the server to use directly.

  client := gr4vy.NewGr4vyClientWithBaseUrl("https://api.acme.gr4vy.app", key, "sandbox")

Your API private key can be created in your admin panel on the Integrations tab.

Making API calls

This library conveniently maps every API path to a seperate function. For example, GET /buyers?limit=100 would be:

  response, _, error := client.ListBuyers(2)

To create, the API requires a request object for that resource that is conventiently named Gr4vy<Resource>Request. To update, the API requires a request object for that resource that is named Gr4vy<Resource>Update.

For example, to create a buyer you will need to pass a Gr4vyBuyerRequest object to the AddBuyer method.

  req := gr4vy.Gr4vyBuyerRequest{
    DisplayName: gr4vy.String("Jane Smith"),
  }
  response, _, error := client.AddBuyer(req)

So to update a buyer you will need to pass in the Gr4vyBuyerUpdate to the UpdateBuyer method.

  req := gr4vy.Gr4vyBuyerUpdate{
    DisplayName: gr4vy.String("Janet Smith"),
  }
  response, err := client.UpdateBuyer(buyerId, req)

Response

Every resolved API call returns the requested resource, a *http.Response object from the "net/http" package and an error object.

  var response *gr4vy.Gr4vyBuyers
  response, http, err = client.ListBuyers(gr4vy.Int32(2))
  if err != nil {
    fmt.Println(err.Error())
    return;
  }

Logging & Debugging

The SDK makes it easy possible to the requests and responses to the console.

  client := gr4vy.NewGr4vyClient("YOUR_GR4VY_ID", key)
  client.Debug = true

This will output the request parameters and response to the console as follows.

Gr4vy - Request - ListBuyers
Gr4vy - Response - {"items":[{"id":"b8433347-a16f-46b5-958f-d681876546a6","type":"buyer","display_name":"Jane Smith","external_identifier":null,"created_at":"2021-04-22T06:51:16.910297+00:00","updated_at":"2021-04-22T07:18:49.816242+00:00"}],"limit":1,"next_cursor":"fAA0YjY5NmU2My00NzY5LTQ2OGMtOTEyNC0xODVjMDdjZTY5MzEAMjAyMS0wNC0yMlQwNjozNTowNy4yNTMxMDY","previous_cursor":null}

Development

Adding new APIs

To add new APIs, run the following command to update the models and APIs based on the API spec.

./openapi-generator-generate.sh

Next, update sdk_<object_name>.go to bind any new APIs or remove any APIs that are no longer available.

Run the tests to ensure the changes do not break any existing tests.

go test -v
Publishing

Once the changes are merged, update the VERSION in gr4vy.go and push the changes to main to release a new version. Then tag that release as the current version in git.

License

This library is released under the MIT License.

Documentation

Index

Constants

View Source
const VERSION = "0.33.0"

Variables

This section is empty.

Functions

func Bool added in v0.2.1

func Bool(v bool) *bool

func GetClient

func GetClient(c *Gr4vyClient) (*APIClient, error)

func GetKeyFromFile

func GetKeyFromFile(fileName string) (string, error)

func Gr4vyNullableInt32 added in v0.26.0

func Gr4vyNullableInt32(v int32) NullableInt32

func Gr4vyNullableString added in v0.31.0

func Gr4vyNullableString(v string) NullableString

func Int32 added in v0.2.1

func Int32(v int32) *int32

func Int64 added in v0.2.1

func Int64(v int64) *int64

func JWKThumbprint

func JWKThumbprint(pub ecdsa.PublicKey) (string, error)

func String added in v0.2.1

func String(v string) NullableString

func StringPtr added in v0.2.1

func StringPtr(v string) *string

Types

type EmbedParams added in v0.6.0

type EmbedParams struct {
	Amount            int32                  `json:"amount"`
	Currency          string                 `json:"currency"`
	BuyerID           string                 `json:"buyer_id"`
	Metadata          map[string]string      `json:"metadata"`
	ConnectionOptions map[string]interface{} `json:"connection_options"`
	MerchantAccountId string                 `json:"merchant_account_id"`
}

type Gr4vyBuyer

type Gr4vyBuyer Buyer

type Gr4vyBuyerRequest added in v0.2.1

type Gr4vyBuyerRequest BuyerRequest

type Gr4vyBuyerUpdate added in v0.2.1

type Gr4vyBuyerUpdate BuyerUpdate

type Gr4vyBuyers added in v0.2.1

type Gr4vyBuyers Buyers

type Gr4vyCartItem added in v0.29.0

type Gr4vyCartItem = CartItem

type Gr4vyCheckoutSession added in v0.27.0

type Gr4vyCheckoutSession CheckoutSession

type Gr4vyCheckoutSessionCreateRequest added in v0.29.0

type Gr4vyCheckoutSessionCreateRequest CheckoutSessionCreateRequest

type Gr4vyCheckoutSessionRequest added in v0.27.0

type Gr4vyCheckoutSessionRequest CheckoutSessionRequest

type Gr4vyCheckoutSessionUpdateRequest added in v0.29.0

type Gr4vyCheckoutSessionUpdateRequest CheckoutSessionUpdateRequest

type Gr4vyClient

type Gr4vyClient struct {
	Debug bool
	// contains filtered or unexported fields
}

func NewGr4vyClient

func NewGr4vyClient(gr4vy_id string, private_key string, environment string) *Gr4vyClient

func NewGr4vyClientWithBaseUrl

func NewGr4vyClientWithBaseUrl(base_url string, private_key string) *Gr4vyClient

func NewGr4vyClientWithMid added in v0.24.0

func NewGr4vyClientWithMid(gr4vy_id string, private_key string, environment string, merchant_account_id string) *Gr4vyClient

func (*Gr4vyClient) AddBuyer

func (c *Gr4vyClient) AddBuyer(body Gr4vyBuyerRequest) (*Gr4vyBuyer, *http.Response, error)

func (*Gr4vyClient) AddBuyerContext added in v0.2.1

func (c *Gr4vyClient) AddBuyerContext(ctx context.Context, body Gr4vyBuyerRequest) (*Gr4vyBuyer, *http.Response, error)

func (*Gr4vyClient) AddCheckoutSession added in v0.27.0

func (*Gr4vyClient) AddPaymentServiceContext added in v0.2.1

func (*Gr4vyClient) AuthorizeNewTransactionContext added in v0.2.1

func (*Gr4vyClient) AuthorizeNewTransactionContextWithIdempotencyKey added in v0.22.0

func (c *Gr4vyClient) AuthorizeNewTransactionContextWithIdempotencyKey(ctx context.Context, body Gr4vyTransactionRequest, pm Gr4vyTransactionPaymentMethodRequest, ik string) (*Gr4vyTransaction, *http.Response, error)

func (*Gr4vyClient) AuthorizeNewTransactionWithIdempotencyKey added in v0.22.0

func (c *Gr4vyClient) AuthorizeNewTransactionWithIdempotencyKey(body Gr4vyTransactionRequest, pm Gr4vyTransactionPaymentMethodRequest, ik string) (*Gr4vyTransaction, *http.Response, error)

func (*Gr4vyClient) BaseUrl

func (c *Gr4vyClient) BaseUrl() string

func (*Gr4vyClient) CaptureTransaction

func (c *Gr4vyClient) CaptureTransaction(transaction_id string, body Gr4vyTransactionCaptureRequest) (*Gr4vyTransaction, *http.Response, error)

func (*Gr4vyClient) CaptureTransactionContext added in v0.2.1

func (c *Gr4vyClient) CaptureTransactionContext(ctx context.Context, transaction_id string, body Gr4vyTransactionCaptureRequest) (*Gr4vyTransaction, *http.Response, error)

func (*Gr4vyClient) DeleteBuyer

func (c *Gr4vyClient) DeleteBuyer(buyer_id string) (*http.Response, error)

func (*Gr4vyClient) DeleteBuyerContext added in v0.2.1

func (c *Gr4vyClient) DeleteBuyerContext(ctx context.Context, buyer_id string) (*http.Response, error)

func (*Gr4vyClient) DeleteCheckoutSession added in v0.29.0

func (c *Gr4vyClient) DeleteCheckoutSession(checkoutSessionId string) (*http.Response, error)

func (*Gr4vyClient) DeletePaymentMethod

func (c *Gr4vyClient) DeletePaymentMethod(payment_method_id string) (*http.Response, error)

func (*Gr4vyClient) DeletePaymentMethodContext added in v0.2.1

func (c *Gr4vyClient) DeletePaymentMethodContext(ctx context.Context, payment_method_id string) (*http.Response, error)

func (*Gr4vyClient) DeletePaymentService

func (c *Gr4vyClient) DeletePaymentService(payment_service_id string) (*http.Response, error)

func (*Gr4vyClient) DeletePaymentServiceContext added in v0.2.1

func (c *Gr4vyClient) DeletePaymentServiceContext(ctx context.Context, payment_service_id string) (*http.Response, error)

func (*Gr4vyClient) GetBuyer

func (c *Gr4vyClient) GetBuyer(buyer_id string) (*Gr4vyBuyer, *http.Response, error)

func (*Gr4vyClient) GetBuyerContext added in v0.2.1

func (c *Gr4vyClient) GetBuyerContext(ctx context.Context, buyer_id string) (*Gr4vyBuyer, *http.Response, error)

func (*Gr4vyClient) GetCheckoutSession added in v0.27.0

func (c *Gr4vyClient) GetCheckoutSession(checkout_session_id string) (*Gr4vyCheckoutSession, *http.Response, error)

func (*Gr4vyClient) GetEmbedToken

func (c *Gr4vyClient) GetEmbedToken(params EmbedParams, checkout_session_id string) (string, error)

func (*Gr4vyClient) GetEmbedTokenWithCheckoutSession added in v0.27.0

func (c *Gr4vyClient) GetEmbedTokenWithCheckoutSession(params EmbedParams) (string, CheckoutSession, error)

func (*Gr4vyClient) GetPaymentMethod

func (c *Gr4vyClient) GetPaymentMethod(payment_method_id string) (*Gr4vyPaymentMethod, *http.Response, error)

func (*Gr4vyClient) GetPaymentMethodContext added in v0.2.1

func (c *Gr4vyClient) GetPaymentMethodContext(ctx context.Context, payment_method_id string) (*Gr4vyPaymentMethod, *http.Response, error)

func (*Gr4vyClient) GetPaymentService

func (c *Gr4vyClient) GetPaymentService(payment_service_id string) (*Gr4vyPaymentService, *http.Response, error)

func (*Gr4vyClient) GetPaymentServiceContext added in v0.2.1

func (c *Gr4vyClient) GetPaymentServiceContext(ctx context.Context, payment_service_id string) (*Gr4vyPaymentService, *http.Response, error)

func (*Gr4vyClient) GetPaymentServiceDefinition

func (c *Gr4vyClient) GetPaymentServiceDefinition(payment_service_definition_id string) (*Gr4vyPaymentServiceDefinition, *http.Response, error)

func (*Gr4vyClient) GetPaymentServiceDefinitionContext added in v0.2.1

func (c *Gr4vyClient) GetPaymentServiceDefinitionContext(ctx context.Context, payment_service_definition_id string) (*Gr4vyPaymentServiceDefinition, *http.Response, error)

func (*Gr4vyClient) GetToken added in v0.2.1

func (c *Gr4vyClient) GetToken() (string, error)

func (*Gr4vyClient) GetTransaction

func (c *Gr4vyClient) GetTransaction(transaction_id string) (*Gr4vyTransaction, *http.Response, error)

func (*Gr4vyClient) GetTransactionContext added in v0.2.1

func (c *Gr4vyClient) GetTransactionContext(ctx context.Context, transaction_id string) (*Gr4vyTransaction, *http.Response, error)

func (*Gr4vyClient) HandleResponse

func (c *Gr4vyClient) HandleResponse(response *http.Response, error error)

func (*Gr4vyClient) ListBuyers

func (c *Gr4vyClient) ListBuyers(limit *int32) (*Gr4vyBuyers, *http.Response, error)

func (*Gr4vyClient) ListBuyersContext added in v0.2.1

func (c *Gr4vyClient) ListBuyersContext(ctx context.Context, limit *int32) (*Gr4vyBuyers, *http.Response, error)

func (*Gr4vyClient) ListPaymentMethods

func (c *Gr4vyClient) ListPaymentMethods(limit *int32) (*Gr4vyPaymentMethods, *http.Response, error)

func (*Gr4vyClient) ListPaymentMethodsContext added in v0.2.1

func (c *Gr4vyClient) ListPaymentMethodsContext(ctx context.Context, limit *int32) (*Gr4vyPaymentMethods, *http.Response, error)

func (*Gr4vyClient) ListPaymentOptions

func (c *Gr4vyClient) ListPaymentOptions() (*Gr4vyPaymentOptions, *http.Response, error)

func (*Gr4vyClient) ListPaymentOptionsContext added in v0.2.1

func (c *Gr4vyClient) ListPaymentOptionsContext(ctx context.Context) (*Gr4vyPaymentOptions, *http.Response, error)

func (*Gr4vyClient) ListPaymentServiceDefinitions

func (c *Gr4vyClient) ListPaymentServiceDefinitions(limit *int32) (*Gr4vyPaymentServiceDefinitions, *http.Response, error)

func (*Gr4vyClient) ListPaymentServiceDefinitionsContext added in v0.2.1

func (c *Gr4vyClient) ListPaymentServiceDefinitionsContext(ctx context.Context, limit *int32) (*Gr4vyPaymentServiceDefinitions, *http.Response, error)

func (*Gr4vyClient) ListPaymentServices

func (c *Gr4vyClient) ListPaymentServices(limit *int32) (*Gr4vyPaymentServices, *http.Response, error)

func (*Gr4vyClient) ListPaymentServicesContext added in v0.2.1

func (c *Gr4vyClient) ListPaymentServicesContext(ctx context.Context, limit *int32) (*Gr4vyPaymentServices, *http.Response, error)

func (*Gr4vyClient) ListTransactions

func (c *Gr4vyClient) ListTransactions(limit *int32) (*Gr4vyTransactions, *http.Response, error)

func (*Gr4vyClient) ListTransactionsContext added in v0.2.1

func (c *Gr4vyClient) ListTransactionsContext(ctx context.Context, limit *int32) (*Gr4vyTransactions, *http.Response, error)

func (*Gr4vyClient) PostListPaymentOptions added in v0.26.0

func (c *Gr4vyClient) PostListPaymentOptions(body Gr4vyPaymentOptionsRequest) (*Gr4vyPaymentOptions, *http.Response, error)

func (*Gr4vyClient) PostListPaymentOptionsContext added in v0.26.0

func (c *Gr4vyClient) PostListPaymentOptionsContext(ctx context.Context, body Gr4vyPaymentOptionsRequest) (*Gr4vyPaymentOptions, *http.Response, error)

func (*Gr4vyClient) RefundTransaction

func (c *Gr4vyClient) RefundTransaction(transaction_id string, body Gr4vyTransactionRefundRequest) (*Gr4vyRefund, *http.Response, error)

func (*Gr4vyClient) RefundTransactionContext added in v0.2.1

func (c *Gr4vyClient) RefundTransactionContext(ctx context.Context, transaction_id string, body Gr4vyTransactionRefundRequest) (*Gr4vyRefund, *http.Response, error)

func (*Gr4vyClient) StorePaymentMethod

func (c *Gr4vyClient) StorePaymentMethod(body Gr4vyPaymentMethodRequest) (*Gr4vyPaymentMethod, *http.Response, error)

func (*Gr4vyClient) StorePaymentMethodContext added in v0.2.1

func (c *Gr4vyClient) StorePaymentMethodContext(ctx context.Context, body Gr4vyPaymentMethodRequest) (*Gr4vyPaymentMethod, *http.Response, error)

func (*Gr4vyClient) UpdateBuyer

func (c *Gr4vyClient) UpdateBuyer(buyer_id string, body Gr4vyBuyerUpdate) (*Gr4vyBuyer, *http.Response, error)

func (*Gr4vyClient) UpdateBuyerContext added in v0.2.1

func (c *Gr4vyClient) UpdateBuyerContext(ctx context.Context, buyer_id string, body Gr4vyBuyerUpdate) (*Gr4vyBuyer, *http.Response, error)

func (*Gr4vyClient) UpdateCheckoutSession added in v0.29.0

func (c *Gr4vyClient) UpdateCheckoutSession(checkoutSessionId string, body Gr4vyCheckoutSessionUpdateRequest) (*Gr4vyCheckoutSession, *http.Response, error)

func (*Gr4vyClient) UpdatePaymentService

func (c *Gr4vyClient) UpdatePaymentService(payment_service_id string, body Gr4vyPaymentServiceUpdate) (*Gr4vyPaymentService, *http.Response, error)

func (*Gr4vyClient) UpdatePaymentServiceContext added in v0.2.1

func (c *Gr4vyClient) UpdatePaymentServiceContext(ctx context.Context, payment_service_id string, body Gr4vyPaymentServiceUpdate) (*Gr4vyPaymentService, *http.Response, error)

type Gr4vyPaymentMethod

type Gr4vyPaymentMethod PaymentMethod

type Gr4vyPaymentMethodRequest added in v0.2.1

type Gr4vyPaymentMethodRequest PaymentMethodRequest

type Gr4vyPaymentMethods added in v0.2.1

type Gr4vyPaymentMethods PaymentMethods

type Gr4vyPaymentOptions added in v0.2.1

type Gr4vyPaymentOptions PaymentOptions

type Gr4vyPaymentOptionsRequest added in v0.26.0

type Gr4vyPaymentOptionsRequest PaymentOptionsRequest

type Gr4vyPaymentService

type Gr4vyPaymentService PaymentService

type Gr4vyPaymentServiceDefinition

type Gr4vyPaymentServiceDefinition PaymentServiceDefinition

type Gr4vyPaymentServiceDefinitions added in v0.2.1

type Gr4vyPaymentServiceDefinitions PaymentServiceDefinitions

type Gr4vyPaymentServiceRequest added in v0.2.1

type Gr4vyPaymentServiceRequest PaymentServiceRequest

type Gr4vyPaymentServiceRequestFields added in v0.22.0

type Gr4vyPaymentServiceRequestFields PaymentServiceRequestFields

type Gr4vyPaymentServiceUpdate added in v0.2.1

type Gr4vyPaymentServiceUpdate PaymentServiceUpdate

type Gr4vyPaymentServices

type Gr4vyPaymentServices PaymentServices

type Gr4vyRefund added in v0.14.0

type Gr4vyRefund Refund

type Gr4vyTransaction

type Gr4vyTransaction Transaction

type Gr4vyTransactionCaptureRequest added in v0.2.1

type Gr4vyTransactionCaptureRequest TransactionCaptureRequest

type Gr4vyTransactionPaymentMethodRequest added in v0.2.1

type Gr4vyTransactionPaymentMethodRequest TransactionPaymentMethodRequest

type Gr4vyTransactionRefundRequest added in v0.20.0

type Gr4vyTransactionRefundRequest TransactionRefundRequest

type Gr4vyTransactionRequest added in v0.2.1

type Gr4vyTransactionRequest TransactionRequest

type Gr4vyTransactions added in v0.2.1

type Gr4vyTransactions Transactions

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL