Do you have API for import/export points in Reward Points for Magento 2

Our Reward Points extension implements REST API for accessing and manipulating data from external tools and services. It can be used for import/export points as well.

It will require some PHP developing skills, but it is not that difficult. Just follow our ready-to-use snippets.

Obtaining Access Token

REST API is based on PHP CURL library for sending requests, which are approved by our extension using access tokens - a unique data chunks, which confirm your identity and permissions.

Therefore, prior to importing/exporting operation you need to send your Magento 2 login and password (typically with admin-level permissions, set at System → Permissions → All Users). Let us have admin account with “admin” login and “sZ62LdQsJunn5ZRG” password. Then code for obtaining access token would be the following:

$userData = array("username" => "admin", "password" => "sZ62LdQsJunn5ZRG");
$ch = curl_init("https://store.com/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($userData))));

$token = curl_exec($ch);

Typical access token should look like this: qdotbtjhl1hdr4cq5cxixxlxbs4velo2

Important notes:

  • CURL works only with HTTPS access. It prevents accidental information leaking and improves security.
  • CURL do not work correctly with self-signed certificates. In this case curl_exec can return false. You can, however, bypass certification verification by adding corresponging option before curl_exec:

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

Once you have the token, you can proceed with importing and exporting data.

Importing Points

Our Reward Points extension do not have a separate balance records. Instead, it builds balance automatically by transactions. So, to import points we need to create transactions.

REST API uses CURL, so we need just to build JSON-expression and send it to the proper URL. We will need the following data:

  • Customer ID - ID of customer, who needs to receive points
  • Points Amount - how many points should come to his balance.
  • Transaction expiration date - when this transaction should expire, if points were not used.

Let us have customer Veronica Costello (ID = 1), and we need to transfer to her 17 points. Here is the code, which will do that (having obtained the token):

$data = [
    'transaction' => [
        'customer_id' => 1,
        'amount' => 17,
        'comment' => 'REST API Points Import',
        'code' => 'api_transaction',
        'expires_at' => ('2019-12-31'),
    ]
];

$ch = curl_init("https://store.com/index.php/rest/V1/rewards/transaction");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData)), "Authorization: Bearer " . json_decode($token)));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = curl_exec($ch);

If import was successful, $result will contain JSON-representation of newly-created transaction:

{
  "id": 1,
  "customer_id": 1,
  "amount": 17,
  "amount_used": null,
  "comment": "REST API Points Import",
  "code": "api_transaction",
  "is_expired": null,
  "is_expiration_email_sent": null,
  "expires_at": "2019-12-31"
}

It will also appear at Marketing → Reward Points → Transactions section:

Important notes:

  • Before importing points you need to have all customers already imported.
  • Points amount can be negative. This way you can perform mass-corrections, if something went wrong with your reward policy

Exporting Points

Exporting points will be a little tricky. Our extension supports only export of transactions, that belong to the particular customer. But you can obtain a customer collection directly from PHP script, for example. Here is ready-to-use snippet:

<?php

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
error_reporting(E_ALL & ~E_NOTICE);

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customers = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();

This snippet will return a collection of all customers. Then we can export transactions one by one customer to a two-leveled array:

$transactionsByCustomers = array();
foreach ($customers as $customer) {
    $ch = curl_init("https://rewards225.qa.mirasvit.com/index.php/rest/V1/rewards/customer/" . $customer->getId() . "/transactions");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData)), "Authorization: Bearer " . json_decode($token)));
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    $result = curl_exec($ch);
    $transactionsByCustomers[] = json_decode($result, true);
}

This array will contain all transactions, stored in our Reward Points module, and can be transformed to any representation you need, or imported to any external software.

Did you know…

… that if you have some PHP skills, you can push possibilities of earning point with our extension even further. We offer you an unique feature of Custom Behaviour Events, which allows you to create your own triggers to award customer with points.

Just copy and paste simple snippets of code, and your customers will be able to receive points for any activity, not covered by our extension.

Loading...