FNWC_Saferpay_Gateway

Safer Payment Gateway.

Defined (1)

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

/class-fnwc-gateway-saferpay.php  
  1. class FNWC_Saferpay_Gateway extends WC_Payment_Gateway { 
  2.  
  3.  
  4.  
  5. public function __construct() { 
  6.  
  7. global $woocommerce; 
  8.  
  9.  
  10. // 1. Required settings 
  11. // see http://docs.woothemes.com/document/payment-gateway-api/ 
  12. $this->id = 'saferpay';  
  13. $this->classname = 'FNWC_Saferpay_Gateway'; 
  14. $this->icon = plugins_url( '/assets/images/credit_cards.png' , __FILE__ ) ; 
  15.  
  16. $this->method_title = 'Saferpay'; 
  17. $this->method_description = __('WooCommerce Payment Gateway for Saferpay', 'fnwc-woocommerce'); 
  18. $this->has_fields = false;  
  19.  
  20. // The notify URL for the Saferpay response (currently no https) 
  21. $this->notify_url = home_url('/wc-api/fnwc_saferpay_gateway'); 
  22. $this->successlink = home_url('/wc-api/fnwc_saferpay_gateway'); 
  23.  
  24.  
  25.  
  26. // 2. Init the settings and the form fields  
  27. $this->init_form_fields(); 
  28. $this->init_settings(); 
  29.  
  30.  
  31. // 3. Get/Set the options 
  32. $this->title = $this->get_option( 'title' ); 
  33. $this->saferpay_accountid = $this->get_option( 'accountid' ); 
  34. $this->order_description = $this->get_option( 'order_description' ); 
  35. $this->order_id_prefix = $this->get_option( 'order_id_prefix' ); 
  36.  
  37. if ( $this->enabled && is_admin() ) { 
  38. $this->install_saferpay_page(); 
  39.  
  40. // 4. Add the actions 
  41. if (is_admin()) 
  42. // Update options 
  43. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); 
  44.  
  45.  
  46. //  
  47. /**  
  48. * Note: WooCommerce Callback API 
  49. * We are now using the notify URL -> we can use the WC-API 
  50. */ 
  51. add_action('woocommerce_api_' . 'fnwc_saferpay_gateway', array($this, 'saferpay_notify')); /** Sanitized lower key classname */ 
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. /** 
  59. * install_saferpay_page function. 
  60. * @access public 
  61. */ 
  62. public function install_saferpay_page() { 
  63.  
  64. $saferpay_page_id = get_option( 'saferpay_page_id', 0); 
  65.  
  66. if ($saferpay_page_id == 0) { 
  67.  
  68. $pid = woocommerce_get_page_id('checkout'); 
  69.  
  70. $page = array( 
  71. 'post_title' => 'Saferpay',  
  72. 'post_name' => 'saferpay',  
  73. 'post_parent' => $pid,  
  74. 'post_status' => 'publish',  
  75. 'post_type' => 'page',  
  76. 'comment_status' => 'closed',  
  77. 'ping_status' => 'closed',  
  78. 'post_content' => "[saferpay-checkout]",  
  79. ); 
  80.  
  81. $id = wp_insert_post( $page ); 
  82. update_option( 'saferpay_page_id', $id); 
  83.  
  84.  
  85.  
  86. /** 
  87. * Get the currently activated payment methods 
  88. * @access public 
  89. * @return void 
  90. */  
  91. function get_payment_methods() 
  92. $result = ""; 
  93. $delimiter = ""; 
  94.  
  95. if($this->get_option( 'payment_method_mastercard' ) == 'yes') 
  96. $result .= $delimiter . "1" ; 
  97. $delimiter = ", "; 
  98.  
  99. if($this->get_option( 'payment_method_visa' ) == 'yes') 
  100. $result .= $delimiter . "2" ; 
  101. $delimiter = ", "; 
  102.  
  103.  
  104. if($this->get_option( 'payment_method_american_express' ) == 'yes') 
  105. $result .= $delimiter . "3" ; 
  106. $delimiter = ", "; 
  107.  
  108. if($this->get_option( 'payment_method_sofort_ueberweisung' ) == 'yes') 
  109. $result .= $delimiter . "15" ; 
  110. $delimiter = ", "; 
  111.  
  112.  
  113. return $result; 
  114.  
  115. /** 
  116. * Set the admin options panel 
  117. * @access public 
  118. * @return void 
  119. */ 
  120. public function admin_options() { 
  121.  
  122. echo '<h3>' . $this->method_title . '</h3>'; 
  123. echo '<p>' . $this->method_description . '</p>'; 
  124.  
  125. // Display additional information here... 
  126.  
  127. echo '<table class="form-table">'; 
  128. $this->generate_settings_html(); 
  129. echo '</table>'; 
  130.  
  131.  
  132. /** 
  133. * Initialise Gateway Settings Form Fields 
  134. * @access public 
  135. * @return void 
  136. */ 
  137. function init_form_fields() { 
  138.  
  139. $this->form_fields = array( 
  140. 'enabled' => array( 
  141. 'title' => __( 'Enable/Disable', 'fnwc-woocommerce' ),  
  142. 'type' => 'checkbox',  
  143. 'label' => __( 'Enable Saferpay Payment', 'fnwc-woocommerce' ),  
  144. 'default' => 'yes' 
  145. ),  
  146. 'accountid' => array( 
  147. 'title' => __( 'Saferpay Account ID', 'fnwc-woocommerce' ),  
  148. 'type' => 'text',  
  149. 'description' => __( 'Your Saferpay account ID. The ID of the Saferpay Test Account is 99867-94913159.', 'fnwc-woocommerce' ),  
  150. 'default' => __( '99867-94913159', 'fnwc-woocommerce' ),  
  151. 'desc_tip' => true,  
  152. ),  
  153. 'title' => array( 
  154. 'title' => __( 'Title', 'fnwc-woocommerce' ),  
  155. 'type' => 'text',  
  156. 'description' => __( 'This controls the title which the user sees during checkout.', 'fnwc-woocommerce' ),  
  157. 'default' => __( 'Saferpay Payment', 'fnwc-woocommerce' ),  
  158. 'desc_tip' => true,  
  159. ),  
  160. 'description' => array( 
  161. 'title' => __( 'Customer Message', 'fnwc-woocommerce' ),  
  162. 'type' => 'textarea',  
  163. 'default' => '' 
  164. ),  
  165. 'order_id_prefix' => array( 
  166. 'title' => __( 'Invoice prefix', 'fnwc-woocommerce' ),  
  167. 'type' => 'text',  
  168. 'description' => __( 'Please enter a prefix for your invoice numbers. If you use your Saferpay account for multiple stores ensure this prefix is unqiue to identify the order correctly.', 'fnwc-woocommerce' ),  
  169. 'default' => __( 'WC-', 'fnwc-woocommerce' ),  
  170. 'desc_tip' => true,  
  171. ),  
  172. 'order_description' => array( 
  173. 'title' => __( 'Invoice description', 'fnwc-woocommerce' ),  
  174. 'type' => 'text',  
  175. 'description' => __( 'This is the description which the user sees during the Saferpay payment process. Use the placeholder #_ORDERID to get the Id of the current order.', 'fnwc-woocommerce' ),  
  176. 'default' => __( 'Your order # #_ORDERID', 'fnwc-woocommerce' ),  
  177. 'desc_tip' => true,  
  178. ),  
  179.  
  180. /** Payment methods */ 
  181. 'payment_methods' => array( 
  182. 'title' => __( 'Payment methods', 'fnwc-woocommerce' ),  
  183. 'type' => 'title',  
  184. 'description' => '',  
  185. ),  
  186. 'payment_method_mastercard' => array( 
  187. 'title' => __( 'Enable/Disable', 'fnwc-woocommerce' ),  
  188. 'type' => 'checkbox',  
  189. 'label' => __('MasterCard', 'fnwc-woocommerce' ),  
  190. 'default' => 'yes' 
  191. ),  
  192. 'payment_method_visa' => array( 
  193. 'title' => __( 'Enable/Disable', 'fnwc-woocommerce' ),  
  194. 'type' => 'checkbox',  
  195. 'label' => __('Visa', 'fnwc-woocommerce' ),  
  196. 'default' => 'yes' 
  197. ),  
  198. 'payment_method_american_express' => array( 
  199. 'title' => __( 'Enable/Disable', 'fnwc-woocommerce' ),  
  200. 'type' => 'checkbox',  
  201. 'label' => __('American Express', 'fnwc-woocommerce' ),  
  202. 'default' => 'yes' 
  203. ),  
  204. 'payment_method_sofort_ueberweisung' => array( 
  205. 'title' => __( 'Enable/Disable', 'fnwc-woocommerce' ),  
  206. 'type' => 'checkbox',  
  207. 'label' => __('Direct payment', 'fnwc-woocommerce' ),  
  208. 'default' => 'no' 
  209. ),  
  210. ); 
  211.  
  212.  
  213. /** 
  214. * Process the payment on checkout 
  215. * @access public 
  216. * @return void 
  217. */  
  218. function process_payment( $order_id ) { 
  219.  
  220. global $woocommerce; 
  221.  
  222. // Get the order  
  223. $order = new WC_Order($order_id); 
  224. $status = 'success'; 
  225.  
  226. /************************************************************ 
  227. * It seems that we don't have to take care of clearing the cart 
  228. * or update the state of the order. 
  229. * see: WC_Gateway_Paypal in the WooCommerce /classes/gateways/paypal 
  230. * folder. The work is done in the return functionality 
  231. */ 
  232.  
  233.  
  234. /************************************************************ 
  235. * What are we doing here?  
  236. * - Build the url to get the payment URL from Saferpay 
  237. * - We check the result for validity 
  238. * - and redirect the user to the payment URL 
  239. */ 
  240.  
  241. // Get the URL of the checkout page (in case the user wants to return) 
  242. $checkouturl = $woocommerce->cart->get_checkout_url(); 
  243. $faillink = home_url('/wc-api/fnwc_saferpay_gateway'); 
  244.  
  245. // Create the so called Init Pay URL for Saferpay 
  246. $createpayiniturl = fn_get_createpayiniturl($this, $order, $checkouturl, $faillink); 
  247. $payurl = ""; 
  248.  
  249. try 
  250. // The Init Pay URL returns the Pay URL 
  251. $payurl = fn_get_urlcontent($createpayiniturl); 
  252. catch(Exception $e) 
  253. $msg = __('Could not connect to the payment provider', 'fnwc-woocommerce' ); 
  254.  
  255. // This is serious, we inform the administrator 
  256. $this->alert_admin($msg . "\r\nOrder ID: ". $order->id); 
  257.  
  258. // Add an error and set the status 
  259. $woocommerce->add_error($msg); 
  260. $status = 'failed'; 
  261.  
  262. // Simple verification of the Pay URL 
  263. if (stripos($payurl, "ERROR") !== false) { 
  264.  
  265. // This is serious, we inform the administrator 
  266. $this->alert_admin($payurl . "\r\nOrder ID: ". $order->id); 
  267.  
  268. // Add an error and set the status 
  269. $woocommerce->add_error($payurl); 
  270. $status = 'failed'; 
  271.  
  272. // Return redirect 
  273. return array( 
  274. 'result' => $status,  
  275. 'redirect' => '' 
  276. ); 
  277.  
  278.  
  279.  
  280.  
  281. // Return redirect 
  282. return array( 
  283. 'result' => $status,  
  284. 'redirect' => $payurl 
  285. ); 
  286.  
  287.  
  288.  
  289. /** 
  290. * Gets the notification from Saferpay and processes the result 
  291. * @access public 
  292. * @return void 
  293. */  
  294. function saferpay_notify() 
  295. // If  
  296. if (!empty($_GET)) 
  297. $url = fn_pay_confirm_complete($this); 
  298. wp_redirect($url);  
  299. exit; 
  300.  
  301. $response = fn_pay_validate_and_complete($this, $_POST); 
  302.  
  303. if($response != "OK") 
  304. $gateway->alert_admin($response);  
  305.  
  306.  
  307. /** 
  308. * Alert the administrator 
  309. * @access public 
  310. * @return void 
  311. */  
  312. function alert_admin($message) 
  313. $to = get_option('admin_email'); 
  314.  
  315. $subject = 'Alert: WooCommerce Payment Gateway for Saferpay'; 
  316. wp_mail($to, $subject, $message ); 
  317.  
  318.