Skip to content

02. Payment API

Vincent Kok edited this page Mar 20, 2024 · 2 revisions

Creating a payment

using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
PaymentRequest paymentRequest = new PaymentRequest() {
    Amount = new Amount(Currency.EUR, 100.00m),
    Description = "Test payment of the example project",
    RedirectUrl = "http://google.com"
};

PaymentResponse paymentResponse = await paymentClient.CreatePaymentAsync(paymentRequest);
string checkoutUrl = paymentResponse.Links.Checkout.Href;

If you want to create a payment with a specific paymentmethod, there are seperate classes that allow you to set paymentmethod specific parameters. For example, a bank transfer payment allows you to set the billing e-mail and due date. Have a look at the Mollie create payment documentation for more information.

The full list of payment specific request classes is:

  • ApplePayPaymentRequest
  • BankTransferPaymentRequest
  • CreditCardPaymentRequest
  • GiftcardPaymentRequest
  • IdealPaymentRequest
  • KbcPaymentRequest
  • PayPalPaymentRequest
  • PaySafeCardPaymentRequest
  • Przelewy24PaymentRequest
  • SepaDirectDebitRequest

For example, if you'd want to create a bank transfer payment, you can instantiate a new BankTransferPaymentRequest:

using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
BankTransferPaymentRequest paymentRequest = new BankTransferPaymentRequest();
// Set bank transfer specific BillingEmail property
paymentRequest.BillingEmail = "{billingEmail}";
BankTransferPaymentResponse response = (BankTransferPaymentResponse)await paymentClient.CreatePaymentAsync(paymentRequest);

Redirecting a customer to the checkout link

Once you have created a payment, you can redirect the customer to the checkout link where he can do the actual payment. The PaymentResponse object contains a Links property that contains the checkout link.

using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
PaymentRequest paymentRequest = new PaymentRequest() {
    Amount = new Amount(Currency.EUR, 100.00m),
    Description = "Test payment of the example project",
    RedirectUrl = "http://google.com"
};

PaymentResponse paymentResponse = await paymentClient.CreatePaymentAsync(paymentRequest);
string checkoutUrl = paymentResponse.Links.Checkout.Href;

QR codes

Some payment methods also support QR codes. In order to retrieve a QR code, you have to set the includeQrCode parameter to true when sending the payment request. For example:

PaymentRequest paymentRequest = new PaymentRequest() {
	Amount = new Amount(Currency.EUR, 100.00m),
	Description = "Description",
	RedirectUrl = "http://www.mollie.com",
	Method = PaymentMethod.Ideal
};
using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
PaymentResponse result = await this._paymentClient.CreatePaymentAsync(paymentRequest, includeQrCode: true);
IdealPaymentResponse idealPaymentResult = result as IdealPaymentResponse;
IdealPaymentResponseDetails idealPaymentDetails = idealPaymentResult.Details;
string qrCode = idealPaymentDetails.QrCode.Src;

Passing multiple payment methods

It is also possible to pass multiple payment methods when creating a new payment. Mollie will then only show the payment methods you've specified when creating the payment request.

PaymentRequest paymentRequest = new PaymentRequest() {
	Amount = new Amount(Currency.EUR, 100.00m),
	Description = "Description",
	RedirectUrl = "http://www.mollie.com",
	Methods = new List<string>() {
		PaymentMethod.Ideal,
		PaymentMethod.CreditCard,
		PaymentMethod.DirectDebit
	}
};

Retrieving a payment by id

using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
PaymentResponse result = await paymentClient.GetPaymentAsync({paymentId});

Keep in mind that some payment methods have specific payment detail values. For example: PayPal payments have reference and customer reference properties. In order to access these properties you have to cast the PaymentResponse to the PayPalPaymentResponse and access the Detail property.

Take a look at the Mollie payment response documentation for a full list of payment methods that have extra detail fields.

The full list of payment specific response classes is:

  • BancontactPaymentResponse
  • BankTransferPaymentResponse
  • BelfiusPaymentResponse
  • CreditCardPaymentResponse
  • GiftcardPaymentResponse
  • IdealPaymentResponse
  • IngHomePayPaymentResponse
  • KbcPaymentResponse
  • PayPalPaymentResponse
  • PaySafeCardPaymentResponse
  • SepaDirectDebitResponse
  • SofortPaymentResponse
  • PointOfSalePaymentResponse

Updating a payment

Some properties of a payment can be updated after the payment has been created.

using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
PaymentUpdateRequest paymentUpdateRequest = new PaymentUpdateRequest() {
	Description = "Updated description",
	Metadata = "My metadata"
};
PaymentResponse updatedPayment = await this._paymentClient.UpdatePaymentAsync({paymentId}, paymentUpdateRequest);

Setting metadata

Mollie allows you to send any metadata you like in JSON notation and will save the data alongside the payment. When you fetch the payment with the API, Mollie will include the metadata. The library allows you to set the metadata JSON string manually, by setting the Metadata property of the PaymentRequest class, but the recommended way of setting/getting the metadata is to use the SetMetadata/Getmetadata methods.

For example:

// Custom metadata class that contains the data you want to include in the metadata class. 
CustomMetadataClass metadataRequest = new CustomMetadataClass() {
    OrderId = 1,
    Description = "{customDescription}"
};

// Create a new payment
PaymentRequest paymentRequest = new PaymentRequest() {
    Amount = new Amount(Currency.EUR, 100.00m),
    Description = "{description}",
    RedirectUrl = this.DefaultRedirectUrl,
};

// Set the metadata
paymentRequest.SetMetadata(metadataRequest);

// When we retrieve the payment response, we can convert our metadata back to our custom class
using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
PaymentResponse result = await paymentClient.CreatePaymentAsync(paymentRequest);
CustomMetadataClass metadataResponse = result.GetMetadata<CustomMetadataClass>();

Retrieving a list of payments

Mollie allows you to set offset and count properties so you can paginate the list. The offset and count parameters are optional. The maximum number of payments you can request in a single roundtrip is 250.

using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
ListResponse<PaymentResponse> response = await paymentClient.GetPaymentListAsync("{offset}", "{count}");

It is also possible to specify the sort direction when querying the list of payments. This allows you to paginate the list of payments in ascending or descending order.

using IPaymentClient paymentClient = new PaymentClient("{yourApiKey}");
ListResponse<PaymentResponse> response = await this._paymentClient.GetPaymentListAsync(from: "payment-id", sort: SortDirection.Asc);