Categorygithub.com/krasun/paddle
modulepackage
0.0.10
Repository: https://github.com/krasun/paddle.git
Documentation: pkg.go.dev

# README

paddle

Build codecov Go Report Card GoDoc

A Paddle API client for Go.

Installation

go get github.com/krasun/paddle

Usage

Import the library:

import "github.com/krasun/paddle"

Calling API methods:

var paddleClient *paddle.Client
switch environment {
case "sandbox":
    paddleClient, err = paddle.NewSandboxClient(paddle.Authentication{paddleVendorID, paddleVendorAuthCode})
    if err != nil {
        log.Fatalf("failed to instantiate paddle %s client: %s", environment, err)
        return
    }
case "production":
    paddleClient, err = paddle.NewProductionClient(paddle.Authentication{paddleVendorID, paddleVendorAuthCode})
    if err != nil {
        log.Fatalf("failed to instantiate paddle %s client: %s", environment, err)
        return
    }
default:
    log.Fatalf("unsupported Paddle environment: %s", environment)
    return
}

options := &paddle.UpdateUserOptions{
    // ... 
    Prorate:         true,
    BillImmediately: true,
    // ...
}
response, _, err := paddleClient.Users.Update(ctx, options)
if err != nil {
    log.Error(err)
    return
}

Handling webhooks:

webhooks, err := paddle.NewWebhooks(paddlePublicKey)
if err != nil {
    log.Fatalf("failed to instantiate Paddle webhooks client: %s", err)
    return
}

func handlePaddleWebhooks(webhooks *paddle.Webhooks, payments *payments) func(http.ResponseWriter, *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {
        ctx := r.Context()

        alert, err := webhooks.ParseRequest(r)
        if err != nil {
            log.Error(err)
            http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError)
            return
        }

        switch alert := alert.(type) {
        // ... 
        case *paddle.SubscriptionCreatedAlert:
            err := payments.processSubscriptionCreated(ctx, alert)
            if err != nil {
                log.Error(err)
                http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError)
                return
            }
        case *paddle.SubscriptionUpdatedAlert:
            // ... 
            return
        case *paddle.SubscriptionCancelledAlert:
            // ... 
            return
        // ...
        }
    }
}

Tests

To run tests, just execute:

$ go test . 

License

paddle is released under the MIT license.

# Functions

NewProductionClient creates a new Paddle production client.
NewSandboxClient creates a new Paddle sandbox client.
NewWebhooks returns a new instance of the webhooks.

# Constants

RefundFull represents full refund.
RefundPartial represents partial refund.
RefundVAT represents VAT refund.
SubscriptionActive represents active subscription status.
SubscriptionDeleted represents deleted subscription status.
SubscriptionPastDue represents past due subscription status.
SubscriptionPaused represents paused subscription status.
SubscriptionTrialing represents trialing subscription status.

# Structs

APIError represents a Paddle API error.
Authentication represents credentials for working with the Paddle API.
CancelUserOptions represents options for cancel user subscription.
ChargeOptions represents options for charing.
ChargeResponse represents a response for the subscription charge.
Client is a Paddle client.
CreateModifierOptions represents options for creating a modifier.
CreateModifierResponse represents a response for the create subscription modifier.
ListUsersOptions represents options for getting subscription users.
OptionalTime represents the Time value that can be zero and forces you to process it differently.
PaymentInformation represents a user payment information.
SubscriptionCancelledAlert is triggered whenever a user cancel a subscription.
SubscriptionCreatedAlert is fired a new subscription is created, and a customer has successfully subscribed.
SubscriptionPaymentFailedAlert is fired when a payment for an existing subscription fails.
SubscriptionPaymentRefundedAlert is fired when a refund for an existing subscription is issued.
SubscriptionPaymentSucceededAlert is fired when a subscription payment is received successfully.
SubscriptionUpdatedAlert is fired when the plan, price, quantity, status of an existing subscription changes, or if the payment date is rescheduled manually.
UpdateUsersOptions represents options for update user subscription.
UpdateUserResponse represents a response for the update user subscription request.
User represents a Paddle user.
UserPayment represents a user payment.
Webhooks validates and parses webhook alerts.

# Type aliases

Charges is an API to work with the Paddle subscription one-off charges.
Modifiers is an API to work with the Paddle subscription modifiers.
RefundType represents refund type: full, vat or partial.
SubscriptionStatus represents subscription status: active, trialing, past due, paused or deleted.
Users is an API to work with the Paddle subscription users.