fn_pay_validate_and_complete

Confirm and Verify the Saferpay payment this methos is called by Saferpay using the notify_url of the gateway.

Description

(string) fn_pay_validate_and_complete( $gateway, $posted ); 

The process runs in the background and we can't inform the user directly if an error occurs.

Returns (string)

- returns the state of the payment

Parameters (2)

0. $gateway
The gateway.
1. $posted
The posted.

Usage

  1. if ( !function_exists( 'fn_pay_validate_and_complete' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-payment-gateway-for-saferpay/fn-saferpay.php'; 
  3.  
  4. // The gateway. 
  5. $gateway = null; 
  6.  
  7. // The posted. 
  8. $posted = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = fn_pay_validate_and_complete($gateway, $posted); 
  12.  

Defined (1)

The function is defined in the following location(s).

/fn-saferpay.php  
  1. function fn_pay_validate_and_complete($gateway, $posted) 
  2. global $woocommerce; 
  3.  
  4. if (!empty($posted) && fn_pay_confirm_is_valid($gateway, $posted))  
  5.  
  6. // At this point we know that the payment is reserved and valid 
  7. $data = stripcslashes($posted['DATA']); 
  8. $xml = simplexml_load_string($data); 
  9.  
  10. if($xml) 
  11. $id = fn_simplexml_attribute($xml, 'ID'); 
  12. $amount = fn_simplexml_attribute($xml, 'AMOUNT'); 
  13.  
  14. // Get the order from the server 
  15. $orderid_with_prefix = fn_simplexml_attribute($xml, 'ORDERID'); 
  16.  
  17. // Get the order id without prefix 
  18. $orderid = str_replace($gateway->order_id_prefix, '', $orderid_with_prefix); 
  19.  
  20. $gateway->alert_admin($orderid . " -- " . $gateway->saferpay_accountid); 
  21.  
  22. $order = new WC_Order($orderid); 
  23.  
  24. // Validate Status 
  25. if($order->status == 'completed' || $order->status == 'processing') 
  26. // Add a note  
  27. $order->add_order_note( __( 'Aborting, order already processing or completed', 'fnwc-woocommerce' ) ); 
  28.  
  29. return "ERROR: " . __('Order already processing or completed', 'fnwc-woocommerce'); 
  30.  
  31. // Validate Amount 
  32. $_amount = round($order->get_total(), 2) * 100; 
  33.  
  34. if($_amount != $amount) 
  35. // Update the post meta 
  36. update_post_meta($order->id, 'Payment alert', __('Payment amount mismatch', 'fnwc-woocommerce')); 
  37.  
  38. return "ERROR: " . __('Payment amount mismatch', 'fnwc-woocommerce'); 
  39.  
  40. // Update Order data 
  41. update_post_meta($order->id, 'Payment type', "Saferpay"); 
  42. update_post_meta($order->id, 'Transaction ID', $id); 
  43.  
  44. /** 
  45. * Complete the payment 
  46. */ 
  47. $completion = fn_complete_saferpay_payment($gateway, $id); 
  48. // Check completion 
  49.  
  50.  
  51.  
  52.  
  53. $order->add_order_note( __( 'Saferpay payment completed', 'fnwc-woocommerce' ) ); 
  54. $order->payment_complete(); /** Smart method - recognizes download vs delivery */ 
  55.  
  56. // Empty cart (silently and gracefully) 
  57. @$woocommerce->cart->empty_cart(); 
  58.  
  59. return "OK"; 
  60.  
  61.  
  62. return "ERROR: " . __('Payment not completed', 'fnwc-woocommerce'); 
  63. else 
  64.  
  65. return "ERROR: " . __('Payment could not be validated', 'fnwc-woocommerce'); 
  66.