クラス
Capture
Amazon Pay V2 SDK Capture Charge.
ソース ソース
ファイル: src/API/Charge/Capture.php
class Capture extends ErrorStore { const TRANSACTION_AMOUNT_EXCEEDED = 'TransactionAmountExceeded'; const TRANSACTION_COUNT_EXCEEDED = 'TransactionCountExceeded'; const INVALID_CHARGE_PERMISSION_STATUS = 'InvalidChargePermissionStatus'; const INVALID_CHARGE_STATUS = 'InvalidChargeStatus'; const AMAZON_REJECTED = 'AmazonRejected'; 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(); } /** * Captures a Charge * * @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/charge.html#capture-charge * @author Evan D Shaw <evandanielshaw@gmail.com> * @param string $chargeId Charge ID * @param int $amount * @param string $currencyCode should be the same as was set during checkout * @param string $softDescriptor * @return array * @throws InvalidArgumentException Thrown by `getAmazonClient`. */ public function post($chargeId, $amount, $currencyCode, $softDescriptor = '') { $payload = [ 'captureAmount' => [ 'amount' => $amount, 'currencyCode' => $currencyCode, ], ]; if (!empty($softDescriptor)) { $payload['softDescriptor'] = $softDescriptor; } $headers = ['x-amz-pay-Idempotency-Key' => uniqid()]; // $_ENV[SandboxSimulation::CAPTURE_CHARGE_SIMCODE] = 'CaptureInitiated'; // $_ENV[SandboxSimulation::CAPTURE_CHARGE_SIMCODE] = 'HardDeclined'; $headers = (new SandboxSimulation($this->module))->captureChargeSetSimCodeIfSandbox($headers); try { $client = $this->module->getAmazonClient(); $result = $client->captureCharge($chargeId, $payload, $headers); if ($this->module->errors->hasError($result)) { $error = $this->module->errors->getAmzErrorResponse($result, $this); (new Logger())->logApiResponse(new Log( $this->order->getChargePermissionId(), Log::ACTION_CAPTURE_PAYMENTS, $error->errorcode, $amount, $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->updateChargeState( new State( $details['state'], $details['reasonCode'], $details['reasonDescription'] ) ); if (!empty(State::STATE_TO_LOG_ACTION[$details['state']])) { (new Logger())->logApiResponse(new Log( $this->order->getChargePermissionId(), State::STATE_TO_LOG_ACTION[$details['state']], Log::RESPONSE_OK, $amount )); } return $result; } /** * Populates error store * * @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/charge.html#error-codes-2 * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void * @throws InvalidArgumentException Thrown if duplicate errorcodes exist. */ public function populate() { $this->addError(new GenericError( self::TRANSACTION_AMOUNT_EXCEEDED, $this->getConstantNameByValue(self::TRANSACTION_AMOUNT_EXCEEDED), 400, function ($message) { return $message; }, __('Transaction amount exceeded.', 'wcexaap') )); $this->addError(new GenericError( self::TRANSACTION_COUNT_EXCEEDED, $this->getConstantNameByValue(self::TRANSACTION_COUNT_EXCEEDED), 422, function ($message) { return $message; }, __("You've exceeded the maximum limit of 1 successfully captured Charge, per Charge Permission", 'wcexaap') )); $this->addError(new GenericError( self::INVALID_CHARGE_PERMISSION_STATUS, $this->getConstantNameByValue(self::INVALID_CHARGE_PERMISSION_STATUS), 422, function ($message) { return $message; }, __('You tried to call an operation on a Charge Permission that is in a state where that operation cannot be called', 'wcexaap') )); $this->addError(new GenericError( self::INVALID_CHARGE_STATUS, $this->getConstantNameByValue(self::INVALID_CHARGE_STATUS), 422, function ($message) { return $message; }, __('This order has already been processed', 'wcexaap') )); $this->addError(new GenericError( self::AMAZON_REJECTED, $this->getConstantNameByValue(self::AMAZON_REJECTED), 422, function ($message) { return $message; }, __('The request to capture payment was rejected by Amazon.', 'wcexaap') )); $this->addError(new GenericError( self::PROCESSING_FAILURE, $this->getConstantNameByValue(self::PROCESSING_FAILURE), 422, function ($message) { return $message; }, __('Amazon was unable to process the charge due to an internal error.', 'wcexaap') )); } }
- __construct — Sets $module member var with dependency injection.
- populate — Populates error store
- post — Captures a Charge