クラス
OrderMeta
ソース ソース
ファイル: src/Models/OrderMeta.php
class OrderMeta
{
const CHARGE_PERMISSION_ID = 'amazonChargePermissionId';
const CHARGE_ID = 'amazonChargeId';
const REFUND_ID = 'amazonRefundId';
const CHARGE_STATE = 'amazonChargeState';
const REFUND_STATE = 'amazonRefundState';
const LINK_KEY = 'amazonLinkKey';
const PAYMENT_CURRENCY = 'amazonPaymentCurrency';
const MOST_RECENT_TRANS_AMOUNT = 'amazonMostRecentTransAmount';
const IPN_NOTIFICATION_IDS = 'amazonIpnNotificationIds';
/**
* Welcart order ID
*
* @var int
*/
private $orderid;
/**
* Payment currency
*
* @var string
*/
private $paymentCurrency;
/**
* Amount of the most recent transaction that involved a payment of some kind
*
* @var int|string
*/
private $mostRecentTransactionAmount;
/**
* `chargePermissionId` returned by `Get Checkout Session` API
*
* @var string
*/
private $chargePermissionId;
/**
* `chargeId` returned by `Get Checkout Session` API
*
* @var string|null
*/
private $chargeId;
/**
* `refundId` returned by `Create Refund` API
*
* @var string|null
*/
private $refundId = null;
/**
* Link key for the 直前決済ログ entry
*
* @var string|null
*/
private $linkKey = null;
/**
* Charge state object for state tracking
*
* @var Charge\State|null
*/
private $chargeState;
/**
* Refund state object for state tracking
*
* @var Refund\State|null
*/
private $refundState = null;
/**
* List of processed IPN ids
*
* @var string[]
*/
private $ipnNotificationIds = [];
/**
* Constructs an AmazonPay order object from an existing transaction
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param int $orderid
* @return void
* @throws InvalidArgumentException Thrown if no such Amazon Pay order exists for the given order ID.
*/
public function __construct($orderid) {
global $usces;
$this->orderid = (int)$orderid;
$this->chargeId = $usces->get_order_meta_value(self::CHARGE_ID, $this->orderid);
$this->chargePermissionId = $usces->get_order_meta_value(self::CHARGE_PERMISSION_ID, $this->orderid);
$this->refundId = $usces->get_order_meta_value(self::REFUND_ID, $this->orderid);
$this->linkKey = $usces->get_order_meta_value(self::LINK_KEY, $this->orderid);
$this->paymentCurrency = $usces->get_order_meta_value(self::PAYMENT_CURRENCY, $this->orderid);
$ipnNotificationIds = $usces->get_order_meta_value(self::IPN_NOTIFICATION_IDS, $this->orderid);
$this->mostRecentTransactionAmount = $usces->get_order_meta_value(
self::MOST_RECENT_TRANS_AMOUNT,
$this->orderid
);
if (empty($this->chargePermissionId)) {
throw new InvalidArgumentException('No such Amazon Pay order exists for the given order ID');
}
if (empty($ipnNotificationIds)) {
$this->ipnNotificationIds = [];
} else {
$this->ipnNotificationIds = json_decode($ipnNotificationIds);
}
if (!empty($this->chargeId)) {
$chargeState = json_decode($usces->get_order_meta_value(self::CHARGE_STATE, $this->orderid), true);
$this->chargeState = new Charge\State(
$chargeState['state'],
$chargeState['reasonCode'],
$chargeState['reasonDescription']
);
}
if (!empty($this->refundId)) {
$refundState = json_decode($usces->get_order_meta_value(self::REFUND_STATE, $this->orderid), true);
$this->refundState = new Refund\State(
$refundState['state'],
$refundState['reasonCode'],
$refundState['reasonDescription']
);
}
}
/**
* Saves Amazon Pay order meta data with `Get Checkout Session` response and charge
* state after a successful purchase
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param array $response
* @param int $orderid
* @param int|string $amount authorize or capture amount
* @param string $currencyCode
* @param Charge\State|null $chargeState
* @return void
*/
public static function saveOrderMetaDataFromCheckoutSession(
array $response,
$orderid,
$amount,
$currencyCode,
Charge\State $chargeState = null
) {
global $usces;
$chargePermissionId = $response['chargePermissionId'];
$chargeId = isset($response['chargeId']) ? $response['chargeId'] : null;
$serializedChargeState = $chargeState !== null ? json_encode($chargeState) : null;
$usces->set_order_meta_value(self::CHARGE_ID, $chargeId, $orderid);
$usces->set_order_meta_value(self::CHARGE_PERMISSION_ID, $chargePermissionId, $orderid);
$usces->set_order_meta_value(self::LINK_KEY, $response['checkoutSessionId'], $orderid);
$usces->set_order_meta_value(self::CHARGE_STATE, $serializedChargeState, $orderid);
$usces->set_order_meta_value(self::MOST_RECENT_TRANS_AMOUNT, $amount, $orderid);
$usces->set_order_meta_value(self::PAYMENT_CURRENCY, $currencyCode, $orderid);
$usces->set_order_meta_value('wc_trans_id', $chargePermissionId, $orderid);
if ($chargeState !== null) {
$state = $chargeState->getState();
if (!empty(Charge\State::STATE_TO_LOG_ACTION[$state])) {
(new Logger())->logApiResponse(new Log(
$chargePermissionId,
Charge\State::STATE_TO_LOG_ACTION[$state],
Log::RESPONSE_OK,
$amount
));
}
} else {
(new Logger())->logApiResponse(new Log(
$chargePermissionId,
Log::ACTION_DEFERRED_AUTHORIZE,
Log::RESPONSE_OK,
$amount
));
}
}
/**
* Updates charge state
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param Charge\State $chargeState
* @return void
*/
public function updateChargeState(Charge\State $chargeState) {
global $usces;
$this->chargeState = $chargeState;
$usces->set_order_meta_value(self::CHARGE_STATE, json_encode($this->chargeState), $this->orderid);
}
/**
* Updates refund state
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param Refund\State $refundState
* @param int|string|null $refundAmount
* @return void
*/
public function updateRefundState(Refund\State $refundState, $refundAmount = null) {
global $usces;
$this->refundState = $refundState;
$usces->set_order_meta_value(self::REFUND_STATE, json_encode($this->refundState), $this->orderid);
if ($refundAmount) {
$this->mostRecentTransactionAmount = $refundAmount;
$usces->set_order_meta_value(self::MOST_RECENT_TRANS_AMOUNT, $refundAmount, $this->orderid);
}
}
/**
* Adds IPN notification ID to list if it doesn't already exist
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param string $id
* @return void
*/
public function updateNotificationIds($id) {
global $usces;
if (!in_array($id, $this->ipnNotificationIds, true)) {
$this->ipnNotificationIds[] = $id;
$usces->set_order_meta_value(
self::IPN_NOTIFICATION_IDS,
json_encode($this->ipnNotificationIds),
$this->orderid
);
}
}
/**
* Returns the current transaction state
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return TransactionState
*/
public function getTransactionState() {
if (!empty($this->refundId)) {
$this->refundState->setChargePermissionId($this->getChargePermissionId());
return $this->refundState;
} elseif (!empty($this->chargeId)) {
$this->chargeState->setChargePermissionId($this->getChargePermissionId());
return $this->chargeState;
} else {
$deferred = new DeferredAuthorize\State(DeferredAuthorize\State::DEFERRED_AUTHORIZE);
$deferred->setChargePermissionId($this->getChargePermissionId());
return $deferred;
}
}
/**
* Sets Refund state object for the order
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param Refund\State $refundState
* @param string $refundId
* @param int|string $refundAmount
* @return void
*/
public function setRefundState(Refund\State $refundState, $refundId, $refundAmount) {
global $usces;
$this->refundState = $refundState;
$this->refundId = $refundId;
$this->mostRecentTransactionAmount = $refundAmount;
$usces->set_order_meta_value(self::REFUND_STATE, json_encode($this->refundState), $this->orderid);
$usces->set_order_meta_value(self::REFUND_ID, $this->refundId, $this->orderid);
$usces->set_order_meta_value(self::MOST_RECENT_TRANS_AMOUNT, $refundAmount, $this->orderid);
}
/**
* Sets Charge state object for the order
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @param Charge\State $chargeState
* @param string $chargeId
* @param int|string $chargeAmount
* @return void
*/
public function setChargeState(Charge\State $chargeState, $chargeId, $chargeAmount) {
global $usces;
$this->chargeState = $chargeState;
$this->chargeId = $chargeId;
$this->mostRecentTransactionAmount = $chargeAmount;
$usces->set_order_meta_value(self::CHARGE_STATE, json_encode($this->chargeState), $this->orderid);
$usces->set_order_meta_value(self::CHARGE_ID, $this->chargeId, $this->orderid);
$usces->set_order_meta_value(self::MOST_RECENT_TRANS_AMOUNT, $chargeAmount, $this->orderid);
}
/**
* Getter for `orderid`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return int
*/
public function getOrderId() {
return $this->orderid;
}
/**
* Getter for `chargePermissionId`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function getChargePermissionId() {
return $this->chargePermissionId;
}
/**
* Getter for `chargeId`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string|null
*/
public function getChargeId() {
return $this->chargeId;
}
/**
* Getter for `refundId`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string|null
*/
public function getRefundId() {
return $this->refundId;
}
/**
* Getter for `linkKey`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string|null
*/
public function getLinkKey() {
return $this->linkKey;
}
/**
* Getter for `chargeState`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Charge\State|null
*/
public function getChargeState() {
return $this->chargeState;
}
/**
* Getter for `refundState`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return Refund\State|null
*/
public function getRefundState() {
return $this->refundState;
}
/**
* Getter for `paymentCurrency`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function getPaymentCurrency() {
return $this->paymentCurrency;
}
/**
* Getter for `mostRecentTransactionAmount`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return int|string
*/
public function getMostRecentTransactionAmount() {
return $this->mostRecentTransactionAmount;
}
/**
* Getter for `ipnNotificationIds`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string[]
*/
public function getIpnNotificationIds() {
return $this->ipnNotificationIds;
}
}
- __construct — Constructs an AmazonPay order object from an existing transaction
- getChargeId — Getter for `chargeId`
- getChargePermissionId — Getter for `chargePermissionId`
- getChargeState — Getter for `chargeState`
- getIpnNotificationIds — Getter for `ipnNotificationIds`
- getLinkKey — Getter for `linkKey`
- getMostRecentTransactionAmount — Getter for `mostRecentTransactionAmount`
- getOrderId — Getter for `orderid`
- getPaymentCurrency — Getter for `paymentCurrency`
- getRefundId — Getter for `refundId`
- getRefundState — Getter for `refundState`
- getTransactionState — Returns the current transaction state
- saveOrderMetaDataFromCheckoutSession — Saves Amazon Pay order meta data with `Get Checkout Session` response and charge state after a successful purchase
- setChargeState — Sets Charge state object for the order
- setRefundState — Sets Refund state object for the order
- updateChargeState — Updates charge state
- updateNotificationIds — Adds IPN notification ID to list if it doesn't already exist
- updateRefundState — Updates refund state