Custom Payments

Credit Cards

24min

Introduction

This guide allows advanced integrations with Moyasar by giving users a way to do custom implementations and designs.

This guide is only recommended for advanced users and if you prefer easy and straightforward integration we recommend. that you use the new Moyasar Payment Form

Overview

In this guide, we'll see how to set up a basic payment form to accept credit/debit card payments.

We will also ensure a successful integration by saving the payment ID before redirecting the user to their bank page, then verify if the payment was successful after their return.

Before Starting

Before you start the integration process, make sure you complete these steps:

  1. Sign up for a Moyasar test account at Moyasar's Dashboard.
  2. Get your API Key To authenticate your API request.

For more details about used APIs in this tutorial, please refer to Moyasar API Docs

Due to security considerations, you are not allowed to post user details to your own server. We will demonstrate how to save the payment ID before redirecting the user to their bank.



Integrate Your Payment Form

We need to create a simple HTML form having two different sets of input fields:

  • Hidden input fields are required to authenticate the payment request and provide the amount and currency.
  • Visible input fields for user-related data.

Here is the specification of the required data and fields to be included in the submission:

Step 1: Connect the HTML Payment Form

First, set your form’s action attribute to payment create endpoint URL, and set the method attribute to POST

HTML


You are not allowed to use any other action other than the one indicated earlier.



Step 2: Include Required Endpoint Fields

Next, we need to include some required hidden fields for making a payment, which are:

callback_url

After completing the transaction, your user will be redirected back to the specified URL.

  1. Add input of type hidden
  2. Set the input’s name to callback_url
  3. Set the value to the URL you want to redirect users to.
HTML


publishable_api_key

The publishable API key is used by Moyasar to identify you (the merchant) and complete the payment process. Including this key in public forms is safe, since it is only used to create payments and nothing else.

  1. Add an input of type hidden
  2. Set the input’s name to publishable_api_key
  3. Set the value to your publishable API key
HTML


amount

The amount you need to charge your user. It must be in the smallest currency unit. For example:

  • SAR (Saudi Riyal) has Halalah as its small unit (10 SAR = 1000 Halalah)
  • USD (US Dollar) has Cent as its small unit (10 USD = 1000 Cents)
  • KWD (Kuwaiti Dinar) has Fils as its small unit (10 KWD = 10000 Fils)
  • JPY (Japanese Yen) doesn't have a small unit, so use it as is.
  1. Add an input of type hidden
  2. Set the input’s name to the amount
  3. Set the value to the amount that you need to charge.
HTML


currency [optional]

The currency is optional and defaults to SAR.

  1. Add an input of type hidden
  2. Set the input’s name to currency
  3. Set the value to the amount that you need to charge.
HTML


source[type]

This is used to specify the payment method and should be in this case set to creditcard to accept payments in Visa, Mastercard, or Mada.

  1. Add input of type hidden
  2. Set the input’s name to source[type]
  3. Set the value to the type of payment method
HTML


description [optional]

This is an optional field where you can add any notes you might need like as a description to the payment object.

  1. Add input of type hidden
  2. Set the input’s name to description
  3. Set the value of the description
HTML



Step 3: Include Payment Details Fields

Finally, we need a few visible elements to allow users to enter their payment details. Here are the required fields for credit card payments:

  • source[name] for the card’s holder name.
  • source[number] for the card’s number.
  • source[month] for the card's expiry month.
  • source[year] for the card's expiry year.
  • source[cvc] for the card's CVC.


Final code

HTML


Submitting Form

The form can be submitted through the basic HTML form described above, or using Javascript which allows us to save the payment ID before redirecting the user.

The following code sample will show how to submit payment details to Moyasar while saving the ID before redirecting to the user's bank.

For this, we need to do the following:

  • Handle form's submit event and prevent default event handling.
  • Serialize form data.
  • Send a POST request to Moyasar using Javascript.
  • Handle the JSON response, then redirect the user to their bank's page.

jQuery Example

JS


Error handling is not shown in this example. In case of an error, Moyasar will return a 4xx status code. Please refer to the errors section in our API docs for more information about errors.



Verify the Payment

Once the user has completed the payment, they are redirected back to your website or app using the callback_url you provided earlier and the following HTTP query parameters will be appended:

  • id
  • status
  • message
URL


Fetch the payment using its ID through the API, and verify its status, amount, and currency before accepting or placing any user's purchase or, using the ID we have saved in the Javascript example, perform the same verifications.

Updated 15 Aug 2024
Did this page help you?