Skip to main content

5-Minute Integration

Use this guide to accept mada, Visa, Mastercard, and American Express in minutes using Moyasar Form.

Before You Start

Before integration, prepare these requirements:

  1. Moyasar account and test mode

    • Create an account on the dashboard.
    • Start with test mode first so you can validate your flow safely before going live.
  2. API keys (important difference)

    • Copy your publishable key (pk_test_...) for frontend form initialization.
    • Keep your secret key (sk_test_...) for backend verification only.
    • Never expose sk_* in browser code, HTML, or mobile apps.
    • Learn more here: Get your API keys.
  3. Callback URL

    • Prepare a callback page on your website (for example: https://example.com/payment-result).
    • This page receives the payment id in the query string after redirect.
    • Your backend should use this id to fetch and verify payment details before confirming the order.
  4. Basic security checklist

    • Use HTTPS.
    • Verify payment server-side (status, amount, currency) and do not trust redirect alone.
    • Save payment IDs for reconciliation and support.

Step 1: Add Moyasar Form Assets

Place the following tags in your page <head>:

Step 2: Add Payment Container

HTML
<div class="mysr-form"></div>

Step 3: Initialize the Form

HTML
<script>
Moyasar.init({
element: '.mysr-form',
amount: 1000, // 10.00 SAR (smallest currency unit)
currency: 'SAR',
description: 'Order #1001',
publishable_api_key: 'pk_test_xxxxxxxxxxxxxxxxx',
callback_url: 'https://example.com/payment-result',
methods: ['creditcard'],
supported_networks: ['mada', 'visa', 'mastercard', 'amex'],
});
</script>

Step 4: Verify Payment on Callback

After redirect, read the id query parameter from your callback URL, then fetch the payment from your backend and verify:

  • status is paid
  • amount matches your order amount
  • currency matches your order currency

Use the payment API here: Fetch payment

Example backend verification:

const paymentId = req.query.id;

const response = await fetch(`https://api.moyasar.com/v1/payments/${paymentId}`, {
headers: {
Authorization: `Basic ${Buffer.from('sk_test_xxx:').toString('base64')}`,
},
});

const payment = await response.json();

if (payment.status === 'paid' && payment.amount === 1000 && payment.currency === 'SAR') {
// Mark order as paid
}

Go Live Checklist

  • Replace pk_test_... with your live key pk_live_...
  • Make sure your website runs on HTTPS
  • Never trust frontend redirect alone; always verify server-side
  • Store payment IDs in your order records for auditing and support