Skip to content

🦖 Gain easy powerful access to the PayRex API in Go.

License

Notifications You must be signed in to change notification settings

angelofallars/payrex-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

102 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PayRex-Go 🦖

GoDoc GitHub Workflow Status (with event) License Stars

The community-built PayRex Go SDK.

Installation

Use go get in your Go project to install the library:

go get -u github.com/angelofallars/payrex-go

Then import payrex-go:

import (
  "github.com/angelofallars/payrex-go"
)

Getting started

For full details on the API, see the PayRex API reference.

To check out all the capabilities of this library, view the Go package documentation.

Here are a few simple examples:

Customers

payrexClient := payrex.NewClient(apiKey)
params := &payrex.CustomerCreateParams{
	Name: "Juan Dela Cruz",
	Email: "jd.cruz@gmail.com",
	Currency: payrex.CurrencyPHP,
}

customer, err := payrexClient.Customers.Create(params)

The API key passed in to payrex.NewClient() should be the Secret API Key from the PayRex dashboard which starts with sk_.

PaymentIntents

payrexClient := payrex.NewClient(apiKey)
params := &payrex.PaymentIntentCreateParams{
	Amount:      100_00, // represents ₱100.00
	Currency:    payrex.CurrencyPHP,
	Description: payrex.NotNil("Dino Treat"),
	PaymentMethods: payrex.Slice(
		payrex.PaymentMethodGCash,
		payrex.PaymentMethodMaya,
	),
}

paymentIntent, err := payrexClient.PaymentIntents.Create(params)

Webhooks

// Enable all webhooks
payrexClient := payrex.NewClient(apiKey)

for webhook, err := range payrexClient.Webhooks.List(nil) {
  if err != nil {
    log.Fatal(err)
  }

  if webhook.Status == payrex.WebhookStatusDisabled {
    _, err = payrexClient.Webhooks.Enable(webhook.ID)
    if err != nil {
      log.Fatal(err)
    }
  }
}

Webhook signing

payrex-go can verify the webhook signatures of a webhook event delivery request, and also parse the request into a payrex.Event value. For more info, see the documentation for webhooks.

How webhook event processing works:

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/angelofallars/payrex-go"
)

var webhookSecretKey = os.Getenv("PAYREX_WEBHOOK_SECRET")

func handleWebhook(w http.ResponseWriter, r *http.Request) {
	event, err := payrex.ParseEvent(r, webhookSecretKey)
	if err != nil {
		w.WriteHeader(http.StatusUnauthorized)
		log.Println(err)
		return
	}

	switch event.ResourceType {
	case payrex.EventResourceTypeBillingStatement:
		billingStatement := event.MustBillingStatement()
		fmt.Printf("%+v\n", billingStatement)

	case payrex.EventResourceTypeCheckoutSession:
		checkoutSession := event.MustCheckoutSession()
		fmt.Printf("%+v\n", checkoutSession)

	case payrex.EventResourceTypePaymentIntent:
		paymentIntent := event.MustPaymentIntent()
		fmt.Printf("%+v\n", paymentIntent)

	case payrex.EventResourceTypePayout:
		payout := event.MustPayout()
		fmt.Printf("%+v\n", payout)

	case payrex.EventResourceTypeRefund:
		refund := event.MustRefund()
		fmt.Printf("%+v\n", refund)
	}
}

For more examples, see the payrex-go/example/ directory.

About

🦖 Gain easy powerful access to the PayRex API in Go.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages