Coder Social home page Coder Social logo

go-paddle's Introduction

go-paddle

go-paddle release (latest SemVer) Test Status

go-paddle is a Go client library for accessing the Paddle API.

Installation

go get github.com/Fakerr/go-paddle

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

import "github.com/Fakerr/go-paddle"

and run go get without paramters.

Usage

The package paddle comes with two different clients. A client for the Product, Subscription, Alert APIs that will require a vendor_id and a vendor_auth_code and a client for the Checkout API.

The services of a client divide the API into logical chunks and correspond to the structure of the Paddle API documentation at https://developer.paddle.com/api-reference/.

Product API, Subscription API, Alert API

import "github.com/Fakerr/go-paddle/paddle"

Construct a new Paddle client, then use the various services on the client to access different parts of the Paddle API. The client always requires a vendor_id and a vendor_auth_code arguments that you can get from the Paddle dashboard. For example:

client := paddle.NewClient(vendorId, vendorAuthCode, nil)

// List all users subscribed to any of your subscription plans
users, _, err := client.Users.List(context.Background(), nil)

Some API methods have optional parameters that can be passed. For example:

client := paddle.NewClient(vendorId, vendorAuthCode, nil)

// List all users subscribed to any of your subscription plans
opt := &UsersOptions{SubscriptionID: "1"}
users, _, err := client.Users.List(context.Background(), opt)

NOTE: Using the context package, one can easily pass cancelation signals and deadlines to various services of the client for handling a request. In case there is no context available, then context.Background() can be used as a starting point.

For more sample code snippets, head over to the example directory.

Checkout API

import "github.com/Fakerr/go-paddle/paddle"

Construct a new Paddle checkout client, then use the various services on the client to access different parts of the Paddle API.

client := paddle.NewCheckoutClient(nil)

// Retrieve prices for one or multiple products or plans
options := &PricesOptions{CustomerCountry: "tn"}
prices, _, err := client.Prices.Get(context.Background(), "1", options)

Sandbox environment

If you want to send requests against a sandbox environment, the package paddle provides two specific clients for that purpose:

client := paddle.NewSandboxClient(sandboxVendorId, sandboxVendorAuthCode, nil)

or to access the checkout API

client := paddle.NewSandboxCheckoutClient(nil)

Pagination

Some requests for resource collections (users, webhooks, etc.) support pagination. Pagination options are described in the paddle.ListOptions struct and passed to the list methods directly or as an embedded type of a more specific list options struct (for example paddle.UsersOptions).

client := paddle.NewClient(vendorId, vendorAuthCode, nil)

// List all users subscribed to any of your subscription plans
opt := &UsersOptions{
	SubscriptionID: "1",
	ListOptions: ListOptions{Page: 2},
}
users, _, err := client.Users.List(context.Background(), opt)

Webhooks

go-paddle comes with helper functions in order to facilitate the validatation and parsing of webhook events. For recognized event types, a value of the corresponding struct type will be returned.

Example usage:

func PaddleWebhookHandler(w http.ResponseWriter, r *http.Request) {
   payload, err := paddle.ValidatePayload(r, webhookSecretKey)
   if err != nil { ... }

   alert, err := paddle.ParsePayload(payload)
   if err != nil { ... }

   switch alert := alert.(type) {
   case *paddle.SubscriptionCreatedAlert:
       processSubscriptionCreatedAlert(alert)
   case *paddle.SubscriptionCanceledAlert:
       processSubscriptionCanceledAlert(alert)
   ...
   }
}

Todos

List of Paddle APIs that are not covered yet or are work in progress:

  • Licenses
  • Transactions

Contributing

Pull requests are welcome, along with any feedback or ideas. The calling pattern is pretty well established, so adding new methods is relatively straightforward.

License

This library is distributed under the MIT-style license found in the LICENSE file.

go-paddle's People

Contributors

fakerr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.