moneymour/api-client-php

The library offers easy access to Moneymour APIs

1.5.0 2019-04-04 09:43 UTC

This package is auto-updated.

Last update: 2024-06-04 21:38:09 UTC


README

The library offers easy access to Moneymour APIs


Installation

A composer package is available on Packagist

$ composer install moneymour/api-client-php

Usage

use Moneymour\ApiClient as MoneymourApiClient;

class ApiClient {

  // Get the following information from https://merchant.sandbox.moneymour.com
  // For production: https://merchant.moneymour.com
  const PRIVATE_KEY = '<YOUR_PRIVATE_KEY>';
  const MERCHANT_ID = '<YOUR_MERCHANT_ID>';
  const MERCHANT_SECRET = '<YOUR_MERCHANT_SECRET>';

  // Build the client
  $signatureFactory = new SignatureFactory(self::PRIVATE_KEY);
  $client = new MoneymourApiClient(
    self::MERCHANT_ID,
    self::MERCHANT_SECRET,
    $signatureFactory,
    MoneymourApiClient::ENVIRONMENT_SANDBOX  // or ENVIRONMENT_PRODUCTION when you get ready
  );

  // Request payload
  $payload = [
    'orderId' => '123456', // the order id in your system
    'amount' => '1080', // must be >= 300 and <= 2000
    'email' => 'customer@merchant.com',
    'phoneNumber' => '+393334444555', // must include +39
    'products' => [ // the list of products in the cart
      [
        'name' => 'iPhone 7',
        'type' => 'Electronics',
        'price' => '500',
        'quantity' => 2,
        'discount' => 0
      ],
      [
        'name' => 'MacBook Pro Charger',
        'type' => 'Electronics',
        'price' => '80',
        'quantity' => 1,
        'discount' => 0
      ]
    ]
  ];

  // Perform the request
  $response = $client->request($payload);
  
  // Output example in JSON format
  print json_encode($res, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE) . "\n";
  
  /*
  {
    "status": "accepted",
    "amount": 1080,
    "phoneNumber": "+393334444555",
    "orderId": "123456",
    "products": [
        {
            "name": "iPhone 7",
            "type": "Electronics",
            "price": "500",
            "price": "2",
            "discount": 0
        },
        {
            "name": "MacBook Pro Charger",
            "type": "Electronics",
            "price": "80",
            "price": "1",
            "discount": 0
        }
    ]
  }
  */
}

Gotchas


Moneymour APIs allow only one pending request at a time. If you get a 403 error having message Duplicated request please cancel the current pending request using the Moneymour app for iOS or Android.


Verify signature in your webhook

$factory = new SignatureFactory(self::PRIVATE_KEY);
$factory->verify(
  $signature, // http request header "Signature"
  $expiresAt, // http request header "Expires-at"
  $body, // http request body
  MoneymourApiClient::ENVIRONMENT_SANDBOX  // or ENVIRONMENT_PRODUCTION when you get ready
); // return true or false