クラス
Create
Amazon Pay V2 SDK Checkout Session create.
ソース ソース
ファイル: src/API/CheckoutSession/Create.php
class Create { /** * Key of checkout session ID returned by `createCheckoutSession` */ const CHECKOUT_SESSION_ID_KEY = 'checkoutSessionId'; /** * AmazonPay module object * * @var AmazonPay */ private $module; /** * Redirect URL for checkout review page * * @var string */ private $checkoutReviewUrl; /** * Sets `$module` member var with dependency injection. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param AmazonPay $module * @param string $checkoutReviewUrl * @return void */ public function __construct(AmazonPay $module, $checkoutReviewUrl) { $this->module = $module; $this->checkoutReviewUrl = $checkoutReviewUrl; } /** * Creates a new checkout session * * @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/checkout-session.html#create-checkout-session * @author Evan D Shaw <evandanielshaw@gmail.com> * @return array * @throws InvalidArgumentException Thrown by `getAmazonClient`. */ public function post() { $opts = $this->module->getActingOpts(); $payload = [ 'webCheckoutDetails' => [ 'checkoutReviewReturnUrl' => $this->checkoutReviewUrl, ], 'platformId' => Constants::PLATFORM_ID, 'storeId' => $opts['client_id'], ]; $headers = ['x-amz-pay-Idempotency-Key' => uniqid()]; try { $client = $this->module->getAmazonClient(); // call `generateButtonSignature` so that an error is thrown if the merchant's // public/private key pair is not correct $client->generateButtonSignature($payload); $result = $client->createCheckoutSession($payload, $headers); if ($this->module->errors->hasError($result)) { return $this->module->errors->getAmzErrorResponse($result); } } catch (\Exception $e) { return $this->module->errors->getErrorResponse( GenericErrorStore::AMAZON_PAY_SDK_CLIENT_EXCEPTION, [$e->getMessage()] ); } return json_decode($result['response'], true)['checkoutSessionId']; } }
- __construct — Sets $module member var with dependency injection.
- post — Creates a new checkout session