gr4vy

package module
v0.0.0-...-8ebdc79 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 17 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 := ioutil.ReadFile("privateKey.pem")
  if err != nil {
    fmt.Println(err)
    return
  }
  client := gr4vy.NewGr4vyClient("YOUR_GR4VY_ID", string(key))
  var params ListBuyersParams
  response, error := client.ListBuyers(params)
  
  if error != nil {
    fmt.Println(error)
    return
  }
  defer response.Body.Close()
  body, err := io.ReadAll(response.Body)
  if err != nil {
    fmt.Println(err)
  } else {
    fmt.Println(string(body))
  }
}

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 := map[string]string{"amount": "200", "currency": "USD", "buyer_id": "d757c76a-cbd7-4b56-95a3-40125b51b29c"}
  client := gr4vy.NewGr4vyClient("YOUR_GR4VY_ID", string(key))
  responseStr, 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.

  var req AddBuyerJSONRequestBody
  helper := string("Jane Smith")
  req.DisplayName = &helper
  response, err := client.AddBuyer(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  var p Buyer
  defer response.Body.Close()
  err = json.NewDecoder(response.Body).Decode(&p)
  if err != nil {
    fmt.Println(err)
    return
  }
  
  embed := map[string]string{"amount": "200", "currency": "USD", "buyer_id": *p.Id}
  
  client = gr4vy.NewGr4vyClient("YOUR_GR4VY_ID", string(key))
  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) and the private key.

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

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", string(key))

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:

  var params ListBuyersParams
  helper := int32(5)
  params.Limit = &helper  
  response, error := client.ListBuyers(params)

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

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

  var req AddBuyerJSONRequestBody
  helper := string("Jane Smith")
  req.DisplayName = &helper
  response, error := client.AddBuyer(req)

Similarly, to update a buyer you will need to pass in the UpdateBuyerJsonRequestBody to the UpdateBuyer method.

  var req UpdateBuyerJSONRequestBody
  helper := string("Jane Smith")
  req.DisplayName = &helper
  response, error := client.UpdateBuyer(buyer.id, req)

Response

Every resolved API call returns both a *http.Response object from the "net/http" package and an error object. where the response object contains a Body attribute with the parsed JSON body other attributes such as the HTTP response status code.

  response, error := client.ListBuyers(params)
  if error != nil {
    fmt.Println(error)
    return
  }

  defer response.Body.Close()
  body, err := io.ReadAll(response.Body)
  if err != nil {
    fmt.Println(err)
  } else {
    fmt.Println(string(body))
  }

Logging & Debugging

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

  client := gr4vy.NewGr4vyClient("YOUR_GR4VY_ID", string(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}

Available APIs

  • ListBuyers
  • ListBuyer

Development

Adding new APIs

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

  go get github.com/deepmap/oapi-codegen/cmd/oapi-codegen
  
  download https://raw.githubusercontent.com/gr4vy/gr4vy-openapi/sdks/openapi.v1.json
  
  oapi-codegen gr4vy-openapi.v1.json > api/gr4vy.gen.go
  ./openapi-generator-generate.sh

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

Publishing

Create a PR for your change to the master branch of github.com/gr4vy/gr4vy-go

License

This library is released under the MIT License.

Documentation

Index

Constants

View Source
const VERSION = "0.0.1"

Variables

This section is empty.

Functions

func GetClient

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

func GetKeyFromFile

func GetKeyFromFile(fileName string) ([]byte, error)

func JWKThumbprint

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

Types

type Gr4vyAddBuyer

type Gr4vyAddBuyer AddBuyerJSONRequestBody

type Gr4vyAddCardRule

type Gr4vyAddCardRule AddCardRuleJSONRequestBody

type Gr4vyAddPaymentService

type Gr4vyAddPaymentService AddPaymentServiceJSONRequestBody

type Gr4vyAuthorizeNewTransaction

type Gr4vyAuthorizeNewTransaction AuthorizeNewTransactionJSONRequestBody

type Gr4vyBuyer

type Gr4vyBuyer Buyer

type Gr4vyCaptureTransaction

type Gr4vyCaptureTransaction CaptureTransactionJSONRequestBody

type Gr4vyCardRuleNumberCondition

type Gr4vyCardRuleNumberCondition CardRuleNumberCondition

type Gr4vyCardRuleTextCondition

type Gr4vyCardRuleTextCondition CardRuleTextCondition

type Gr4vyClient

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

func NewGr4vyClient

func NewGr4vyClient(gr4vy_id string, private_key string) *Gr4vyClient

func NewGr4vyClientWithBaseUrl

func NewGr4vyClientWithBaseUrl(base_url string, private_key string) *Gr4vyClient

func (*Gr4vyClient) AddBuyer

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

func (*Gr4vyClient) AddCardRule

func (c *Gr4vyClient) AddCardRule(body Gr4vyAddCardRule) (*http.Response, error)

func (*Gr4vyClient) AddPaymentService

func (c *Gr4vyClient) AddPaymentService(body Gr4vyAddPaymentService) (*http.Response, error)

func (*Gr4vyClient) AuthorizeNewTransaction

func (c *Gr4vyClient) AuthorizeNewTransaction(body Gr4vyAuthorizeNewTransaction, pm Gr4vyPaymentMethod) (*http.Response, error)

func (*Gr4vyClient) AuthorizeTransaction

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

func (*Gr4vyClient) BaseUrl

func (c *Gr4vyClient) BaseUrl() string

func (*Gr4vyClient) CaptureTransaction

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

func (*Gr4vyClient) DeleteBuyer

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

func (*Gr4vyClient) DeleteCardRule

func (c *Gr4vyClient) DeleteCardRule(card_rule_id string) (*http.Response, error)

func (*Gr4vyClient) DeletePaymentMethod

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

func (*Gr4vyClient) DeletePaymentService

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

func (*Gr4vyClient) GetBuyer

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

func (*Gr4vyClient) GetCardRule

func (c *Gr4vyClient) GetCardRule(card_rule_id string) (*http.Response, error)

func (*Gr4vyClient) GetEmbedToken

func (c *Gr4vyClient) GetEmbedToken(params map[string]string) (string, error)

func (*Gr4vyClient) GetPaymentMethod

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

func (*Gr4vyClient) GetPaymentService

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

func (*Gr4vyClient) GetPaymentServiceDefinition

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

func (*Gr4vyClient) GetTransaction

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

func (*Gr4vyClient) HandleResponse

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

func (*Gr4vyClient) ListBuyers

func (c *Gr4vyClient) ListBuyers(params Gr4vyListBuyersParams) (*http.Response, error)

func (*Gr4vyClient) ListCardsRules

func (c *Gr4vyClient) ListCardsRules(params Gr4vyListCardsRulesParams) (*http.Response, error)

func (*Gr4vyClient) ListPaymentMethods

func (c *Gr4vyClient) ListPaymentMethods(params Gr4vyListPaymentMethodsParams) (*http.Response, error)

func (*Gr4vyClient) ListPaymentOptions

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

func (*Gr4vyClient) ListPaymentServiceDefinitions

func (c *Gr4vyClient) ListPaymentServiceDefinitions(params Gr4vyListPaymentServiceDefinitionsParams) (*http.Response, error)

func (*Gr4vyClient) ListPaymentServices

func (c *Gr4vyClient) ListPaymentServices(params Gr4vyListPaymentServicesParams) (*http.Response, error)

func (*Gr4vyClient) ListTransactions

func (c *Gr4vyClient) ListTransactions(params Gr4vyListTransactionsParams) (*http.Response, error)

func (*Gr4vyClient) RefundTransaction

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

func (*Gr4vyClient) StorePaymentMethod

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

func (*Gr4vyClient) UpdateBuyer

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

func (*Gr4vyClient) UpdateCardRule

func (c *Gr4vyClient) UpdateCardRule(card_rule_id string, body Gr4vyUpdateCardRule) (*http.Response, error)

func (*Gr4vyClient) UpdatePaymentService

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

type Gr4vyListBuyersParams

type Gr4vyListBuyersParams ListBuyersParams

type Gr4vyListCardsRulesParams

type Gr4vyListCardsRulesParams ListCardsRulesParams

type Gr4vyListPaymentMethodsParams

type Gr4vyListPaymentMethodsParams ListPaymentMethodsParams

type Gr4vyListPaymentOptionsParams

type Gr4vyListPaymentOptionsParams ListPaymentOptionsParams

type Gr4vyListPaymentServiceDefinitionsParams

type Gr4vyListPaymentServiceDefinitionsParams ListPaymentServiceDefinitionsParams

type Gr4vyListPaymentServicesParams

type Gr4vyListPaymentServicesParams ListPaymentServicesParams

type Gr4vyListTransactionsParams

type Gr4vyListTransactionsParams ListTransactionsParams

type Gr4vyPaymentMethod

type Gr4vyPaymentMethod CardRequest

type Gr4vyPaymentService

type Gr4vyPaymentService PaymentService

type Gr4vyPaymentServiceDefinition

type Gr4vyPaymentServiceDefinition PaymentServiceDefinition

type Gr4vyStorePaymentMethod

type Gr4vyStorePaymentMethod StorePaymentMethodJSONRequestBody

type Gr4vyUpdateBuyer

type Gr4vyUpdateBuyer UpdateBuyerJSONRequestBody

type Gr4vyUpdateCardRule

type Gr4vyUpdateCardRule UpdateCardRuleJSONRequestBody

type Gr4vyUpdatePaymentService

type Gr4vyUpdatePaymentService UpdatePaymentServiceJSONRequestBody

Directories

Path Synopsis
Package Openapi provides primitives to interact with the openapi HTTP API.
Package Openapi provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

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