Exemplo n.º 1
0
 public function webhookAction()
 {
     // Webhook test by Mollie
     if (isset($_GET['testByMollie'])) {
         Mollie_WC_Plugin::debug(__METHOD__ . ': Webhook tested by Mollie.', true);
         return;
     }
     if (empty($_GET['order_id']) || empty($_GET['key'])) {
         Mollie_WC_Plugin::setHttpResponseCode(400);
         Mollie_WC_Plugin::debug(__METHOD__ . ":  No order ID or order key provided.");
         return;
     }
     $order_id = $_GET['order_id'];
     $key = $_GET['key'];
     $data_helper = Mollie_WC_Plugin::getDataHelper();
     $order = $data_helper->getWcOrder($order_id);
     if (!$order) {
         Mollie_WC_Plugin::setHttpResponseCode(404);
         Mollie_WC_Plugin::debug(__METHOD__ . ":  Could not find order {$order_id}.");
         return;
     }
     if (!$order->key_is_valid($key)) {
         Mollie_WC_Plugin::setHttpResponseCode(401);
         Mollie_WC_Plugin::debug(__METHOD__ . ":  Invalid key {$key} for order {$order_id}.");
         return;
     }
     // No Mollie payment id provided
     if (empty($_REQUEST['id'])) {
         Mollie_WC_Plugin::setHttpResponseCode(400);
         Mollie_WC_Plugin::debug(__METHOD__ . ': No payment ID provided.', true);
         return;
     }
     $payment_id = $_REQUEST['id'];
     $test_mode = $data_helper->getActiveMolliePaymentMode($order_id) == 'test';
     // Load the payment from Mollie, do not use cache
     $payment = $data_helper->getPayment($payment_id, $test_mode, $use_cache = false);
     // Payment not found
     if (!$payment) {
         Mollie_WC_Plugin::setHttpResponseCode(404);
         Mollie_WC_Plugin::debug(__METHOD__ . ": payment {$payment_id} not found.", true);
         return;
     }
     if ($order_id != $payment->metadata->order_id) {
         Mollie_WC_Plugin::setHttpResponseCode(400);
         Mollie_WC_Plugin::debug(__METHOD__ . ": Order ID does not match order_id in payment metadata. Payment ID {$payment->id}, order ID {$order_id}");
         return;
     }
     // Payment requires different gateway, payment method changed on Mollie platform?
     if ($payment->method != $this->getMollieMethodId()) {
         Mollie_WC_Plugin::setHttpResponseCode(400);
         Mollie_WC_Plugin::debug($this->id . ": Invalid gateway. This gateways can process Mollie " . $this->getMollieMethodId() . " payments. This payment has payment method " . $payment->method, true);
         return;
     }
     // Order does not need a payment
     if (!$this->orderNeedsPayment($order)) {
         // Duplicate webhook call
         Mollie_WC_Plugin::setHttpResponseCode(204);
         Mollie_WC_Plugin::debug($this->id . ": Order {$order_id} does not need a payment (payment webhook {$payment->id}).", true);
         return;
     }
     Mollie_WC_Plugin::debug($this->id . ": Mollie payment {$payment->id} (" . $payment->mode . ") webhook call for order {$order->id}.", true);
     $method_name = 'onWebhook' . ucfirst($payment->status);
     if (method_exists($this, $method_name)) {
         $this->{$method_name}($order, $payment);
     } else {
         $order->add_order_note(sprintf(__('%s payment %s (%s).', 'mollie-payments-for-woocommerce'), $this->method_title, $payment->status, $payment->id . ($payment->mode == 'test' ? ' - ' . __('test mode', 'mollie-payments-for-woocommerce') : '')));
     }
     // Status 200
 }