Skip to content

Commit 81b8799

Browse files
authored
Merge pull request #57 from cloudblue/vendor-asset_request-apiary-scenario
apiary-scenario
2 parents 4c12cfa + 6ca3e1d commit 81b8799

File tree

6 files changed

+213
-2
lines changed

6 files changed

+213
-2
lines changed

examples/apiary-scenario/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
![Apiary Scenario Vendor](https://connect.cloudblue.com/community/sdk/vendor-scenario-example/fulfillment/manage-asset/asset-request-wit-php-sdk/)
2+
3+
# Apiary Scenario Vendor Template for PHP
4+
5+
This template was develop based on the Connect SDK Template functionallity.
6+
The Connect SDK Template for php provides developers an complete skeleton to start their automation project using the [Connect Fulfillment API](http://help.vendor.connect.cloud.im/support/solutions/articles/43000030735-fulfillment-management-module) together with the [Connect SDK for PHP](https://github.com/ingrammicro/connect-php-sdk).
7+
8+
## Requirements
9+
10+
In order to use this template you will need an environment capable to run PHP scripts, any version starting PHP 5.6 is supported. Additionally please ensure that [composer](https://getcomposer.org/) it's functional.
11+
12+
## Installation
13+
14+
Once you has pulled the project, go to the folder examples/apiary-scenario and run "composer update" to get the latest versions of the dependencies and to update the file composer.lock
15+
16+
## PHP
17+
In the config.php you must to set the API Vendor credentials.
18+
In `app/ProductFulfillment.php` file in the `processRequest()` method is the logic of the process.
19+
This have two parts.
20+
21+
Create tenant: Process every request that not have filled the parameter tenantId, calling the API Vendor System simulated by Apiary making a POST with the new tenant. Cactch the Id of the response and store in the parameter tenantId of the Purchase Request in Connect.
22+
23+
Process tenant: Verify the status of every request into Vendor System and if the status="ready", process the Purchase Request in Connect.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Ingram Micro Cloud Blue Connect SDK.
5+
*
6+
* @copyright (c) 2020. Ingram Micro. All Rights Reserved.
7+
*/
8+
9+
namespace App;
10+
11+
/**
12+
* Class ProductFulfillment
13+
* @package App
14+
*/
15+
define('URL_APIARY', 'https://private-368580-vendorexample.apiary-mock.com/');
16+
17+
class ProductFulfillment extends \Connect\FulfillmentAutomation
18+
{
19+
/**
20+
* Process each pending request
21+
* @param \Connect\Request $request
22+
*/
23+
24+
public function processRequest($request)
25+
{
26+
$client = new \GuzzleHttp\Client();
27+
switch($request->type){
28+
case 'purchase':{
29+
$parameterTenantId = $request->asset->getParameterById('tenantId')->value;
30+
if($parameterTenantId === "") {
31+
if(count($request->asset->items)==1){
32+
foreach ($request->asset->items as $item) {
33+
$mpn = $item->mpn;
34+
$quantity = $item->quantity;
35+
break;
36+
};
37+
$body = array(
38+
'Attributes' => [
39+
'product' => [
40+
'item' => $mpn,
41+
'quantity' => $quantity
42+
],
43+
'account' => [
44+
'accountFirstName' => $request->asset->tiers->customer->contact_info->contact->first_name,
45+
'accountLastName' => $request->asset->tiers->customer->contact_info->contact->last_name,
46+
'accountCompany' => $request->asset->tiers->customer->contact_info->address_line1,
47+
'accountAddress' => $request->asset->tiers->customer->contact_info->address_line1,
48+
'accountCity' => $request->asset->tiers->customer->contact_info->city,
49+
'accountState' => $request->asset->tiers->customer->contact_info->state,
50+
'accountPostalCode' => $request->asset->tiers->customer->contact_info->postal_code,
51+
'accountCountry' => $request->asset->tiers->customer->contact_info->country,
52+
'accountEmail' => $request->asset->tiers->customer->contact_info->contact->email,
53+
'accountPhone' => $request->asset->tiers->customer->contact_info->contact->phone_number->phone_number
54+
]
55+
]
56+
);
57+
$res = $client->request('POST', URL_APIARY.'tenant', [
58+
'body' => json_encode($body)
59+
]);
60+
$response = json_decode($res->getBody(), True);
61+
$this->logger->info("Tenant:".$response['tenantId']);
62+
$this->logger->info("External ID:".$response['externalId']);
63+
$this->logger->info("Product Item Id:".$response['productItemId']);
64+
$this->logger->info("Status:".$response['status']);
65+
if($response['tenantId']){
66+
$this->updateParameters($request, array (
67+
new \Connect\Param([
68+
'id' => 'tenantId',
69+
'value' => $response['tenantId']
70+
])
71+
));
72+
} else {
73+
$this->logger->info("Error processing tenant in Vendor System");
74+
}
75+
} else {
76+
$this->logger->info("Request malformed, too many items");
77+
}
78+
throw new \Connect\Skip("Moving to next request");
79+
} else {
80+
$res = $client->request('GET', URL_APIARY.'tenant/'.$parameterTenantId);
81+
$data = json_decode($res->getBody(), True);
82+
if ($data['status'] === 'ready'){
83+
$this->logger->info("Service is ready on Vendor System");
84+
return new \Connect\ActivationTemplateResponse($request->asset->configuration->getParameterById('templateId')->value);
85+
}
86+
}
87+
}
88+
default:
89+
throw new \Connect\Skip("This processor not handle this type of request: ".$request->type);
90+
}
91+
}
92+
93+
/**
94+
* Processing each pending Tier Config
95+
* @param \Connect\TierConfigRequest $tierConfigRequest
96+
*/
97+
public function processTierConfigRequest($tierConfigRequest)
98+
{
99+
// TODO: Implement processTierConfigRequest() method
100+
}
101+
102+
/**
103+
* Run the Product Fulfillment Request Processor
104+
* @return bool
105+
* @throws \GuzzleHttp\Exception\GuzzleException
106+
*/
107+
public function run()
108+
{
109+
try {
110+
111+
/**
112+
* run the application in custom context, any error
113+
* handling customization should be done here
114+
*/
115+
$this->process();
116+
return true;
117+
} catch (\Exception $e) {
118+
$this->logger->error($e->getMessage());
119+
if (is_callable([$this->logger, 'dump'])) {
120+
$this->logger->dump();
121+
}
122+
}
123+
124+
return false;
125+
}
126+
127+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "apsconnect/connect-quickstart-template",
3+
"homepage": "http://www.ingrammicrocloud.com/",
4+
"type": "project",
5+
"description": "APS Connect QuickStart Template",
6+
"license": "proprietary",
7+
"config": {
8+
"preferred-install": "dist",
9+
"discard-changes": true,
10+
"sort-packages": true,
11+
"optimize-autoloader": true
12+
},
13+
"minimum-stability": "dev",
14+
"prefer-stable": true,
15+
"support": {
16+
"issues": "http://www.ingrammicrocloud.com/support",
17+
"docs": "http://www.ingrammicrocloud.com/docs"
18+
},
19+
"require": {
20+
"php": ">=5.6.0",
21+
"apsconnect/connect-sdk": ">=19.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^5.7",
25+
"mockery/mockery": "^0.9"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"App\\": "app/"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Test\\": "tests/"
35+
}
36+
},
37+
"scripts": {
38+
"post-root-package-install": [
39+
"@php -r \"file_exists('config.json') || copy('config.example.json', 'config.json');\""
40+
]
41+
}
42+
}
43+

examples/apiary-scenario/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"apiKey": "SU-000-000-000:0000000000000000000000000000000000000000000",
3+
"apiEndpoint": "https://api.connect.cloudblue.com/public/v1",
4+
"logLevel": 2,
5+
"timeout": 120,
6+
"sslVerifyHost": false
7+
}
8+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$requests = new \App\ProductFulfillment();
6+
$requests->run();

src/Template.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Template extends Model
1818
* @var string
1919
*/
2020
public $id;
21+
2122
/**
2223
* @var string
2324
*/
@@ -26,12 +27,15 @@ class Template extends Model
2627
/**
2728
* @var string
2829
*/
29-
30+
public $type;
31+
32+
/**
33+
* @var string
34+
*/
3035
public $name;
3136

3237
/**
3338
* @var string
3439
*/
35-
3640
public $body;
3741
}

0 commit comments

Comments
 (0)