-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for saved payment methods (#97)
- Loading branch information
1 parent
f3be3d4
commit df53ce8
Showing
32 changed files
with
1,298 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Paddle\SDK\Exceptions\ApiError; | ||
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$environment = Paddle\SDK\Environment::tryFrom(getenv('PADDLE_ENVIRONMENT') ?: '') ?? Paddle\SDK\Environment::SANDBOX; | ||
$apiKey = getenv('PADDLE_API_KEY') ?: null; | ||
$customerId = getenv('PADDLE_CUSTOMER_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)); | ||
|
||
// ┌─── | ||
// │ Create Customer Auth Token │ | ||
// └────────────────────────────┘ | ||
try { | ||
$authToken = $paddle->customers->generateAuthToken($customerId); | ||
} catch (ApiError|MalformedResponse $e) { | ||
var_dump($e); | ||
exit; | ||
} | ||
|
||
echo sprintf("Created Customer Auth Token: %s\n", $authToken->customerAuthToken); | ||
echo sprintf(" - Expires At: %s\n", $authToken->expiresAt->format(DATE_RFC3339_EXTENDED)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Paddle\SDK\Exceptions\ApiError; | ||
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse; | ||
use Paddle\SDK\Resources\PaymentMethods\Operations\ListPaymentMethods; | ||
use Paddle\SDK\Resources\Shared\Operations\List\Pager; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$environment = Paddle\SDK\Environment::tryFrom(getenv('PADDLE_ENVIRONMENT') ?: '') ?? Paddle\SDK\Environment::SANDBOX; | ||
$apiKey = getenv('PADDLE_API_KEY') ?: null; | ||
$customerId = getenv('PADDLE_CUSTOMER_ID') ?: null; | ||
$paymentMethodId = getenv('PADDLE_PAYMENT_METHOD_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 Payment Methods │ | ||
// └──────────────────────┘ | ||
try { | ||
$paymentMethods = $paddle->paymentMethods->list( | ||
$customerId, | ||
new ListPaymentMethods( | ||
pager: new Pager(perPage: 10), | ||
), | ||
); | ||
} catch (ApiError|MalformedResponse $e) { | ||
var_dump($e); | ||
exit; | ||
} | ||
|
||
echo "List Payment Methods\n"; | ||
|
||
foreach ($paymentMethods as $paymentMethod) { | ||
echo sprintf("- %s:\n", $paymentMethod->id); | ||
echo sprintf(" - Type: %s\n", $paymentMethod->type->getValue()); | ||
|
||
if ($paymentMethod->card) { | ||
echo sprintf(" - Card Type: %s\n", $paymentMethod->card->type->getValue()); | ||
echo sprintf(" - Card Holder Name: %s\n", $paymentMethod->card->cardholderName); | ||
echo sprintf(" - Card Last 4 Digits: %s\n", $paymentMethod->card->last4); | ||
echo sprintf(" - Card Expiry Year: %d\n", $paymentMethod->card->expiryYear); | ||
echo sprintf(" - Card Expiry Month: %d\n", $paymentMethod->card->expiryMonth); | ||
} | ||
|
||
if ($paymentMethod->paypal) { | ||
echo sprintf(" - PayPal Reference: %s\n", $paymentMethod->paypal->reference); | ||
echo sprintf(" - PayPal Email: %s\n", $paymentMethod->paypal->email); | ||
} | ||
} | ||
|
||
// ┌─── | ||
// │ Get Payment Method | | ||
// └────────────────────┘ | ||
try { | ||
$paymentMethod = $paddle->paymentMethods->get($customerId, $paymentMethodId); | ||
} catch (ApiError|MalformedResponse $e) { | ||
var_dump($e); | ||
exit; | ||
} | ||
|
||
echo sprintf("Get Payment Method: %s\n", $paymentMethod->id); | ||
echo sprintf(" - Type: %s\n", $paymentMethod->type->getValue()); | ||
|
||
if ($paymentMethod->card) { | ||
echo sprintf(" - Card Type: %s\n", $paymentMethod->card->type->getValue()); | ||
echo sprintf(" - Card Holder Name: %s\n", $paymentMethod->card->cardholderName); | ||
echo sprintf(" - Card Last 4 Digits: %s\n", $paymentMethod->card->last4); | ||
echo sprintf(" - Card Expiry Year: %d\n", $paymentMethod->card->expiryYear); | ||
echo sprintf(" - Card Expiry Month: %d\n", $paymentMethod->card->expiryMonth); | ||
} | ||
|
||
if ($paymentMethod->paypal) { | ||
echo sprintf(" - PayPal Reference: %s\n", $paymentMethod->paypal->reference); | ||
echo sprintf(" - PayPal Email: %s\n", $paymentMethod->paypal->email); | ||
} | ||
|
||
// ┌─── | ||
// │ Delete Payment Method | | ||
// └───────────────────────┘ | ||
try { | ||
$paddle->paymentMethods->delete($customerId, $paymentMethodId); | ||
} catch (ApiError|MalformedResponse $e) { | ||
var_dump($e); | ||
exit; | ||
} | ||
|
||
echo sprintf("Deleted Payment Method: %s\n", $paymentMethodId); | ||
echo sprintf(" - Type: %s\n", $paymentMethod->type->getValue()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities\Collections; | ||
|
||
use Paddle\SDK\Entities\PaymentMethod; | ||
|
||
class PaymentMethodCollection extends Collection | ||
{ | ||
public static function from(array $itemsData, Paginator|null $paginator = null): self | ||
{ | ||
return new self( | ||
array_map(fn (array $item): PaymentMethod => PaymentMethod::from($item), $itemsData), | ||
$paginator, | ||
); | ||
} | ||
|
||
public function current(): PaymentMethod | ||
{ | ||
return parent::current(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities; | ||
|
||
use Paddle\SDK\Notifications\Entities\DateTime; | ||
use Paddle\SDK\Notifications\Entities\Entity; | ||
|
||
class CustomerAuthToken implements Entity | ||
{ | ||
private function __construct( | ||
public string $customerAuthToken, | ||
public \DateTimeInterface $expiresAt, | ||
) { | ||
} | ||
|
||
public static function from(array $data): self | ||
{ | ||
return new self( | ||
customerAuthToken: $data['customer_auth_token'], | ||
expiresAt: DateTime::from($data['expires_at']), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities; | ||
|
||
use Paddle\SDK\Entities\Shared\Card; | ||
use Paddle\SDK\Entities\Shared\Paypal; | ||
use Paddle\SDK\Entities\Shared\SavedPaymentMethodOrigin; | ||
use Paddle\SDK\Entities\Shared\SavedPaymentMethodType; | ||
|
||
class PaymentMethod implements Entity | ||
{ | ||
private function __construct( | ||
public string $id, | ||
public string $customerId, | ||
public string $addressId, | ||
public SavedPaymentMethodType $type, | ||
public Card|null $card, | ||
public Paypal|null $paypal, | ||
public SavedPaymentMethodOrigin $origin, | ||
public \DateTimeInterface $savedAt, | ||
public \DateTimeInterface $updatedAt, | ||
) { | ||
} | ||
|
||
public static function from(array $data): self | ||
{ | ||
return new self( | ||
id: $data['id'], | ||
customerId: $data['customer_id'], | ||
addressId: $data['address_id'], | ||
type: SavedPaymentMethodType::from($data['type']), | ||
card: isset($data['card']) ? Card::from($data['card']) : null, | ||
paypal: isset($data['paypal']) ? Paypal::from($data['paypal']) : null, | ||
origin: SavedPaymentMethodOrigin::from($data['origin']), | ||
savedAt: DateTime::from($data['saved_at']), | ||
updatedAt: DateTime::from($data['updated_at']), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities\Shared; | ||
|
||
class Paypal | ||
{ | ||
private function __construct( | ||
public string $email, | ||
public string $reference, | ||
) { | ||
} | ||
|
||
public static function from(array $data): self | ||
{ | ||
return new self( | ||
$data['email'], | ||
$data['reference'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities\Shared; | ||
|
||
use Paddle\SDK\PaddleEnum; | ||
|
||
/** | ||
* @method static SavedPaymentMethodOrigin SavedDuringPurchase() | ||
* @method static SavedPaymentMethodOrigin Subscription() | ||
*/ | ||
final class SavedPaymentMethodOrigin extends PaddleEnum | ||
{ | ||
private const SavedDuringPurchase = 'saved_during_purchase'; | ||
private const Subscription = 'subscription'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities\Shared; | ||
|
||
use Paddle\SDK\PaddleEnum; | ||
|
||
/** | ||
* @method static SavedPaymentMethodType Alipay() | ||
* @method static SavedPaymentMethodType ApplePay() | ||
* @method static SavedPaymentMethodType Card() | ||
* @method static SavedPaymentMethodType GooglePay() | ||
* @method static SavedPaymentMethodType Paypal() | ||
*/ | ||
final class SavedPaymentMethodType extends PaddleEnum | ||
{ | ||
private const Alipay = 'alipay'; | ||
private const ApplePay = 'apple_pay'; | ||
private const Card = 'card'; | ||
private const GooglePay = 'google_pay'; | ||
private const Paypal = 'paypal'; | ||
} |
Oops, something went wrong.