coinpayments

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 22, 2022 License: MIT Imports: 14 Imported by: 0

README

coinpayments-go

Build codecov Scrutinizer Code Quality Go Report Card GitHub contributors GitHub license PkgGoDev

This package provides a generic go client template for the CoinPayments HTTP API

Installation

coinpayments-go is compatible with modern Go releases in module mode, with Go installed:

go get github.com/NdoleStudio/coinpayments-go

Alternatively the same can be achieved if you use import in a package:

import "github.com/NdoleStudio/coinpayments-go"

Implemented

  • Payments
    • create_transaction: Create Transaction

Usage

Initializing the Client

An instance of the client can be created using New().

package main

import (
	"github.com/NdoleStudio/coinpayments-go"
)

func main()  {
    client := coinpayments.New(
        coinpayments.WithAPIKey(/* API Key */),
        coinpayments.WithAPISecret(/* API Secret */),
    )
}
Error handling

All API calls return an error as the last return object. All successful calls will return a nil error.

status, response, err := client.Payments.CreatePayment(context.Background())
if err != nil {
    //handle error
}
Payments
create_transaction: Create Transaction
transaction, response, err := client.Payments.CreatePayment(context.Background(), &CreatePaymentRequest{
        Amount:           "1.00000000",
        OriginalCurrency: "USD",
        SendingCurrency:  "USD",
        BuyerEmail:       "john@example.com",
    }
)

if err != nil {
    log.Fatal(err)
}

log.Println(transaction.Error) // ok

Testing

You can run the unit tests for this client from the root directory using the command below:

go test -v

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Index

Constants

View Source
const (
	// HeaderNameHMAC is used for authentication
	HeaderNameHMAC = "HMAC"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Payment *paymentService
	// contains filtered or unexported fields
}

Client is the coinpayments API client. Do not instantiate this client with Client{}. Use the New method instead.

func New

func New(options ...Option) *Client

New creates and returns a new Client from a slice of Option.

func (*Client) IpnHMAC added in v0.0.3

func (client *Client) IpnHMAC(data string) (string, error)

IpnHMAC returns the hmac hash of the data

type CreatePaymentRequest

type CreatePaymentRequest struct {
	Amount           string `json:"amount"`
	OriginalCurrency string `json:"currency1"`
	SendingCurrency  string `json:"currency2"`
	BuyerEmail       string `json:"buyer_email"`
}

CreatePaymentRequest are the parameters for creating a payment

type CreatePaymentResponse

type CreatePaymentResponse struct {
	Error  string              `json:"error"`
	Result CreatePaymentResult `json:"result"`
}

CreatePaymentResponse is the api response after creating a payment

type CreatePaymentResult

type CreatePaymentResult struct {
	Amount         string `json:"amount"`
	Address        string `json:"address"`
	DestTag        string `json:"dest_tag"`
	TransactionID  string `json:"txn_id"`
	ConfirmsNeeded string `json:"confirms_needed"`
	Timeout        int    `json:"timeout"`
	CheckoutURL    string `json:"checkout_url"`
	StatusURL      string `json:"status_url"`
	QrcodeURL      string `json:"qrcode_url"`
}

CreatePaymentResult is the result of creating a payment

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is options for constructing a client

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey the coinpayments api key

func WithAPISecret

func WithAPISecret(apiSecret string) Option

WithAPISecret the coinpayments api secret

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL set's the base url for the coinpayments API

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient sets the underlying HTTP client used for API requests. By default, http.DefaultClient is used.

func WithIPNSecret added in v0.0.3

func WithIPNSecret(ipnSecret string) Option

WithIPNSecret the coinpayments IPN secret

type PaymentIpnRequest added in v0.0.2

type PaymentIpnRequest struct {
	Status           string `form:"status"`
	StatusText       string `form:"status_text"`
	TxnID            string `form:"txn_id"`
	Currency1        string `form:"currency1"`
	Currency2        string `form:"currency2"`
	Amount1          string `form:"amount1"`
	Amount2          string `form:"amount2"`
	Fee              string `form:"fee"`
	IpnType          string `form:"ipn_type"`
	BuyerName        string `form:"buyer_name"`
	Email            string `form:"email"`
	ItemName         string `form:"item_name"`
	ItemNumber       string `form:"item_number"`
	Invoice          string `form:"invoice"`
	Custom           string `form:"custom"`
	ReceivedAmount   string `form:"received_amount"`
	ReceivedConfirms string `form:"received_confirms"`
}

PaymentIpnRequest is the response we expect back from the server when the command is "api"

func (PaymentIpnRequest) IsComplete added in v0.0.3

func (request PaymentIpnRequest) IsComplete() bool

IsComplete returns true with the payment is completed

func (PaymentIpnRequest) IsFailed added in v0.0.3

func (request PaymentIpnRequest) IsFailed() bool

IsFailed returns ttrue when the payment is failed

func (PaymentIpnRequest) IsWaiting added in v0.0.3

func (request PaymentIpnRequest) IsWaiting() bool

IsWaiting returns true when the payment is in the waiting state

type PaymentTransaction added in v0.0.3

type PaymentTransaction struct {
	TimeCreated       int    `json:"time_created"`
	TimeExpires       int    `json:"time_expires"`
	Status            int    `json:"status"`
	StatusText        string `json:"status_text"`
	Type              string `json:"type"`
	Coin              string `json:"coin"`
	Amount            int    `json:"amount"`
	AmountFormatted   string `json:"amountf"`
	Received          int    `json:"received"`
	ReceivedFormatted string `json:"receivedf"`
	ReceiveConfirms   int    `json:"recv_confirms"`
	PaymentAddress    string `json:"payment_address"`
	TimeCompleted     int    `json:"time_completed"`
	SenderIP          string `json:"sender_ip"`
}

PaymentTransaction is the transaction details

type PaymentTransactionResponse added in v0.0.3

type PaymentTransactionResponse struct {
	Error  string             `json:"error"`
	Result PaymentTransaction `json:"result"`
}

PaymentTransactionResponse is the response gotten when we fetch a transaction

type Response

type Response struct {
	HTTPResponse *http.Response
	Body         *[]byte
}

Response captures the http response

func (*Response) Error

func (r *Response) Error() error

Error ensures that the response can be decoded into a string inc ase it's an error response

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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