|
| 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 | +} |
0 commit comments