Skip to content

11 Captures examples

Victor Hugo Avelar Ossorio edited this page Mar 3, 2024 · 1 revision

Example for Captures resource operations

Create a new capture

package main

import (
 "context"
 "log"

 "github.com/VictorAvelar/mollie-api-go/v4/mollie"
)

func main() {
 config := mollie.NewAPITestingConfig(true)

 client, err := mollie.NewClient(nil, config)
 if err != nil {
  log.Fatal(err)
 }

 ctx := context.Background()
 paymentID := "tr_example_payment_id"

 response, capture, err := client.Captures.Create(ctx, paymentID, createCapture())
 if err != nil {
  log.Fatal(err)
 }

 log.Println(response.Body)
 log.Println(capture)
}

func createCapture() mollie.CreateCapture {
 return mollie.CreateCapture{
  Description: "example capture",
  Metadata:    map[string]any{"order_id": "1234"},
  Amount: &mollie.Amount{
   Value:    "20.00",
   Currency: "EUR",
  },
 }
}

List captures for a payment

package main

import (
 "context"
 "log"

 "github.com/VictorAvelar/mollie-api-go/v4/mollie"
)

func main() {
 config := mollie.NewAPITestingConfig(true)

 client, err := mollie.NewClient(nil, config)
 if err != nil {
  log.Fatal(err)
 }

 ctx := context.Background()
 paymentID := "tr_example_payment_id"

 response, captures, err := client.Captures.List(ctx, paymentID, &mollie.CaptureOptions{
  Embed: []mollie.EmbedValue{mollie.EmbedPayment},
 })
 if err != nil {
  log.Fatal(err)
 }

 log.Println(response.Body)

 for _, capture := range captures.Embedded.Captures {
  log.Println(capture)
 }
}