Skip to content

Commit

Permalink
Added a form-demo example
Browse files Browse the repository at this point in the history
  • Loading branch information
x1s committed Jun 1, 2016
1 parent 66ff3d5 commit 1c7a8cf
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
This file is a manually maintained list of changes for each release. Feel free to add your
changes here when sending pull requests. Also send corrections if you spot any mistakes.

## v2.0.1 (2016-06-02) - x1s
* Features:
- Added missing methods for detailing campaigns
- Added missing methods for listing passes
- Added a example projetc with the basic coupon flow

## v2.0.0 (2015-11-19) - lmmendes
* Feature:
- Initial relase compatible with API v2.0 (beta release)
Expand Down
3 changes: 3 additions & 0 deletions examples/form-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/*
composer.lock
composer.phar
68 changes: 68 additions & 0 deletions examples/form-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Form Campaign DEMO

This is a *simple* project aimed to show how to use the Passworks PHP SDK

## Configure

First of all you sould have a Passworks Account and get yours `API credentials`
Then set it to the `config.php`

```
$API_USER = 'username';
$API_KEY = 'api_key';
```

Then go to the example folder, install the composer and and load the API,

```
# cd examples/form-demo
# curl -sS https://getcomposer.org/installer | php
# php composer.phar install
...
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing passworks/passworks-php (v2.0.1)
Downloading: 100%
Writing lock file
Generating autoload files
```

## Create a campaign

The `generate_campaign.php` script generates a new coupon campaign and you can change it placing
your campaign's information.

> The campaign structure is documentented in this file
```
# php generate_campaign.php
```

This campaign was created with 2 CSV fields (name, email) that are supposed to be filled during
the pass creation.

## Running a basic server

For this test, you can run the basic PHP server.

```
# php -S localhost:8080
PHP 7.0.7 Development Server started at Wed Jun 1 11:14:14 2016
Listening on http://localhost:8080
Document root is /Users/pedroivo/Documents/passworks/passworks-php/examples/form-demo
Press Ctrl-C to quit.
```

Then navigate to `http://localhost:8080` and see the campaigns and create some passes

## Create passes

After create the campaign, navigate to the `http://localhost:8080` page and chose your campaign.
Then just fill the fields and create a new pass.

Note that there is a list os the passes already created with the links to each format of distribution.



Binary file added examples/form-demo/assets/climb-gym.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions examples/form-demo/campaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
require 'config.php';
$campaign_id = $_GET["id"];
$campaign = $api->getCouponInfo($campaign_id);
$passes = $api->listCouponPasses($campaign_id);

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Testing Creation Passes</title>
<link rel="stylesheet" href="">
<style>

form label {
display: block;
}

</style>
</head>
<body>

<h2>Create your pass for the campaign "<?= $campaign->name ?>"</h2>
<form action="generate_pass.php" method="post">
<input type="hidden" name="id" value="<?= $campaign_id ?>">
<label for="">
Name
<input type="text" name="name" placeholder="Whats is your name">
</label>
<label for="">
Email
<input type="email" name="email" placeholder="What is your email?">
</label>
<input type="submit" value="Generate">
</form>

<?php if (sizeof($passes) > 0) { ?>
<section>
<h2>Existing passes</h2>
<table>
<thead>
<tr>
<th>Pass ID</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($passes as $key => $value) { ?>
<tr>
<td><?= $value->id ?></td>
<td><?= $value->created_at ?></td>
<td>
<a href="<?= $value->page_url ?>" title="">Page</a>
<a href="<?= $value->pkpass_url ?>" title="">Download</a>
<a href="<?= $value->page_url ?>.qrcode" title="">QR Code</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</section>
<?php } ?>

</body>
</html>
25 changes: 25 additions & 0 deletions examples/form-demo/campaign_new.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
require 'config.php';

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Testing Creation Passes</title>
<link rel="stylesheet" href="">
</head>
<body>

<section>
<h2>New Campaign</h2>
<form action="campaign_new.php" method="post">
<label for="">
Name
<input type="text" name="name" placeholder="Whats is your name">
</label>
</section>

</body>
</html>
5 changes: 5 additions & 0 deletions examples/form-demo/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"passworks/passworks-php": "2.0.0"
}
}
12 changes: 12 additions & 0 deletions examples/form-demo/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
require 'vendor/autoload.php';

$API_USER = 'asd';
$API_KEY = 'ROV2zGLvXzP0DmFtBmDNLQ';


// Instantiate the Passworks client

$api = new Passworks\Client($API_USER, $API_KEY);
$api->setEndpoint('http://api.passworks.git:3000');

85 changes: 85 additions & 0 deletions examples/form-demo/generate_campaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
require 'config.php';

// upload a asset (background image)
$icon = $api->createAsset('icon', 'assets/climb-gym.png');

// As any other object in the API it returns a stdClass with the object's properties
// print_r($icon);
// stdClass Object
// (
// [id] => d5a32073-2ded-481b-838f-58d29f9e11ca
// [asset_type] => icon
// [created_at] => 2016-05-31T15:18:21Z
// )

$coupon_campaign_args = array(
'name' => "50% Off for Climbers",
'icon_id' => $icon->id,
'secondary_fields' => array(
array(
'key' => "Name", // This key is going to be used when matching the pass information
'value' => "Your Name",
'label' => "Name"
),
array(
'key' => "Email", // This key is going to be used when matching the pass information
'value' => "Your email",
'label' => "Email"
)
),
'back_fields' => array(
array(
'key' => "contact",
'value' => "Call us anytime: +351 915 652 071",
'label' => "Our contact"
)
),
'barcode' => array(
'format' => "qrcode",
'message' => "",
'alt_text' => "",
'alt_text_type' => "mirror"
)
);

$coupon_campaign = $api->createCouponCampaign($coupon_campaign_args);

// print_r($coupon_campaign);
//
// stdClass Object (
// [id] => 9f28f63e-823b-43b0-8c53-30c7ed596ff5
// [name] => 20% Off for Climbers
// [template_id] => 62a4341e-4499-4b27-b6bc-32a9ee42af94
// [organization_name] => asd
// [barcode] => stdClass Object (
// [format] => none
// [message] =>
// [alt_text] =>
// [alt_text_type] => mirror
// )
// [barcodes] => Array()
// [allow_multiple_redeems] =>
// [header_fields] => Array()
// [primary_fields] => Array()
// [secondary_fields] => Array()
// [auxiliary_fields] => Array()
// [back_fields] => Array()
// [locations] => Array()
// [beacons] => Array()
// [created_at] => 2016-05-31T15:19:56Z
// [updated_at] => 2016-05-31T15:19:56Z
// [distributions] => Array (
// [0] => stdClass Object (
// [id] => 98497cb0-e798-4b02-a0d5-c04034c9ce05
// [distribution_type] => downloadPage
// [state] => published
// [page_url] => http://192.168.1.79.xip.io:3000/p/s8K7vPi9vQ
// [pkpass_url] => http://192.168.1.79.xip.io:3000/p/s8K7vPi9vQ.pkpass
// [preview_url] => http://192.168.1.79.xip.io:3000/p/s8K7vPi9vQ.png
// [created_at] => 2016-05-31T15:19:56.696Z
// [updated_at] => 2016-05-31T15:19:56.769Z
// [published_at] => 2016-05-31T15:19:56.776Z
// )
// )
// )
29 changes: 29 additions & 0 deletions examples/form-demo/generate_pass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
require 'config.php';

$campaign_id = $_POST["id"];
$name = $_POST["name"];
$email = $_POST["email"];

if (strlen($name) <= 0 || strlen($email) <= 0) {
// TODO send a message
header("Location: index.php");
}

$params = array(
'secondary_fields' => array(
array(
'value' => $name,
'key' => 'Name', // key matching the campaign field
'label' => 'Name'
),
array(
'value' => $email,
'key' => 'Email', // key matching the campaign field
'label' => 'Email'
)
)
);

$api->createCoupon($campaign_id, $params);
header("Location: campaign.php?id=$campaign_id");
45 changes: 45 additions & 0 deletions examples/form-demo/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
require 'config.php';

$campaigns = $api->getCouponCampaigns();

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Testing Creation Passes</title>
<link rel="stylesheet" href="">
</head>
<body>

<section>
<h2>Campaigns</h2>
<table>
<thead>
<tr>
<th>Campaign</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($campaigns as $key => $value) { ?>
<tr>
<td>
<img src="<?= $value->distributions[0]->preview_url ?>" alt="">
<a href="campaign.php?id=<?= $value->id ?>" title=""><?= $value->name ?></a>
</td>
<td>
<a href="<?= $value->distributions[0]->page_url ?>" title="">Page</a>
<a href="<?= $value->distributions[0]->pkpass_url ?>" title="">Download</a>
<a href="<?= $value->distributions[0]->page_url ?>.qrcode" title="">QR Code</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</section>

</body>
</html>
Empty file added examples/form-demo/passes.php
Empty file.

0 comments on commit 1c7a8cf

Please sign in to comment.