-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpmpro-payfast.php
256 lines (205 loc) · 8 KB
/
pmpro-payfast.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/*
Plugin Name: Paid Memberships Pro - PayFast Gateway
Plugin URI: https://www.paidmembershipspro.com/add-ons/payfast-payment-gateway/
Description: Adds PayFast as a gateway option for Paid Memberships Pro.
Version: 1.6.1
Author: Paid Memberships Pro
Author URI: https://www.paidmembershipspro.com
Text Domain: pmpro-payfast
Domain Path: /languages
*/
define( 'PMPRO_PAYFAST_DIR', plugin_dir_path( __FILE__ ) );
// load payment gateway class after all plugins are loaded to make sure PMPro stuff is available
function pmpro_payfast_plugins_loaded() {
load_plugin_textdomain( 'pmpro-payfast', false, basename( __DIR__ ) . '/languages' );
// make sure PMPro is loaded
if ( ! defined( 'PMPRO_DIR' ) ) {
return;
}
require_once( PMPRO_PAYFAST_DIR . '/classes/class.pmprogateway_payfast.php' );
}
add_action( 'plugins_loaded', 'pmpro_payfast_plugins_loaded' );
// Register activation hook.
register_activation_hook( __FILE__, 'pmpro_payfast_admin_notice_activation_hook' );
/**
* Runs only when the plugin is activated.
*
* @since 0.1.0
*/
function pmpro_payfast_admin_notice_activation_hook() {
// Create transient data.
set_transient( 'pmpro-payfast-admin-notice', true, 5 );
}
/**
* Admin Notice on Activation.
*
* @since 0.1
*/
function pmpro_payfast_admin_notice() {
// Check transient, if available display notice.
if ( get_transient( 'pmpro-payfast-admin-notice' ) ) { ?>
<div class="updated notice is-dismissible">
<p><?php printf( __( 'Thank you for activating. <a href="%s">Visit the payment settings page</a> to configure the Payfast Gateway.', 'pmpro-payfast' ), esc_url( get_admin_url( null, 'admin.php?page=pmpro-paymentsettings' ) ) ); ?></p>
</div>
<?php
// Delete transient, only display this notice once.
delete_transient( 'pmpro-payfast-admin-notice' );
}
}
add_action( 'admin_notices', 'pmpro_payfast_admin_notice' );
/**
* Show an admin warning notice if there is a level setup that is incorrect.
* @since 0.9
*/
function pmpro_payfast_check_level_compat(){
// Only show the notice on either the levels page or payment settings page.
if ( ! isset( $_REQUEST['page'] ) || $_REQUEST['page'] != 'pmpro-membershiplevels' ) {
return;
}
$level = isset( $_REQUEST['edit'] ) ? intval( $_REQUEST['edit'] ) : '';
// Don't check if level is not set.
if ( empty( $level ) ) {
return;
}
$compatible = pmpro_payfast_check_billing_compat( $level );
if ( ! $compatible ){
?>
<div class="notice notice-error fade">
<p>
<?php esc_html_e( "PayFast currently doesn't support custom trials. Please can you update your membership levels that may have these set.", 'pmpro-payfast' );?>
</p>
</div>
<?php
}
}
add_action( 'admin_notices', 'pmpro_payfast_check_level_compat' );
/**
* Fix PMPro Payfast showing SSL error in admin menus
* when set up correctly.
*
* @since 0.9
*/
function pmpro_payfast_pmpro_is_ready( $pmpro_is_ready ) {
global $pmpro_gateway_ready, $pmpro_pages_ready;
if ( empty($pmpro_gateway_ready) && 'payfast' === get_option( 'pmpro_gateway' ) ) {
if( get_option( 'pmpro_payfast_merchant_id' ) && get_option( 'pmpro_payfast_merchant_key' ) && get_option( 'pmpro_payfast_passphrase' ) ) {
$pmpro_gateway_ready = true;
}
}
return ( $pmpro_gateway_ready && $pmpro_pages_ready );
}
add_filter( 'pmpro_is_ready', 'pmpro_payfast_pmpro_is_ready' );
/**
* Check if there are billing compatibility issues for levels and PayFast.
* @since 0.9
*/
function pmpro_payfast_check_billing_compat( $level = NULL ){
if( !function_exists( 'pmpro_init' ) ){
return;
}
$gateway = get_option("pmpro_gateway");
if( $gateway == "payfast" ){
global $wpdb;
//check ALL the levels
if( empty( $level ) ){
$sqlQuery = "SELECT * FROM $wpdb->pmpro_membership_levels ORDER BY id ASC";
$levels = $wpdb->get_results($sqlQuery, OBJECT);
if( !empty( $levels ) ){
foreach( $levels as $level ){
if( !pmpro_payfast_check_billing_compat( $level->id ) ){
return false;
}
}
}
} else {
if( is_numeric( $level ) && $level > 0 ){
$level = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->pmpro_membership_levels WHERE id = %d LIMIT 1" , $level ) );
if( pmpro_isLevelTrial( $level ) ){
return false;
}
}
}
}
return true;
}
/**
* Show a warning if custom trial is selected during level setup.
* @since 0.9
*/
function pmpro_payfast_custom_trial_js_check() {
$gateway = get_option( 'pmpro_gateway' );
if ( $gateway !== 'payfast' ) {
return;
}
$custom_trial_warning = __( sprintf( 'PayFast does not support custom trials. Please use the %s instead.', "<a href='https://www.paidmembershipspro.com/add-ons/subscription-delays' target='_blank'>Subscription Delay Add On</a>" ), 'pmpro-payfast' ); ?>
<script>
jQuery(document).ready(function(){
var message = "<?php echo $custom_trial_warning; ?>";
jQuery( '<tr id="payfast-trial-warning" style="display:none"><th></th><td><em><strong>' + message + '</strong></em></td></tr>' ).insertAfter( '.trial_info' );
// Show for existing levels.
if ( jQuery('#custom-trial').is(':checked') ) {
jQuery( '#payfast-trial-warning' ).show();
}
// Toggle if checked or not
pmpro_payfast_trial_checked();
function pmpro_payfast_trial_checked() {
jQuery('#custom_trial').change(function(){
if ( jQuery(this).prop('checked') ) {
jQuery( '#payfast-trial-warning' ).show();
} else {
jQuery( '#payfast-trial-warning' ).hide();
}
});
}
});
</script>
<?php
}
add_action( 'pmpro_membership_level_after_other_settings', 'pmpro_payfast_custom_trial_js_check' );
/**
* Function to add links to the plugin action links
*
* @param array $links Array of links to be shown in plugin action links.
*/
function pmpro_payfast_plugin_action_links( $links ) {
$new_links = array();
if ( current_user_can( 'manage_options' ) ) {
$new_links[] = '<a href="' . get_admin_url( null, 'admin.php?page=pmpro-paymentsettings' ) . '">' . __( 'Configure Payfast', 'pmpro-payfast' ) . '</a>';
}
return array_merge( $new_links, $links );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'pmpro_payfast_plugin_action_links' );
/**
* Function to add links to the plugin row meta
*
* @param array $links Array of links to be shown in plugin meta.
* @param string $file Filename of the plugin meta is being shown for.
*/
function pmpro_payfast_plugin_row_meta( $links, $file ) {
if ( strpos( $file, 'pmpro-payfast.php' ) !== false ) {
$new_links = array(
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/add-ons/payfast-payment-gateway/' ) . '" title="' . esc_attr( __( 'View Documentation', 'pmpro-payfast' ) ) . '">' . __( 'Docs', 'pmpro-payfast' ) . '</a>',
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/support/' ) . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro-payfast' ) ) . '">' . __( 'Support', 'pmpro-payfast' ) . '</a>',
);
$links = array_merge( $links, $new_links );
}
return $links;
}
add_filter( 'plugin_row_meta', 'pmpro_payfast_plugin_row_meta', 10, 2 );
function pmpro_payfast_discount_code_result( $discount_code, $discount_code_id, $level_id, $code_level ){
global $wpdb;
//okay, send back new price info
$sqlQuery = "SELECT l.id, cl.*, l.name, l.description, l.allow_signups FROM $wpdb->pmpro_discount_codes_levels cl LEFT JOIN $wpdb->pmpro_membership_levels l ON cl.level_id = l.id LEFT JOIN $wpdb->pmpro_discount_codes dc ON dc.id = cl.code_id WHERE dc.code = '" . $discount_code . "' AND cl.level_id = '" . $level_id . "' LIMIT 1";
$code_level = $wpdb->get_row($sqlQuery);
//if the discount code doesn't adjust the level, let's just get the straight level
if(empty($code_level)){
$code_level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_membership_levels WHERE id = '" . $level_id . "' LIMIT 1");
}
if( pmpro_isLevelFree( $code_level ) ){ //A valid discount code was returned
?>
jQuery('#pmpro_payfast_before_checkout').hide();
<?php
}
}
add_action( 'pmpro_applydiscountcode_return_js', 'pmpro_payfast_discount_code_result', 10, 4 );