Skip to content

Commit

Permalink
docs: Add simulation example
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle committed Oct 17, 2024
1 parent 3d66135 commit 80391a7
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/catalog_management.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

require __DIR__ . '/../vendor/autoload.php';

$environment = Paddle\SDK\Environment::tryFrom(getenv('PAD') ?: '') ?? Paddle\SDK\Environment::SANDBOX;
$environment = Paddle\SDK\Environment::tryFrom(getenv('PADDLE_ENVIRONMENT') ?: '') ?? Paddle\SDK\Environment::SANDBOX;
$apiKey = getenv('PADDLE_API_KEY') ?: null;

if (is_null($apiKey)) {
Expand Down
130 changes: 130 additions & 0 deletions examples/simulations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Exceptions\ApiError;
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse;
use Paddle\SDK\Notifications\Entities\Address;
use Paddle\SDK\Resources\Shared\Operations\List\Pager;
use Paddle\SDK\Resources\Simulations\Operations\CreateSimulation;
use Paddle\SDK\Resources\Simulations\Operations\ListSimulations;
use Paddle\SDK\Resources\Simulations\Operations\UpdateSimulation;

require __DIR__ . '/../vendor/autoload.php';

$environment = Paddle\SDK\Environment::tryFrom(getenv('PADDLE_ENVIRONMENT') ?: '') ?? Paddle\SDK\Environment::SANDBOX;
$apiKey = getenv('PADDLE_API_KEY') ?: null;
$simulationId = getenv('PADDLE_SIMULATION_ID') ?: null;
$notificationSettingId = getenv('PADDLE_NOTIFICATION_SETTING_ID') ?: null;

if (is_null($apiKey)) {
echo "You must provide the PADDLE_API_KEY in the environment:\n";
echo "PADDLE_API_KEY=your-key php examples/basic_usage.php\n";
exit(1);
}

$paddle = new Paddle\SDK\Client($apiKey, options: new Paddle\SDK\Options($environment));

// ┌───
// │ List Simulations │
// └──────────────────┘
try {
$simulations = $paddle->simulations->list(new ListSimulations(new Pager(perPage: 10)));
} catch (ApiError|MalformedResponse $e) {
var_dump($e);
exit;
}

echo "List Simulations\n";

foreach ($simulations as $simulation) {
echo sprintf("- %s:\n", $simulation->name);
echo sprintf(" - ID: %s\n", $simulation->id);
echo sprintf(" - Type: %s\n", $simulation->type->getValue());
echo sprintf(" - Notification Setting ID: %s\n", $simulation->notificationSettingId);
}

// ┌───
// │ Create Simulation │
// └───────────────────┘
try {
$simulation = $paddle->simulations->create(
new CreateSimulation(
notificationSettingId: $notificationSettingId,
type: EventTypeName::AddressCreated(),
name: 'Simulate Address Creation',
payload: Address::from([
'id' => 'add_01hv8gq3318ktkfengj2r75gfx',
'country_code' => 'US',
'status' => 'active',
'created_at' => '2024-04-12T06:42:58.785000Z',
'updated_at' => '2024-04-12T06:42:58.785000Z',
'customer_id' => 'ctm_01hv6y1jedq4p1n0yqn5ba3ky4',
'description' => 'Head Office',
'first_line' => '4050 Jefferson Plaza, 41st Floor',
'second_line' => null,
'city' => 'New York',
'postal_code' => '10021',
'region' => 'NY',
]),
)
);
} catch (ApiError|MalformedResponse $e) {
var_dump($e);
exit;
}

echo sprintf("Created Simulation: %s\n", $simulation->name);
echo sprintf("- ID: %s\n", $simulation->id);
echo sprintf("- Type: %s\n", $simulation->type->getValue());
echo sprintf("- Notification Setting ID: %s\n", $simulation->notificationSettingId);

// ┌───
// │ Get Simulation │
// └────────────────┘
try {
$simulation = $paddle->simulations->get($simulationId);
} catch (ApiError|MalformedResponse $e) {
var_dump($e);
exit;
}

echo sprintf("Get Simulation: %s\n", $simulation->name);
echo sprintf("- ID: %s\n", $simulation->id);
echo sprintf("- Type: %s\n", $simulation->type->getValue());
echo sprintf("- Notification Setting ID: %s\n", $simulation->notificationSettingId);

// ┌───
// │ Update Simulation │
// └───────────────────┘
try {
$simulation = $paddle->simulations->update(
$simulationId,
new UpdateSimulation(
type: EventTypeName::AddressCreated(),
payload: Address::from([
'id' => 'add_01hv8gq3318ktkfengj2r75gfx',
'country_code' => 'US',
'status' => 'active',
'created_at' => '2024-04-12T06:42:58.785000Z',
'updated_at' => '2024-04-12T06:42:58.785000Z',
'customer_id' => 'ctm_01hv6y1jedq4p1n0yqn5ba3ky4',
'description' => 'Head Office',
'first_line' => '4050 Jefferson Plaza, 41st Floor',
'second_line' => null,
'city' => 'New York',
'postal_code' => '10021',
'region' => 'NY',
]),
)
);
} catch (ApiError|MalformedResponse $e) {
var_dump($e);
exit;
}

echo sprintf("Updated Simulation: %s\n", $simulation->name);
echo sprintf("- ID: %s\n", $simulation->id);
echo sprintf("- Type: %s\n", $simulation->type->getValue());
echo sprintf("- Notification Setting ID: %s\n", $simulation->notificationSettingId);

0 comments on commit 80391a7

Please sign in to comment.