• プラグイン一覧
    - WCEX Item Combo Set
    - WCEX Amazon Pay
    - WCEX Wishlist お気に入りリスト
  • リリース情報
  • お役立ちコラム
  • お問い合わせ
  • サポート
    • よくある質問
      • WCEX Amazon Pay
      • WCEX Wishlist お気に入りリスト
      • wcex-item-combo-set
    • リファレンス
      • WCEX Amazon Pay
      • WCEX Wishlist お気に入りリスト
      • wcex-item-combo-set
新規会員登録
ログイン
新規会員登録
ログイン
カート
  • プラグイン一覧
    • - WCEX Item Combo Set
    • - WCEX Amazon Pay
    • - WCEX Wishlist お気に入りリスト
  • リリース情報
  • お役立ちコラム
  • サポート
    • - よくある質問
      • - WCEX Amazon Pay
      • - WCEX Wishlist お気に入りリスト
      • - wcex-item-combo-set
    • - リファレンス
      • - WCEX Amazon Pay
      • - WCEX Wishlist お気に入りリスト
      • - wcex-item-combo-set
  • お問い合わせ
Aivec APPs > WCEX Amazon Pay > クラス > Listener
レファレンス
バージョン
2.6.4
絞り込み:

目次

  • ソース
  • 関数

フック

  • アクション
  • フィルター

ファンクション

    クラス

    Listener

    Handles Instant Payment Notifications (IPN) for asynchronous operations

    ソース #ソース

    ファイル: 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;
        }
    }
    

    ソースを伸ばす ソースを縮める


    関数 #関数

    Top ↑

    • __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

    • 新規会員登録
    • ログイン
      • プラグイン一覧
      • 会社概要
      • リリース情報
      • よくある質問
      • お役立ちコラム
      • お問い合わせ
      • 個人情報保護方針
      • 特定商取引法に基づく表記
      • 情報セキュリティ基本方針
      • 利用規約

    アイベック合同会社は「Welcart」「Amazon Pay」の公式パートナーです。

    ※Amazon、Amazon.co.jp、Amazon Payおよびそれらのロゴは、Amazon.com,inc.またはその関連会社の商標です。
    ※LINE Pay、およびLINE Pay 提携サービスのロゴは、法律上保護を受ける商標です。

    © 2025 Aivec llc All Rights Reserved.