クラス
Create
ソース ソース
ファイル: src/API/Refund/Create.php
class Create extends ErrorStore
{
const TRANSACTION_AMOUNT_EXCEEDED = 'TransactionAmountExceeded';
const INVALID_CHARGE_STATUS = 'InvalidChargeStatus';
const TRANSACTION_COUNT_EXCEEDED = 'TransactionCountExceeded';
const PAYMENT_METHOD_NOT_ALLOWED = 'PaymentMethodNotAllowed';
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();
}
/**
* Creates a Refund
*
* @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/refund.html#create-refund
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $chargeId Charge ID
* @param int $refundAmount
* @param string $currencyCode
* @param string $softDescriptor Default: ''
* @return array
* @throws InvalidArgumentException Thrown by `getAmazonClient`.
*/
public function post(
$chargeId,
$refundAmount,
$currencyCode,
$softDescriptor = ''
) {
$payload = [
'chargeId' => $chargeId,
'refundAmount' => [
'amount' => $refundAmount,
'currencyCode' => $currencyCode,
],
];
if (!empty($softDescriptor)) {
$payload['softDescriptor'] = $softDescriptor;
}
$headers = ['x-amz-pay-Idempotency-Key' => uniqid()];
// $_ENV[SandboxSimulation::CREATE_REFUND_SIMCODE] = 'AmazonRejected';
$headers = (new SandboxSimulation($this->module))->createRefundSetSimCodeIfSandbox($headers);
try {
$client = $this->module->getAmazonClient();
$result = $client->createRefund($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_ASYNC_REFUND,
$error->errorcode,
$refundAmount,
$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->setRefundState(
new State(
$details['state'],
$details['reasonCode'],
$details['reasonDescription']
),
$response['refundId'],
$refundAmount
);
(new Logger())->logApiResponse(new Log(
$this->order->getChargePermissionId(),
Log::ACTION_ASYNC_REFUND,
Log::RESPONSE_OK,
$refundAmount
));
return $result;
}
/**
* Populates error store
*
* @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/refund.html#error-codes
* @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;
},
__('You\'ve exceeded the maximum refund amount allowed for this Refund', 'wcexaap')
));
$this->addError(new GenericError(
self::INVALID_CHARGE_STATUS,
$this->getConstantNameByValue(self::INVALID_CHARGE_STATUS),
422,
function ($message) {
return $message;
},
__('You tried to call a Refund operation on a Charge that is not in a Completed state. Check the Charge status for more information', '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 10 Refunds per Charge', 'wcexaap')
));
$this->addError(new GenericError(
self::AMAZON_REJECTED,
$this->getConstantNameByValue(self::AMAZON_REJECTED),
422,
function ($message) {
return $message;
},
__('Amazon has rejected the refund. You should issue a refund to the buyer in an alternate manner (for example, a gift card or store credit)', 'wcexaap')
));
$this->addError(new GenericError(
self::PROCESSING_FAILURE,
$this->getConstantNameByValue(self::PROCESSING_FAILURE),
500,
function ($message) {
return $message;
},
__('Amazon could not process the transaction because of an internal processing error or because the buyer has already received a refund from an A-to-z claim or a chargeback. You should only retry the Refund if the Charge object is in the Captured state. Otherwise, you should refund the buyer in an alternative way (for example, a store credit or a check)', 'wcexaap')
));
}
}
- __construct — Sets `$module` member var with dependency injection.
- populate — Populates error store
- post — Creates a Refund