クラス
Listener
ソース ソース
ファイル: src/IPN/Listener.php
class Listener
{
/**
* AmazonPay module object
*
* @var AmazonPay
*/
private $module;
/**
* Sets `$module` member var with dependency injection.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param AmazonPay $module
* @return void
*/
public function __construct(AmazonPay $module) {
$this->module = $module;
}
/**
* Listens for IPN messages from Amazon
*
* If the IPN is valid, the associated Welcart order object is updated
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function listen() {
$body = file_get_contents('php://input');
$message = json_decode($body, true);
if (empty($message)) {
return;
}
if (!isset($message['Message'])) {
return;
}
$response = json_decode($message['Message'], true);
if (!isset($response['ObjectType']) || !isset($response['ObjectId'])) {
return;
}
$opts = $this->module->getActingOpts();
$opts['ipn_verified'] = true;
$this->module->updateActingOpts($opts);
switch ($response['ObjectType']) {
case EventNames::CHARGE:
$orderId = $this->getOrderIdFromChargeId($response['ObjectId']);
if ($orderId !== null) {
$order = new Models\OrderMeta($orderId);
if (!in_array($response['NotificationId'], $order->getIpnNotificationIds(), true)) {
$order->updateNotificationIds($response['NotificationId']);
$this->updateChargeState($order);
}
}
break;
case EventNames::REFUND:
$orderId = $this->getOrderIdFromRefundId($response['ObjectId']);
if ($orderId !== null) {
$order = new Models\OrderMeta($orderId);
if (!in_array($response['NotificationId'], $order->getIpnNotificationIds(), true)) {
$order->updateNotificationIds($response['NotificationId']);
$this->updateRefundState($order);
}
}
break;
}
}
/**
* Updates charge state
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param Models\OrderMeta $order
* @return void
*/
public function updateChargeState(Models\OrderMeta $order) {
// order state is updated automatically by this API
$result = (new API\Charge\Get($this->module, $order))->get($order->getChargeId());
if ($result instanceof GenericError) {
// 100 tells Amazon to retry later
http_response_code(100);
exit();
}
}
/**
* Updates refund state
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param Models\OrderMeta $order
* @return void
*/
public function updateRefundState(Models\OrderMeta $order) {
// order state is updated automatically by this API
$result = (new API\Refund\Get($this->module, $order))->get($order->getRefundId());
if ($result instanceof GenericError) {
// 100 tells Amazon to retry later
http_response_code(100);
exit();
}
}
/**
* Returns the order_id associated with an Amazon Pay chargeId, if one exists
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param string $chargeId
* @return int|null
*/
public function getOrderIdFromChargeId($chargeId) {
global $wpdb;
$order_meta_table_name = $wpdb->prefix . 'usces_order_meta';
$query = $wpdb->prepare(
"SELECT order_id
FROM {$order_meta_table_name}
WHERE meta_key = %s
AND meta_value = %s
ORDER BY order_id ASC
LIMIT 1",
Models\OrderMeta::CHARGE_ID,
$chargeId
);
$orderId = $wpdb->get_var($query);
if (empty($orderId)) {
return null;
}
return $orderId;
}
/**
* Returns the order_id associated with an Amazon Pay refundId, if one exists
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param string $refundId
* @return int|null
*/
public function getOrderIdFromRefundId($refundId) {
global $wpdb;
$order_meta_table_name = $wpdb->prefix . 'usces_order_meta';
$query = $wpdb->prepare(
"SELECT order_id
FROM {$order_meta_table_name}
WHERE meta_key = %s
AND meta_value = %s
ORDER BY order_id ASC
LIMIT 1",
Models\OrderMeta::REFUND_ID,
$refundId
);
$orderId = $wpdb->get_var($query);
if (empty($orderId)) {
return null;
}
return $orderId;
}
}
- __construct — Sets `$module` member var with dependency injection.
- getOrderIdFromChargeId — Returns the order_id associated with an Amazon Pay chargeId, if one exists
- getOrderIdFromRefundId — Returns the order_id associated with an Amazon Pay refundId, if one exists
- listen — Listens for IPN messages from Amazon
- updateChargeState — Updates charge state
- updateRefundState — Updates refund state