Skip to content

Commit

Permalink
wip: create customer if it doesn't already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 31, 2024
1 parent 63334ac commit d7a781a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
66 changes: 66 additions & 0 deletions example/06-create-customer-if-no-match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* This example attempts to find a customer by the email address, supplied as
* the first argument to the script. If there is no match, it will create a new
* customer.
*
* The script requires the following arguments, in order:
* 1. email address
* 2. first name
* 3. last name
* 4. mobile number
*
* For any example to work, you must supply your own secrets in config.ini:
* - username
* - client
* - secret_key
*/

use BrightFlair\SpektrixAPI\CustomerNotFoundException;

chdir(dirname(__DIR__));
require "vendor/autoload.php";

$config = parse_ini_file("config.ini");
$client = new BrightFlair\SpektrixAPI\Client(
$config["username"],
$config["client"],
$config["secret_key"],
);

$email = $argv[1] ?? null;
if(!$email) {
fwrite(STDERR, "No email address supplied\n");
exit(1);
}
$firstName = $argv[2] ?? null;
if(!$firstName) {
fwrite(STDERR, "No first name supplied\n");
exit(1);
}
$lastName = $argv[3] ?? null;
if(!$lastName) {
fwrite(STDERR, "No last name supplied\n");
exit(1);
}
$mobileNumber = $argv[4] ?? null;
if(!$mobileNumber) {
fwrite(STDERR, "No mobile number supplied\n");
exit(1);
}

try {
$customer = $client->getCustomer(email: $email);
echo "Customer found!\n";
echo "ID: $customer->id\n";
echo "Email: $customer->email\n";
echo "First name: $customer->firstName\n";
echo "Last name: $customer->lastName\n";
}
catch(CustomerNotFoundException) {
echo "No customer was found with the email address $email\n";
echo "Creating new customer...\n";

// TODO: This simply isn't working. HTTP 500 with no reason why. I'm going to have to get back in contact with the team.
$customer = $client->createCustomer($email, $firstName, $lastName, $mobileNumber);
}
40 changes: 39 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,40 @@ public function __construct(
$this->http = $fetchClient ?? new Http();
}

public function createCustomer(
string $email,
?string $firstName = null,
?string $lastName = null,
?string $mobile = null,
):Customer {
$endpoint = Endpoint::createCustomer;
$authenticatedRequest = new AuthenticatedRequest(
$this->secretKey,
$endpoint,
$this->client,
[
"email" => $email,
"firstName" => $firstName ?? "",
"lastName" => $lastName ?? "",
"mobile" => $mobile ?? "",
"birthDate" => ("D, d M Y H:i:s T"),
"friendlyId" => uniqid(),
]
);

if($json = $this->json($authenticatedRequest)) {
return new Customer(
$json->getString("id"),
$json->getString("email"),
firstName: $json->getString("firstName"),
lastName: $json->getString("lastName"),
mobile: $json->getString("mobile"),
);
}

throw new CustomerNotFoundException($email ?? $id);

Check failure on line 56 in src/Client.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

Undefined variable: $id

Check failure on line 56 in src/Client.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

Undefined variable: $id
}

public function getCustomer(
?string $id = null,
?string $email = null,
Expand Down Expand Up @@ -150,6 +184,7 @@ public function addTagToCustomer(
throw new SpektrixAPIException("Error adding tag ID $tagId to customer $customerId");
}


public function removeTagFromCustomer(
Tag|string $tag,
Customer|string $customer,
Expand All @@ -175,7 +210,6 @@ public function removeTagFromCustomer(
$this->json($authenticatedRequest);
}


private function json(AuthenticatedRequest $authenticatedRequest):?JsonObject {
$authorizationHeader = Signature::AUTH_PREFIX
. " "
Expand All @@ -191,6 +225,10 @@ private function json(AuthenticatedRequest $authenticatedRequest):?JsonObject {
"Authorization" => $authorizationHeader,
];

if($authenticatedRequest->body) {
$httpHeaders["Content-type"] = "application/x-www-form-urlencoded";
}

$init = [
"method" => $authenticatedRequest->httpMethod,
"headers" => $httpHeaders,
Expand Down
1 change: 1 addition & 0 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace BrightFlair\SpektrixAPI;

enum Endpoint:string {
case createCustomer = "POST v3/customers email={email}&firstName={firstName}&lastName={lastName}&mobile={mobile}"; //&Phone={mobile}&Title=XX&Password=ABCDEFGHIJKLMNOPQRSTUVWXYZ&BirthDate=2024-01-31T22:45:56.2997626+00:00
case getCustomerById = "GET v3/customers/{id}";
case getCustomerByEmail = "GET v3/customers?email={email}";
case getAllTags = "GET v3/tags";
Expand Down

0 comments on commit d7a781a

Please sign in to comment.