diff --git a/examples/accessTokenRequest.php b/examples/accessTokenRequest.php index af606bf..d7598fe 100644 --- a/examples/accessTokenRequest.php +++ b/examples/accessTokenRequest.php @@ -16,4 +16,4 @@ $client = new Client(); /** @var AccessTokenResponse $accessTokenResponse */ $accessTokenResponse = $client->request($request); -var_dump($accessTokenResponse->getAccessToken()); \ No newline at end of file +var_dump($accessTokenResponse->getAccessToken()); diff --git a/examples/createOrderCode.php b/examples/createOrderCode.php new file mode 100644 index 0000000..7817ef2 --- /dev/null +++ b/examples/createOrderCode.php @@ -0,0 +1,21 @@ +request($request); +print_r($order->getOrder()->getOrderId()); +print_r($order->getCodeUrl()); diff --git a/examples/createOrderNow.php b/examples/createOrderNow.php deleted file mode 100644 index 96eda26..0000000 --- a/examples/createOrderNow.php +++ /dev/null @@ -1,23 +0,0 @@ -request($request); -var_dump($order->getOrder()); - diff --git a/src/Auth/AccessTokenRequest.php b/src/Auth/AccessTokenRequest.php index d3bfd10..01b0eab 100644 --- a/src/Auth/AccessTokenRequest.php +++ b/src/Auth/AccessTokenRequest.php @@ -18,7 +18,7 @@ public function __construct( ) { parent::__construct( "POST", - $this->getAccessTokenUrl(), + $this->getBaseUrl() . '/api/get-token', ['Content-Type' => 'application/json'], $this->makeJsonBody($clientId, $clientSecret, $email, $password) ); diff --git a/src/Order/Order.php b/src/Order/Order.php index b691c7f..0d79631 100644 --- a/src/Order/Order.php +++ b/src/Order/Order.php @@ -3,48 +3,82 @@ namespace AllDigitalRewards\NeoCurrency\Order; use AllDigitalRewards\NeoCurrency\AbstractEntity; +use Exception; class Order extends AbstractEntity { + protected $campaign_brand_id; + protected $denomination; + protected $currency; + protected $codes; + protected $uuids; protected $order_id; - protected $custom1; - protected $data; - public function getOrderId() + public function getCampaignBrandId() { - return $this->order_id; + return $this->campaign_brand_id; } - public function setOrderId($order_id): void + public function setCampaignBrandId($campaign_brand_id): void { - $this->order_id = $order_id; + $this->campaign_brand_id = $campaign_brand_id; } - public function getCustom1() + public function getDenomination() { - return $this->custom1; + return $this->denomination; } - public function setCustom1($custom1): void + public function setDenomination($denomination): void { - $this->custom1 = $custom1; + $this->denomination = $denomination; } - public function getData() + public function getCurrency() { - return $this->data; + return $this->currency; + } + + public function setCurrency($currency): void + { + $this->currency = $currency; + } + + public function getCodes() + { + return $this->codes; + } + + public function setCodes($codes): void + { + $this->codes = $codes; + } + + public function getUuids() + { + return $this->uuids; + } + + public function setUuids($uuids): void + { + $this->uuids = $uuids; + } + + public function getOrderId() + { + return $this->order_id; + } + + public function setOrderId($order_id): void + { + $this->order_id = $order_id; } - public function setData($data): void + /** + * @throws Exception + */ + public function getOrderCode() { - if (!empty($data) && is_array($data)) { - $infoCollection = []; - foreach ($data as $info) { - $infoCollection[] = new OrderInfo($info); - } - $this->data = $infoCollection; - return; - } - $this->data = $data; + return $this->getCodes()[0] ?? throw new Exception('Code not present'); } } diff --git a/src/Order/OrderNowRequest.php b/src/Order/OrderCodeRequest.php similarity index 64% rename from src/Order/OrderNowRequest.php rename to src/Order/OrderCodeRequest.php index 206db21..2a320d6 100644 --- a/src/Order/OrderNowRequest.php +++ b/src/Order/OrderCodeRequest.php @@ -5,31 +5,25 @@ use AllDigitalRewards\NeoCurrency\AbstractRequest; use AllDigitalRewards\NeoCurrency\HasResponse; -class OrderNowRequest extends AbstractRequest implements HasResponse +class OrderCodeRequest extends AbstractRequest implements HasResponse { private int $brandId; private float $denomination; - private int $quantity; - private string $poNumber; - private string $email; + private int $campaignId; private string $clientId; public function __construct( string $clientId, string $accessToken, + int $campaignId, int $brandId, string $denomination, - int $quantity = 1, - string $poNumber = '', - string $email = '' ) { $this->clientId = $clientId; $this->accessToken = $accessToken; + $this->campaignId = $campaignId; $this->brandId = $brandId; $this->denomination = $denomination; - $this->quantity = $quantity; - $this->poNumber = $poNumber; - $this->email = $email; $this->setUpRequest(); } @@ -37,7 +31,7 @@ private function setUpRequest() { parent::__construct( "POST", - $this->getBaseUrl() . '/createordernow', + $this->getBaseUrl() . '/codes/create', $this->getRequestHeaders(), $this->makeJsonBody() ); @@ -45,21 +39,20 @@ private function setUpRequest() public function getResponseObject(): string { - return OrderResponse::class; + return OrderCodeResponse::class; } private function makeJsonBody(): string { return json_encode( [ - 'client_id' => $this->clientId, - 'purchase_order_number' => $this->poNumber, - 'custom1' => $this->email, + 'campaign_id' => $this->campaignId, 'brands' => [ [ "id" => $this->brandId, "denomination" => $this->denomination, - "quantity" => $this->quantity, + "quantity" => 1, + "client_id" => $this->clientId, ] ] ], diff --git a/src/Order/OrderResponse.php b/src/Order/OrderCodeResponse.php similarity index 50% rename from src/Order/OrderResponse.php rename to src/Order/OrderCodeResponse.php index 6e0f060..125bf5f 100644 --- a/src/Order/OrderResponse.php +++ b/src/Order/OrderCodeResponse.php @@ -3,22 +3,24 @@ namespace AllDigitalRewards\NeoCurrency\Order; use AllDigitalRewards\NeoCurrency\AbstractResponse; +use AllDigitalRewards\NeoCurrency\Traits\HasEnvironmentsTrait; use Exception; -class OrderResponse extends AbstractResponse +class OrderCodeResponse extends AbstractResponse { - private $order = null; + use HasEnvironmentsTrait; + + private Order $order; /** * @throws Exception */ public function extractData(array $data): AbstractResponse { - if (empty($data['success'])) { + if (empty($data['success'][0])) { throw new Exception('Order Failure'); } - - $order = new Order($data['success']); + $order = new Order($data['success'][0]); $this->setOrder($order); return $this; } @@ -28,8 +30,16 @@ private function setOrder(Order $order) $this->order = new Order($order); } - public function getOrder(): ?Order + public function getOrder(): Order { return $this->order; } + + /** + * @throws Exception + */ + public function getCodeUrl(): string + { + return $this->getActivateCodeBaseUrl() . '/' . $this->getOrder()->getOrderCode(); + } } diff --git a/src/Order/OrderInfo.php b/src/Order/OrderInfo.php deleted file mode 100644 index cba4436..0000000 --- a/src/Order/OrderInfo.php +++ /dev/null @@ -1,64 +0,0 @@ -campaign_brand_id; - } - - public function setCampaignBrandId($campaign_brand_id): void - { - $this->campaign_brand_id = $campaign_brand_id; - } - - public function getDenomination() - { - return $this->denomination; - } - - public function setDenomination($denomination): void - { - $this->denomination = $denomination; - } - - public function getCurrency() - { - return $this->currency; - } - - public function setCurrency($currency): void - { - $this->currency = $currency; - } - - public function getCodes() - { - return $this->codes; - } - - public function setCodes($codes): void - { - $this->codes = $codes; - } - - public function getUuids() - { - return $this->uuids; - } - - public function setUuids($uuids): void - { - $this->uuids = $uuids; - } -} diff --git a/src/Traits/HasEnvironmentsTrait.php b/src/Traits/HasEnvironmentsTrait.php index eb9dceb..f777f1a 100644 --- a/src/Traits/HasEnvironmentsTrait.php +++ b/src/Traits/HasEnvironmentsTrait.php @@ -4,43 +4,30 @@ trait HasEnvironmentsTrait { - private string $prodUrl = 'https://redeem.yourdigitalreward.com/api'; - private string $stagingUrl = 'https://redeem.yourdigitalreward.com/api/sandbox'; - private bool $isProduction; + private string $baseUrl = 'https://redeem.yourdigitalreward.com'; + private bool $isProduction = false; - public function setProduction(): void - { - $this->isProduction = true; - } - - public function setStaging(): void + public function isProduction(): bool { - $this->isProduction = false; + return $this->isProduction; } - public function isProduction(): bool + public function setIsProduction(bool $isProduction): void { - if (isset($this->isProduction)) { - // Environment was intentionally set. - return $this->isProduction; - } - - return getenv('ENVIRONMENT') === 'PRODUCTION'; + $this->isProduction = $isProduction; } public function getBaseUrl(): string { - return $this->isProduction() - ? $this->prodUrl - : $this->stagingUrl; + return getenv('ENVIRONMENT') === 'PRODUCTION' || $this->isProduction() + ? $this->baseUrl . '/api' + : $this->baseUrl . '/api/sandbox'; } - /** - * Unfortunately doing this because their sandbox doesn't have a token endpoint - * And collides with cleanly making a BaseUrl if in Sandbox env - */ - public function getAccessTokenUrl(): string + public function getActivateCodeBaseUrl(): string { - return $this->prodUrl . '/get-token'; + return getenv('ENVIRONMENT') === 'PRODUCTION' + ? $this->baseUrl . '/activate-code' + : $this->baseUrl . '/sandbox/activate-code'; } }