Sample Request Code

Implementation examples in various programming languages

Payment Request Examples

Node.js Example

Using Axios for HTTP requests and crypto for hash generation

const axios = require('axios');
const crypto = require('crypto');

async function createPaymentRequest() {
  // Your EcobankPay merchant credentials
  const merchantKey = 'your-merchant-key';
  const secretKey = 'your-secret-key'; // Provided by EcobankPay

  // Order details
  const invoiceId = 'INV-' + Date.now(); // Generate a unique invoice ID
  const total = '100.00';
  const description = 'Payment for order #12345';

  // Customer information
  const customerName = 'John Doe';
  const customerEmail = 'john.doe@example.com';
  const customerPhone = '233201234567';

  // Callback URLs
  const successUrl = 'https://your-website.com/success';
  const cancelledUrl = 'https://your-website.com/cancel';
  const ipnUrl = 'https://your-website.com/ipn-notification';

  // Create secure hash
  const dataToHash = `invoice_id=${invoiceId}&merchant_key=${merchantKey}&total=${total}`;
  const secureHash = crypto.createHmac('sha256', secretKey)
                          .update(dataToHash)
                          .digest('hex');

  // Request payload
  const payload = {
    merchant_key: merchantKey,
    invoice_id: invoiceId,
    total: total,
    description: description,
    name: customerName,
    email: customerEmail,
    number: customerPhone,
    success_url: successUrl,
    cancelled_url: cancelledUrl,
    ipn_url: ipnUrl,
    generate_checkout_url: true,
    secure_hash: secureHash
  };

  try {
    // Send payment request to EcobankPay
    const response = await axios.post(
      'https://pgw.paywithonline.com/v1/mobile_agents_v2',
      payload,
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    );

    console.log('Payment request successful:');
    console.log(response.data);

    // If successful, redirect the customer to the checkout URL
    if (response.data.success) {
      console.log('Redirect customer to:', response.data.url);
      // In a web application, you would redirect the user:
      // res.redirect(response.data.url);
    }

    return response.data;
  } catch (error) {
    console.error('Error creating payment request:');
    if (error.response) {
      console.error(error.response.data);
    } else {
      console.error(error.message);
    }
    throw error;
  }
}

// Call the function
createPaymentRequest().catch(err => {
  console.error('Payment request failed:', err);
});

Next Steps

After creating a payment request and receiving a successful response with a checkout URL, you should:

  • Redirect the customer to the checkout URL to complete the payment
  • Handle the callback when the customer is redirected back to your success or cancelled URL
  • Set up your IPN endpoint to receive payment notifications
  • Verify the payment status using the status check endpoint

See the Sample Response Code page for examples of how to handle responses and payment verifications.