Skip to content

Creating, Listing and Editing Coupons

pedro ivo edited this page Jul 11, 2016 · 7 revisions
<?php

require 'vendor/autoload.php';

use Passworks\Client;

// Instantiate the Passworks client
$api = new Passworks\Client('your api username', 'your api key');

// upload a asset (background image)
$api->createAsset('background', '/local-path-to-a-image/image.png');

// create a coupon campaign
$coupon_campaign_args = array(
 'name'    => "My first coupon campaign",
 'icon_id' => '51838f55-8d40-4554-be22-03ae685a0a32'
);
$coupon_campaign = $api->createCouponCampaign($coupon_campaign_args);
print_r($coupon_campaign);

// fetching all coupon campaigns as an array
print_r($api->getCouponCampaigns()->toArray());

// iterating all coupon campaigns
$coupons = $api->getCouponCampaigns();
foreach($coupons as $coupon){
  print_r($coupon);
}

// add a pass (coupon) to the coupon campaign
$coupon_args = array(
 'back_fields' => array(
   array(
     'value' => 'Marques de Pombal, Portugal',
     'key'   => 'address',
     'label' => 'Addess'
   )
 )
);
$coupon = $api->createCoupon($coupon_campaign->id, $coupon_args);
print_r($coupon);

// get a specific coupon
print_r($api->getCoupon($coupon_campaign->id, $coupon->id));

// update the coupon
$coupon_update_params  = array(
 'back_fields' => array(
   array(
     'value' => 'Av. da Liberdade, 230',
     'key'   => 'address',
     'label' => 'Addess'
   )
 )
);

// the $extra array is option i'ts used to send extra aguments to the updateCoupon function
// in this case we are telling passworks not to push the pass to the user after update.
// (by default passworks pushes each pass after update)
$extra          = array('push' => false);
$updated_coupon = $api->updateCoupon($coupon_campaign->id, $coupon->id, $coupon_update_params, $extra);

print_r($updated_coupon);


// There are two reports available, a daily and a totalisation report
// for more information, check the API documentation:
// https://github.com/passworks/passworks-api/blob/master/v2/sections/reports.md
//
// Daily usage:
// $report_params = array("start_date" => '2010-10-10', "end_date" => '2016-06-30');
// $daily_report = $api->getCouponDailyReport($campaign_id, $report_params);

// Totalisation
$total_report = $api->getCouponTotalReport($coupon_campaign->id);
$report_data = get_object_vars($total_report->report);

print_r($report_data);
// Array
// (
//     [apple] => stdClass Object
//         (
//             [installs_count] => 0
//             [uninstalls_count] => 0
//         )

//     [android] => stdClass Object
//         (
//             [installs_count] => 2
//             [uninstalls_count] => 0
//         )

//     [installs_count] => 2
//     [uninstalls_count] => 0
//     [redeems_count] => 0
//     [fetches_count] => 0
//     [downloads_count] => 1
//     [views_count] => 0
//     [creates_count] => 3
//     [expires_count] => 0
//     [pushes_count] => 0
//     [updates_count] => 0
//     [webhooks_count] => 0
//     [api_calls_count] => 0
//     [distribution_updates_count] => 0
//     [active_count] => 2
//     [removed_count] => 0
// )

?>

Clone this wiki locally