coinbase

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2021 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Go Doc Release Go Report Card Build Status Sourcegraph TODOs

gdk

Getting Started

Most requests to the Commerce API must be authenticated with an API key. You can create an API key in your Settings page after creating a Coinbase Commerce account.

Installation

go get -v github.com/benalucorp/coinbase-commerce-go

Quick Start

package main

import (
	"context"
	"log"

	"github.com/benalucorp/coinbase-commerce-go"
	"github.com/benalucorp/coinbase-commerce-go/pkg/api"
	"github.com/benalucorp/coinbase-commerce-go/pkg/entity"
	"github.com/benalucorp/coinbase-commerce-go/pkg/enum"
)

func main() {
	client, err := coinbase.NewClient(api.Config{
		Key:              "REPLACE_WITH_YOUR_API_KEY",
		Timeout:          0,    // Default: 1 min.
		RetryCount:       0,    // Default: 0 = disable.
		RetryMaxWaitTime: 0,    // Default: 2 secs.
		Debug:            true, // Turn on on development for easier debugging.
	})
	if err != nil {
		log.Fatal(err)
	}

	// Create a charge.
	createResp, err := client.CreateCharge(context.Background(), &entity.CreateChargeReq{
		Name:        "The Sovereign Individual",
		Description: "Mastering the Transition to the Information Age",
		LocalPrice: entity.CreateChargePrice{
			Amount:   "100.00",
			Currency: "USD",
		},
		PricingType: enum.PricingTypeFixedPrice,
		Metadata: entity.CreateChargeMetadata{
			CustomerID:   "id_1005",
			CustomerName: "Satoshi Nakamoto",
		},
		RedirectURL: "https://charge/completed/page",
		CancelURL:   "https://charge/canceled/page",
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v", createResp)

	// Show a charge by providing either the code or id.
	showResp, err := client.ShowCharge(context.Background(), &entity.ShowChargeReq{
		ChargeCode: createResp.Data.Code,
		ChargeID:   "",
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v", showResp)
}

For more example check here.

Test Double / Stub

Sometime it's make sense to make an API call without actually calling the API. In order to support that this library has a built-in stub that can be triggered. You can enable stub by injecting certain value to the context data. You can also enforce that certain API call will always return error with specific type and message.

package main

import (
	"context"
	"log"

	"github.com/benalucorp/coinbase-commerce-go"
	"github.com/benalucorp/coinbase-commerce-go/pkg/api"
	"github.com/benalucorp/coinbase-commerce-go/pkg/entity"
	"github.com/benalucorp/coinbase-commerce-go/pkg/enum"
	"github.com/benalucorp/coinbase-commerce-go/pkg/api/stub"
)

func AlwaysSuccess(ctx context.Context, client *coinbase.Client) {
	// Enable stub that always success and return data.
	ctx = stub.Enable(ctx)

	// Call any client method.
	resp, err := client.CreateCharge(ctx, &entity.CreateChargeReq{})
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v", resp)
}

func AlwaysError(ctx context.Context, client *coinbase.Client) {
	// Enable stub that always error and return specific error.
	ctx = stub.SetErrDetailResp(context.Background(), entity.ErrDetailResp{
		Type:    "bad_request",
		Message: "stub: error triggered",
	})

	// Call any client method.
	resp, err := client.CreateCharge(ctx, &entity.CreateChargeReq{})
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v", resp)
}

Supported API

Version: 2018-03-22

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the main client to interact with Coinbase Commerce API.

func NewClient

func NewClient(cfg api.Config) (*Client, error)

NewClient creates a client to interact with Coinbase Commerce API.

func (Client) CancelCharge

func (c Client) CancelCharge(ctx context.Context, req *entity.CancelChargeReq) (*entity.CancelChargeResp, error)

CancelCharge cancels a charge that has been previously created. Supply the unique charge code or id that was returned when the charge was created. This information is also returned when a charge is first created.

Note: Only new charges can be successfully canceled. Once payment is detected, charge can no longer be canceled.

Reference: https://commerce.coinbase.com/docs/api/#cancel-a-charge

func (Client) CreateCharge

func (c Client) CreateCharge(ctx context.Context, req *entity.CreateChargeReq) (*entity.CreateChargeResp, error)

CreateCharge charge a customer with certain amount of currency. To get paid in cryptocurrency, you need to create a charge object and provide the user with a cryptocurrency address to which they must send cryptocurrency. Once a charge is created a customer must broadcast a payment to the blockchain before the charge expires. Reference: https://commerce.coinbase.com/docs/api/#create-a-charge

func (Client) CreateCheckout added in v0.0.3

func (c Client) CreateCheckout(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error)

CreateCheckout create a new checkout. Reference: https://commerce.coinbase.com/docs/api/#create-a-checkout

func (Client) DeleteCheckout added in v0.0.3

func (c Client) DeleteCheckout(ctx context.Context, req *entity.DeleteCheckoutReq) (*entity.DeleteCheckoutResp, error)

DeleteCheckout delete a checkout. Reference: https://commerce.coinbase.com/docs/api/#delete-a-checkout

func (Client) ListCharges

func (c Client) ListCharges(ctx context.Context, req *entity.ListChargesReq) (*entity.ListChargesResp, error)

ListCharges lists all the charges. Reference: https://commerce.coinbase.com/docs/api/#list-charges

func (Client) ListCheckouts added in v0.0.3

func (c Client) ListCheckouts(ctx context.Context, req *entity.ListCheckoutsReq) (*entity.ListCheckoutsResp, error)

ListCheckouts lists all the checkouts. Reference: https://commerce.coinbase.com/docs/api/#list-checkouts

func (Client) ResolveCharge

func (c Client) ResolveCharge(ctx context.Context, req *entity.ResolveChargeReq) (*entity.ResolveChargeResp, error)

ResolveCharge resolves a charge that has been previously marked as unresolved. Supply the unique charge code or id that was returned when the charge was created. This information is also returned when a charge is first created.

Note: Only unresolved charges can be successfully resolved

Reference: https://commerce.coinbase.com/docs/api/#resolve-a-charge

func (Client) ShowCharge

func (c Client) ShowCharge(ctx context.Context, req *entity.ShowChargeReq) (*entity.ShowChargeResp, error)

ShowCharge retrieves the details of a charge that has been previously created. Supply the unique charge code or id that was returned when the charge was created. This information is also returned when a charge is first created. Reference: https://commerce.coinbase.com/docs/api/#show-a-charge

func (Client) ShowCheckout added in v0.0.3

func (c Client) ShowCheckout(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error)

ShowCheckout show a single checkout. Reference: https://commerce.coinbase.com/docs/api/#show-a-checkout

func (Client) UpdateCheckout added in v0.0.3

func (c Client) UpdateCheckout(ctx context.Context, req *entity.UpdateCheckoutReq) (*entity.UpdateCheckoutResp, error)

UpdateCheckout update a checkout. Reference: https://commerce.coinbase.com/docs/api/#update-a-checkout

Directories

Path Synopsis
pkg
api

Jump to

Keyboard shortcuts

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