Skip to content

Instantly share code, notes, and snippets.

@xcommerce-gists
Created November 20, 2012 02:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xcommerce-gists/4115482 to your computer and use it in GitHub Desktop.
Save xcommerce-gists/4115482 to your computer and use it in GitHub Desktop.
Magento coupon code generator test script
<?php
/********************************************************************
File name: rest_test.php
Description:
A PHP test script that calls the Coupon AutoGen extension
to Magento's REST API.
The Coupon AutoGen API takes:
-- the rule ID of the &quot;Generate Coupons&quot; rule to execute
-- the number of coupon codes to generate
-- the length of each coupon code
-- the format of each coupon code
The API returns the generated coupon codes, in JSON-encoded form
********************************************************************/
// Replace <<...>> below with the key and secret values generated for the Coupon AutoGen Test Driver
$consumerKey = '<<YOUR CONSUMER KEY>>'; // from Admin Panel's &quot;REST - OAuth Consumers page&quot;
$consumerSecret = '<<YOUR CONSUMER SECRET>>'; // from Admin Panel's &quot;REST - OAuth Consumers page&quot;
// Set the OAuth callback URL to this script since it contains the logic
// to execute *after* the user authorizes this script to use the Coupon AutoGen API
$callbackUrl = "http://<<host-or-ip:port>>/<<path>>/rest_test.php";
// Set the URLs below to match your Magento installation
$temporaryCredentialsRequestUrl = "http://<<host-or-ip:port>>/<<path>>/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://<<host-or-ip:port>>/<<path>>/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://<<host-or-ip:port>>/<<path>>/oauth/token';
$apiUrl = 'http://<<host-or-ip:port>>/<<path>>/api/rest';
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
exit;
} else if ($_SESSION['state'] == 1) {
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: ' . $callbackUrl);
exit;
} else {
// We have the OAuth client and token. Now, let's make the API call.
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
// Set the array of params to send with the request
$ruleId = <<RULE_ID>>; // Set to the rule ID of the Generate Coupons rule
$couponGenerationData = array();
$couponGenerationData['qty'] = 2; // Number of coupons codes to create
$couponGenerationData['length'] = 7; // Length of each coupon code
// Options for format include:
// alphanum (for alphanumeric codes), alpha (for alphabetical codes), and num (for numeric codes)
$couponGenerationData['format'] = "alphanum"; // Use alphanumeric for the coupon code format
// Generate coupon codes via POST
$resourceUrl = "$apiUrl/coupondemo/rules/{$ruleId}/codes";
$oauthClient->fetch($resourceUrl, json_encode($couponGenerationData), OAUTH_HTTP_METHOD_POST, array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
));
// Retrieve list of created coupons via GET
$collectionFilters = array('limit' => $couponGenerationData['qty'], 'order' => 'coupon_id', 'dir' => 'dsc');
$oauthClient->fetch($resourceUrl, $collectionFilters, OAUTH_HTTP_METHOD_GET, array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
));
$coupons = json_decode($oauthClient->getLastResponse(), true);
// Display the newly generated codes to demonstrate that the Coupon AutoGen API works
// In reality, you might put these codes in emails to customers, store them in a database, etc.
echo "New coupon codes:<br/>";
foreach ($coupons as $coupon) {
echo " --> " . $coupon['code'] . "<br/>";
}
}
} catch (OAuthException $e) {
print_r($e->getMessage());
echo "<br/>";
print_r($e->lastResponse);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment