/classes/gateways/class.pmprogateway_check.php

  1. <?php 
  2. //include pmprogateway 
  3. require_once(dirname(__FILE__) . "/class.pmprogateway.php"); 
  4.  
  5. //load classes init method 
  6. add_action('init', array('PMProGateway_check', 'init')); 
  7.  
  8. class PMProGateway_check extends PMProGateway 
  9. function __construct($gateway = NULL) 
  10. $this->gateway = $gateway; 
  11. return $this->gateway; 
  12. }  
  13.  
  14. /** 
  15. * Run on WP init 
  16. *  
  17. * @since 1.8 
  18. */ 
  19. static function init() 
  20. {  
  21. //make sure Pay by Check is a gateway option 
  22. add_filter('pmpro_gateways', array('PMProGateway_check', 'pmpro_gateways')); 
  23.  
  24. //add fields to payment settings 
  25. add_filter('pmpro_payment_options', array('PMProGateway_check', 'pmpro_payment_options')); 
  26. add_filter('pmpro_payment_option_fields', array('PMProGateway_check', 'pmpro_payment_option_fields'), 10, 2); 
  27. add_filter('pmpro_checkout_after_payment_information_fields', array('PMProGateway_check', 'pmpro_checkout_after_payment_information_fields')); 
  28.  
  29. //code to add at checkout 
  30. $gateway = pmpro_getGateway(); 
  31. if($gateway == "check") 
  32. add_filter('pmpro_include_billing_address_fields', '__return_false'); 
  33. add_filter('pmpro_include_payment_information_fields', '__return_false'); 
  34. add_filter('pmpro_required_billing_fields', array('PMProGateway_check', 'pmpro_required_billing_fields')); 
  35.  
  36. /** 
  37. * Make sure Check is in the gateways list 
  38. *  
  39. * @since 1.8 
  40. */ 
  41. static function pmpro_gateways($gateways) 
  42. if(empty($gateways['check'])) 
  43. $gateways['check'] = __('Pay by Check', 'paid-memberships-pro' ); 
  44.  
  45. return $gateways; 
  46.  
  47. /** 
  48. * Get a list of payment options that the Check gateway needs/supports. 
  49. *  
  50. * @since 1.8 
  51. */ 
  52. static function getGatewayOptions() 
  53. {  
  54. $options = array( 
  55. 'sslseal',  
  56. 'nuclear_HTTPS',  
  57. 'gateway_environment',  
  58. 'instructions',  
  59. 'currency',  
  60. 'use_ssl',  
  61. 'tax_state',  
  62. 'tax_rate' 
  63. ); 
  64.  
  65. return $options; 
  66.  
  67. /** 
  68. * Set payment options for payment settings page. 
  69. *  
  70. * @since 1.8 
  71. */ 
  72. static function pmpro_payment_options($options) 
  73. {  
  74. //get stripe options 
  75. $check_options = PMProGateway_check::getGatewayOptions(); 
  76.  
  77. //merge with others. 
  78. $options = array_merge($check_options, $options); 
  79.  
  80. return $options; 
  81.  
  82. /** 
  83. * Display fields for Check options. 
  84. *  
  85. * @since 1.8 
  86. */ 
  87. static function pmpro_payment_option_fields($values, $gateway) 
  88. ?> 
  89. <tr class="pmpro_settings_divider gateway gateway_check" <?php if($gateway != "check") { ?>style="display: none;"<?php } ?>> 
  90. <td colspan="2"> 
  91. <?php _e('Pay by Check Settings', 'paid-memberships-pro' ); ?> 
  92. </td> 
  93. </tr> 
  94. <tr class="gateway gateway_check" <?php if($gateway != "check") { ?>style="display: none;"<?php } ?>> 
  95. <th scope="row" valign="top"> 
  96. <label for="instructions"><?php _e('Instructions', 'paid-memberships-pro' );?></label>  
  97. </th> 
  98. <td> 
  99. <textarea id="instructions" name="instructions" rows="3" cols="80"><?php echo esc_textarea($values['instructions'])?></textarea> 
  100. <p><small><?php _e('Who to write the check out to. Where to mail it. Shown on checkout, confirmation, and invoice pages.', 'paid-memberships-pro' );?></small></p> 
  101. </td> 
  102. </tr>  
  103. <?php 
  104.  
  105. /** 
  106. * Remove required billing fields 
  107. *  
  108. * @since 1.8 
  109. */ 
  110. static function pmpro_required_billing_fields($fields) 
  111. unset($fields['bfirstname']); 
  112. unset($fields['blastname']); 
  113. unset($fields['baddress1']); 
  114. unset($fields['bcity']); 
  115. unset($fields['bstate']); 
  116. unset($fields['bzipcode']); 
  117. unset($fields['bphone']); 
  118. unset($fields['bemail']); 
  119. unset($fields['bcountry']); 
  120. unset($fields['CardType']); 
  121. unset($fields['AccountNumber']); 
  122. unset($fields['ExpirationMonth']); 
  123. unset($fields['ExpirationYear']); 
  124. unset($fields['CVV']); 
  125.  
  126. return $fields; 
  127.  
  128. /** 
  129. * Show instructions on checkout page 
  130. * Moved here from pages/checkout.php 
  131. * @since 1.8.9.3 
  132. */ 
  133. static function pmpro_checkout_after_payment_information_fields() { 
  134. global $gateway; 
  135. global $pmpro_level; 
  136.  
  137. if($gateway == "check" && !pmpro_isLevelFree($pmpro_level)) { 
  138. $instructions = pmpro_getOption("instructions"); 
  139. echo '<div class="pmpro_check_instructions">' . wpautop($instructions) . '</div>'; 
  140.  
  141.  
  142. /** 
  143. * Process checkout. 
  144. * 
  145. */ 
  146. function process(&$order) 
  147. //clean up a couple values 
  148. $order->payment_type = "Check"; 
  149. $order->CardType = ""; 
  150. $order->cardtype = ""; 
  151.  
  152. //check for initial payment 
  153. if(floatval($order->InitialPayment) == 0) 
  154. //auth first, then process 
  155. if($this->authorize($order)) 
  156. {  
  157. $this->void($order);  
  158. if(!pmpro_isLevelTrial($order->membership_level)) 
  159. //subscription will start today with a 1 period trial 
  160. $order->ProfileStartDate = date_i18n("Y-m-d") . "T0:0:0"; 
  161. $order->TrialBillingPeriod = $order->BillingPeriod; 
  162. $order->TrialBillingFrequency = $order->BillingFrequency;  
  163. $order->TrialBillingCycles = 1; 
  164. $order->TrialAmount = 0; 
  165.  
  166. //add a billing cycle to make up for the trial, if applicable 
  167. if(!empty($order->TotalBillingCycles)) 
  168. $order->TotalBillingCycles++; 
  169. elseif($order->InitialPayment == 0 && $order->TrialAmount == 0) 
  170. //it has a trial, but the amount is the same as the initial payment, so we can squeeze it in there 
  171. $order->ProfileStartDate = date_i18n("Y-m-d") . "T0:0:0";  
  172. $order->TrialBillingCycles++; 
  173.  
  174. //add a billing cycle to make up for the trial, if applicable 
  175. if($order->TotalBillingCycles) 
  176. $order->TotalBillingCycles++; 
  177. else 
  178. //add a period to the start date to account for the initial payment 
  179. $order->ProfileStartDate = date_i18n("Y-m-d", strtotime("+ " . $order->BillingFrequency . " " . $order->BillingPeriod, current_time("timestamp"))) . "T0:0:0"; 
  180.  
  181. $order->ProfileStartDate = apply_filters("pmpro_profile_start_date", $order->ProfileStartDate, $order); 
  182. return $this->subscribe($order); 
  183. else 
  184. if(empty($order->error)) 
  185. $order->error = __("Unknown error: Authorization failed.", 'paid-memberships-pro' ); 
  186. return false; 
  187. else 
  188. //charge first payment 
  189. if($this->charge($order)) 
  190. {  
  191. //set up recurring billing  
  192. if(pmpro_isLevelRecurring($order->membership_level)) 
  193. {  
  194. if(!pmpro_isLevelTrial($order->membership_level)) 
  195. //subscription will start today with a 1 period trial 
  196. $order->ProfileStartDate = date_i18n("Y-m-d") . "T0:0:0"; 
  197. $order->TrialBillingPeriod = $order->BillingPeriod; 
  198. $order->TrialBillingFrequency = $order->BillingFrequency;  
  199. $order->TrialBillingCycles = 1; 
  200. $order->TrialAmount = 0; 
  201.  
  202. //add a billing cycle to make up for the trial, if applicable 
  203. if(!empty($order->TotalBillingCycles)) 
  204. $order->TotalBillingCycles++; 
  205. elseif($order->InitialPayment == 0 && $order->TrialAmount == 0) 
  206. //it has a trial, but the amount is the same as the initial payment, so we can squeeze it in there 
  207. $order->ProfileStartDate = date_i18n("Y-m-d") . "T0:0:0";  
  208. $order->TrialBillingCycles++; 
  209.  
  210. //add a billing cycle to make up for the trial, if applicable 
  211. if(!empty($order->TotalBillingCycles)) 
  212. $order->TotalBillingCycles++; 
  213. else 
  214. //add a period to the start date to account for the initial payment 
  215. $order->ProfileStartDate = date_i18n("Y-m-d", strtotime("+ " . $this->BillingFrequency . " " . $this->BillingPeriod, current_time("timestamp"))) . "T0:0:0"; 
  216.  
  217. $order->ProfileStartDate = apply_filters("pmpro_profile_start_date", $order->ProfileStartDate, $order); 
  218. if($this->subscribe($order)) 
  219. $order->status = apply_filters("pmpro_check_status_after_checkout", "success"); //saved on checkout page  
  220. return true; 
  221. else 
  222. if($this->void($order)) 
  223. if(!$order->error) 
  224. $order->error = __("Unknown error: Payment failed.", 'paid-memberships-pro' ); 
  225. else 
  226. if(!$order->error) 
  227. $order->error = __("Unknown error: Payment failed.", 'paid-memberships-pro' ); 
  228.  
  229. $order->error .= " " . __("A partial payment was made that we could not void. Please contact the site owner immediately to correct this.", 'paid-memberships-pro' ); 
  230.  
  231. return false;  
  232. else 
  233. //only a one time charge 
  234. $order->status = apply_filters("pmpro_check_status_after_checkout", "success"); //saved on checkout page  
  235. return true; 
  236. else 
  237. if(empty($order->error)) 
  238. $order->error = __("Unknown error: Payment failed.", 'paid-memberships-pro' ); 
  239.  
  240. return false; 
  241. }  
  242. }  
  243.  
  244. function authorize(&$order) 
  245. //create a code for the order 
  246. if(empty($order->code)) 
  247. $order->code = $order->getRandomCode(); 
  248.  
  249. //simulate a successful authorization 
  250. $order->payment_transaction_id = "CHECK" . $order->code; 
  251. $order->updateStatus("authorized");  
  252. return true;  
  253.  
  254. function void(&$order) 
  255. //need a transaction id 
  256. if(empty($order->payment_transaction_id)) 
  257. return false; 
  258.  
  259. //simulate a successful void 
  260. $order->payment_transaction_id = "CHECK" . $order->code; 
  261. $order->updateStatus("voided");  
  262. return true; 
  263. }  
  264.  
  265. function charge(&$order) 
  266. //create a code for the order 
  267. if(empty($order->code)) 
  268. $order->code = $order->getRandomCode(); 
  269.  
  270. //simulate a successful charge 
  271. $order->payment_transaction_id = "CHECK" . $order->code; 
  272. $order->updateStatus("success");  
  273. return true;  
  274.  
  275. function subscribe(&$order) 
  276. //create a code for the order 
  277. if(empty($order->code)) 
  278. $order->code = $order->getRandomCode(); 
  279.  
  280. //filter order before subscription. use with care. 
  281. $order = apply_filters("pmpro_subscribe_order", $order, $this); 
  282.  
  283. //simulate a successful subscription processing 
  284. $order->status = "success";  
  285. $order->subscription_transaction_id = "CHECK" . $order->code;  
  286. return true; 
  287. }  
  288.  
  289. function update(&$order) 
  290. //simulate a successful billing update 
  291. return true; 
  292.  
  293. function cancel(&$order) 
  294. //require a subscription id 
  295. if(empty($order->subscription_transaction_id)) 
  296. return false; 
  297.  
  298. //simulate a successful cancel  
  299. $order->updateStatus("cancelled");  
  300. return true; 
  301. }  
.