/** * Process IPN messages from Amazon * * @access public * @since 4.0 * @return void */ public function process_ipn() { if (!isset($_GET['wpsc-listener']) || $_GET['wpsc-listener'] !== 'amazon') { return; } if (isset($_GET['state'])) { return; } // Get the IPN headers and Message body $headers = getallheaders(); $body = file_get_contents('php://input'); $this->doing_ipn = true; if (!class_exists('PayWithAmazon\\IpnHandler')) { require_once WPSC_MERCHANT_V3_SDKS_PATH . '/amazon-payments/sdk/IpnHandler.php'; } try { $ipn = new PayWithAmazon\IpnHandler($headers, $body); $data = $ipn->toArray(); $seller_id = $data['SellerId']; if ($seller_id != $this->gateway->seller_id) { wp_die(__('Invalid Amazon seller ID', 'wpsc'), __('IPN Error', 'wpsc'), array('response' => 401)); } switch ($data['NotificationType']) { case 'OrderReferenceNotification': break; case 'PaymentAuthorize': break; case 'PaymentCapture': $status = $data['CaptureDetails']['CaptureStatus']['State']; if ('Declined' === $status) { $value = $data['CaptureDetails']['CaptureReferenceId']; $reason = $data['CaptureDetails']['CaptureStatus']['ReasonCode']; // Get Order ID by reference $order = WPSC_Purchase_Log::get_log_by_meta('amazon_capture_id', $value); if (!$order) { break; } // Update status to declined $order->set('processed', WPSC_Purchase_Log::PAYMENT_DECLINED)->save(); // Update Amazon note $order->set('amazon-status', __('Could not authorize Amazon payment.', 'wpsc'))->save(); // Email user $hard = 'InvalidPaymentMethod' == $reason; $this->send_decline_email($hard, $order); } break; case 'PaymentRefund': $refund_id = $data['RefundDetails']['AmazonRefundId']; $status = $data['RefundDetails']['RefundStatus']['State']; $amount = $data['RefundDetails']['RefundAmount']; if ('Completed' === $status) { // get payment ID based on refund ID $order = WPSC_Purchase_Log::get_log_by_meta('amazon_refund_id', $refund_id); // Update status to refunded $order->set('processed', WPSC_Purchase_Log::REFUNDED)->save(); // Add payment note for refund. $order->set('amazon-status', sprintf(__('Refunded %s', 'wpsc'), wpsc_currency_display($amount)))->save(); // Update refund ID wpsc_add_purchase_meta($order->get('id'), 'amazon_refund_id', $refund_id); } break; } } catch (Exception $e) { wp_die($e->getErrorMessage(), __('IPN Error', 'wpsc'), array('response' => 401)); } }