クラス
Create
Amazon Pay V2 SDK Create Charge.
ソース ソース
ファイル: src/API/Charge/Create.php
class Create extends ErrorStore { const TRANSACTION_AMOUNT_EXCEEDED = 'TransactionAmountExceeded'; const INVALID_CHARGE_PERMISSION_STATUS = 'InvalidChargePermissionStatus'; const SOFT_DECLINED = 'SoftDeclined'; const HARD_DECLINED = 'HardDeclined'; const TRANSACTION_COUNT_EXCEEDED = 'TransactionCountExceeded'; const PAYMENT_METHOD_NOT_ALLOWED = 'PaymentMethodNotAllowed'; const AMAZON_REJECTED = 'AmazonRejected'; const MFA_NOT_COMPLETED = 'MFANotCompleted'; const TRANSACTION_TIMED_OUT = 'TransactionTimedOut'; const PROCESSING_FAILURE = 'ProcessingFailure'; /** * AmazonPay module object * * @var AmazonPay */ private $module; /** * Amazon Pay order object * * @var OrderMeta */ private $order; /** * Sets `$module` member var with dependency injection. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param AmazonPay $module * @param OrderMeta $order * @return void */ public function __construct(AmazonPay $module, OrderMeta $order) { $this->module = $module; $this->order = $order; parent::__construct(); $this->populate(); } /** * Creates a Charge * * @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/charge.html#create-charge * @author Evan D Shaw <evandanielshaw@gmail.com> * @param string $chargePermissionId Charge Permission ID * @param int $chargeAmount * @param string $currencyCode * @param bool $captureNow Default: `false` * @param bool $canHandlePendingAuthorization Default: `false` * @param string $softDescriptor Default: '' * @return array * @throws InvalidArgumentException Thrown by `getAmazonClient`. */ public function post( $chargePermissionId, $chargeAmount, $currencyCode, $captureNow = false, $canHandlePendingAuthorization = false, $softDescriptor = '' ) { $payload = [ 'chargePermissionId' => $chargePermissionId, 'captureAmount' => [ 'amount' => $chargeAmount, 'currencyCode' => $currencyCode, ], 'captureNow' => $captureNow, 'canHandlePendingAuthorization' => $canHandlePendingAuthorization, ]; if (!empty($softDescriptor)) { $payload['softDescriptor'] = $softDescriptor; } $headers = ['x-amz-pay-Idempotency-Key' => uniqid()]; $headers = (new SandboxSimulation($this->module))->createChargeSetSimCodeIfSandbox($headers); $actionType = Log::ACTION_AUTHORIZE; if ($captureNow === true) { $actionType = Log::ACTION_AUTHORIZE_AND_CAPTURE; } try { $client = $this->module->getAmazonClient(); $result = $client->createCharge($payload, $headers); if ($this->module->errors->hasError($result)) { $error = $this->module->errors->getAmzErrorResponse($result, $this); (new Logger())->logApiResponse(new Log( $this->order->getChargePermissionId(), $actionType, $error->errorcode, $chargeAmount, $error )); return $error; } } catch (\Exception $e) { return $this->module->errors->getErrorResponse( GenericErrorStore::AMAZON_PAY_SDK_CLIENT_EXCEPTION, [$e->getMessage()] ); } $response = json_decode($result['response'], true); $details = $response['statusDetails']; $this->order->setChargeState( new State( $details['state'], $details['reasonCode'], $details['reasonDescription'] ), $response['chargeId'], $chargeAmount ); (new Logger())->logApiResponse(new Log( $this->order->getChargePermissionId(), $actionType, Log::RESPONSE_OK, $chargeAmount )); return $result; } /** * Populates error store * * @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/charge.html#error-codes * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void * @throws InvalidArgumentException Thrown if duplicate errorcodes exist. */ public function populate() { $defaultErrors = $this->getErrorCodeMap(); $defaultErrorMessage = $defaultErrors[parent::INTERNAL_SERVER_ERROR]->message; $this->addError(new GenericError( self::TRANSACTION_AMOUNT_EXCEEDED, $this->getConstantNameByValue(self::TRANSACTION_AMOUNT_EXCEEDED), 400, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::INVALID_CHARGE_PERMISSION_STATUS, $this->getConstantNameByValue(self::INVALID_CHARGE_PERMISSION_STATUS), 422, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::SOFT_DECLINED, $this->getConstantNameByValue(self::SOFT_DECLINED), 422, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::HARD_DECLINED, $this->getConstantNameByValue(self::HARD_DECLINED), 422, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::TRANSACTION_COUNT_EXCEEDED, $this->getConstantNameByValue(self::TRANSACTION_COUNT_EXCEEDED), 422, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::PAYMENT_METHOD_NOT_ALLOWED, $this->getConstantNameByValue(self::PAYMENT_METHOD_NOT_ALLOWED), 422, function ($message) { return $message; }, __('There was a problem with the selected payment method.', 'wcexaap') )); $this->addError(new GenericError( self::AMAZON_REJECTED, $this->getConstantNameByValue(self::AMAZON_REJECTED), 422, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::MFA_NOT_COMPLETED, $this->getConstantNameByValue(self::MFA_NOT_COMPLETED), 422, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::TRANSACTION_TIMED_OUT, $this->getConstantNameByValue(self::TRANSACTION_TIMED_OUT), 422, function ($message) { return $message; }, $defaultErrorMessage )); $this->addError(new GenericError( self::PROCESSING_FAILURE, $this->getConstantNameByValue(self::PROCESSING_FAILURE), 500, function ($message) { return $message; }, $defaultErrorMessage )); } }
- __construct — Sets $module member var with dependency injection.
- populate — Populates error store
- post — Creates a Charge