クラス
PollingCron
Cron job for polling Amazon Pay APIs as a fallback in case IPNs don’t work
ソース ソース
ファイル: src/Admin/PollingCron.php
class PollingCron { /** * Cron event name */ const EVENT_NAME = 'AmazonPayPolling'; /** * 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; } /** * Clear cron event * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void */ public function clearEvent() { wp_clear_scheduled_hook(self::EVENT_NAME); } /** * Schedules polling cron if not already scheduled * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return bool */ public function scheduleEvent() { add_action(self::EVENT_NAME, [$this, 'updateAllOrders']); $cron = wp_next_scheduled(self::EVENT_NAME); if ($cron) { return false; } $unixts = (new DateTime('now', new DateTimeZone('UTC')))->getTimestamp(); return wp_schedule_event($unixts, 'hourly', self::EVENT_NAME); } /** * Loops through all Amazon Pay orders and updates transaction states accordingly * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void * @throws InvalidArgumentException Thrown by `OrderMeta` constructor if order doesn't exist. */ public function updateAllOrders() { $orderIds = $this->getAllAmazonPayOrders(); foreach ($orderIds as $oa) { $orderId = (int)$oa['order_id']; $ometa = new Models\OrderMeta($orderId); // order state is updated automatically by these API calls if ($ometa->getChargeId() !== null) { $chargeState = $ometa->getChargeState(); if (!$chargeState->isCanceled() && !$chargeState->isCaptured()) { (new API\Charge\Get($this->module, $ometa))->get($ometa->getChargeId()); } } if ($ometa->getRefundId() !== null) { (new API\Refund\Get($this->module, $ometa))->get($ometa->getRefundId()); } } } /** * Returns all order IDs associated with an Amazon Pay order * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @return array */ public function getAllAmazonPayOrders() { 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 ORDER BY order_id ASC", Models\OrderMeta::CHARGE_PERMISSION_ID ); $orderIds = $wpdb->get_results($query, ARRAY_A); if (empty($orderIds)) { return []; } return $orderIds; } }
- __construct — Sets $module member var with dependency injection.
- clearEvent — Clear cron event
- getAllAmazonPayOrders — Returns all order IDs associated with an Amazon Pay order
- scheduleEvent — Schedules polling cron if not already scheduled
- updateAllOrders — Loops through all Amazon Pay orders and updates transaction states accordingly