Skip to content

10 Payments examples

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

Examples for Payment resource operations

Create a new 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()

 response, payment, err := client.Payments.Create(ctx, createPayment(), &mollie.PaymentOptions{
  Include: []mollie.IncludeValue{mollie.IncludeQrCode},
 })
 if err != nil {
  log.Fatal(err)
 }

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

func createPayment() mollie.CreatePayment {
 return mollie.CreatePayment{
  Description:                     "example payment",
  RedirectURL:                     "https://example.com/redirect",
  CancelURL:                       "https://example.com/cancel",
  WebhookURL:                      "https://example.com/webhook",
  RestrictPaymentMethodsToCountry: "NL",
  Amount: &mollie.Amount{
   Currency: "EUR",
   Value:    "10.00",
  },
  Locale: mollie.English,
  Method: []mollie.PaymentMethod{mollie.DirectDebit, mollie.CreditCard},
 }
}

Update an existing 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()

 // Example id from an existing payment.
 response, payment, err := client.Payments.Update(ctx, "tr_example", updatePayment())
 if err != nil {
  log.Fatal(err)
 }

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

func updatePayment() mollie.UpdatePayment {
 return mollie.UpdatePayment{
  Description:                     "Updated example payment with new payment method.",
  RestrictPaymentMethodsToCountry: "NL",
  Method:                          mollie.GiftCard,
 }
}

Cancel 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()

 // Example id from an existing payment.
 response, payment, err := client.Payments.Cancel(ctx, "tr_example")
 if err != nil {
  log.Fatal(err)
 }

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

List payments

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()

 response, payments, err := client.Payments.List(ctx, &mollie.ListPaymentOptions{
  Embed:   []mollie.EmbedValue{mollie.EmbedCaptures},
  Include: []mollie.IncludeValue{mollie.IncludeQrCode},
 })
 if err != nil {
  log.Fatal(err)
 }

 log.Println(response.Body)

 for _, payment := range payments.Embedded.Payments {
  log.Println(payment)
 }
}