クラス
Cancel
ソース ソース
ファイル: src/API/Charge/Cancel.php
class Cancel extends ErrorStore
{
const INVALID_CHARGE_PERMISSION_STATUS = 'InvalidChargePermissionStatus';
const INVALID_CHARGE_STATUS = 'InvalidChargeStatus';
/**
* 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();
}
/**
* Cancels 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 string $reason
* @return array
* @throws InvalidArgumentException Thrown by `getAmazonClient`.
*/
public function delete($chargeId, $reason) {
$payload = ['cancellationReason' => $reason];
try {
$client = $this->module->getAmazonClient();
$result = $client->cancelCharge($chargeId, $payload);
if ($this->module->errors->hasError($result)) {
$error = $this->module->errors->getAmzErrorResponse($result, $this);
(new Logger())->logApiResponse(new Log(
$this->order->getChargePermissionId(),
Log::ACTION_CANCEL_TRANS,
$error->errorcode,
null,
$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']
)
);
(new Logger())->logApiResponse(new Log(
$this->order->getChargePermissionId(),
Log::ACTION_CANCEL_TRANS,
Log::RESPONSE_OK
));
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::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;
},
__('You tried to call an operation on a Charge that is in a state where that operation cannot be called', 'wcexaap')
));
}
}
- __construct — Sets `$module` member var with dependency injection.
- delete — Cancels a Charge
- populate — Populates error store