Example #1
0
 /**
  * Update lead status of the specified payment
  *
  * @param Pronamic_Pay_Payment $payment
  */
 public static function status_update(Pronamic_Pay_Payment $payment, $can_redirect = false)
 {
     $source_id = $payment->get_source_id();
     $order = new WC_Order((int) $source_id);
     $gateway = new Pronamic_WP_Pay_Extensions_WooCommerce_IDealGateway();
     $data = new Pronamic_WP_Pay_Extensions_WooCommerce_PaymentData($order, $gateway);
     // Only update if order is not 'processing' or 'completed'
     // @see https://github.com/woothemes/woocommerce/blob/v2.0.0/classes/class-wc-order.php#L1279
     $should_update = !Pronamic_WP_Pay_Extensions_WooCommerce_WooCommerce::order_has_status($order, array(Pronamic_WP_Pay_Extensions_WooCommerce_WooCommerce::ORDER_STATUS_COMPLETED, Pronamic_WP_Pay_Extensions_WooCommerce_WooCommerce::ORDER_STATUS_PROCESSING));
     // Defaults
     $status = null;
     $url = $data->get_normal_return_url();
     $status = $payment->get_status();
     $payment_method_title = $order->payment_method_title;
     switch ($status) {
         case Pronamic_WP_Pay_Statuses::CANCELLED:
             $url = $data->get_cancel_url();
             break;
         case Pronamic_WP_Pay_Statuses::EXPIRED:
             if ($should_update) {
                 $note = sprintf('%s %s.', $payment_method_title, __('payment expired', 'pronamic_ideal'));
                 // WooCommerce PayPal gateway uses 'failed' order status for an 'expired' payment
                 // @see http://plugins.trac.wordpress.org/browser/woocommerce/tags/1.5.4/classes/gateways/class-wc-paypal.php#L557
                 $order->update_status(Pronamic_WP_Pay_Extensions_WooCommerce_WooCommerce::ORDER_STATUS_FAILED, $note);
             }
             $url = $data->get_error_url();
             break;
         case Pronamic_WP_Pay_Statuses::FAILURE:
             if ($should_update) {
                 $note = sprintf('%s %s.', $payment_method_title, __('payment failed', 'pronamic_ideal'));
                 $order->update_status(Pronamic_WP_Pay_Extensions_WooCommerce_WooCommerce::ORDER_STATUS_FAILED, $note);
             }
             $url = $data->get_error_url();
             break;
         case Pronamic_WP_Pay_Statuses::SUCCESS:
             if ($should_update) {
                 // Payment completed
                 $order->add_order_note(sprintf('%s %s.', $payment_method_title, __('payment completed', 'pronamic_ideal')));
                 // Mark order complete
                 $order->payment_complete();
             }
             $url = $data->get_success_url();
             break;
         case Pronamic_WP_Pay_Statuses::OPEN:
             if ($should_update) {
                 $order->add_order_note(sprintf('%s %s.', $payment_method_title, __('payment open', 'pronamic_ideal')));
             }
             break;
         default:
             if ($should_update) {
                 $order->add_order_note(sprintf('%s %s.', $payment_method_title, __('payment unknown', 'pronamic_ideal')));
             }
             break;
     }
     if ($can_redirect) {
         wp_redirect($url);
         exit;
     }
 }