-
Notifications
You must be signed in to change notification settings - Fork 4
/
camptix-payment-banktransfer.php
399 lines (350 loc) · 15.2 KB
/
camptix-payment-banktransfer.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php // encoding: utf-8
/*
Plugin Name: CampTix Payment Method: Bank transfer
Description: Extends CampTix with support for Bank transfer
Version: 1.0.0
Author: Raphael Michel
*/
require_once dirname( __FILE__ ) . '/../camptix/camptix.php';
if(!class_exists('CampTix_Payment_Method'))
die('Plugin "CampTix Payment Method: Bank transfer" needs CampTix to be installed.');
class CampTix_Payment_Method_Banktransfer extends CampTix_Payment_Method {
public $id = 'banktransfer';
public $name = 'Bank Transfer';
public $description = 'Bank transfer checkout.';
public $supported_currencies = array( 'AUD', 'CAD', 'EUR', 'GBP', 'JPY', 'USD', 'NZD', 'CHF', 'HKD', 'SGD', 'SEK',
'DKK', 'PLN', 'NOK', 'HUF', 'CZK', 'ILS', 'MXN', 'BRL', 'MYR', 'PHP', 'TWD', 'THB', 'TRY');
protected $options = array();
public function __construct () {
$this->name = __('Bank Transfer', 'camptixpaymentbanktransfer');
$this->description = __('Bank transfer checkout. Please fill in your bank details below, the user will be prompted to transfer money according to this details. <strong>Make sure to include the shortcode [bankdetails] in your e-mail templates!</strong>', 'camptixpaymentbanktransfer');
parent::__construct();
}
public function payment_checkout( $payment_token ) {
global $camptix;
$ident = uniqid(mt_rand(100,999));
$order = $this->get_order( $payment_token );
$attendees = get_posts( array(
'posts_per_page' => 100,
'post_type' => 'tix_attendee',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => 'tix_payment_token',
'compare' => '=',
'value' => $payment_token,
'type' => 'CHAR',
),
),
) );
foreach($attendees as $a) {
$ID = $a->ID;
update_post_meta( $ID, 'tix_banktransfer_token', $ident );
}
return $this->payment_result( $payment_token, CampTix_Plugin::PAYMENT_STATUS_PENDING, $payment_data );
}
// Fired when CampTix initialized, you don't have to call this
function camptix_init() {
$this->options = array_merge( array(
'bankdetails' => '',
'term_duration' => '14',
'term_lasttime' => '2099-12-31'
), $this->get_payment_options() );
add_action( 'camptix_notices', array($this, 'payment_pending_information'), 11 );
add_action( 'camptix_init_email_templates_shortcodes', array( $this, 'init_email_templates_shortcodes' ), 9 );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
function admin_enqueue_scripts() {
global $wp_query;
if ( ! $wp_query->query_vars ) { // only on singular admin pages
if ( 'tix_ticket' == get_post_type() || 'tix_coupon' == get_post_type() ) {
}
}
// Let's see whether to include admin.css and admin.js
if ( is_admin() ) {
$post_types = array( 'tix_ticket', 'tix_coupon', 'tix_email', 'tix_attendee' );
$pages = array( 'camptix_options', 'camptix_tools' );
if (
( in_array( get_post_type(), $post_types ) ) ||
( isset( $_REQUEST['post_type'] ) && in_array( $_REQUEST['post_type'], $post_types ) ) ||
( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $pages ) )
) {
wp_enqueue_style( 'camptix-payment-banktransfer-admin', plugins_url( '/admin.css', __FILE__ ), array(), $this->css_version );
}
}
$screen = get_current_screen();
if ( 'tix_ticket_page_camptix_options' == $screen->id ) {
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_style( 'jquery-ui', plugins_url( '/external/jquery-ui.css', __FILE__ ), array(), $this->version );
}
}
function init_email_templates_shortcodes () {
add_shortcode( 'bankdetails', array( $this, 'shortcode_bankdetails' ) );
}
function shortcode_bankdetails( $attr ) {
global $camptix;
$price = $camptix->append_currency( get_post_meta( $camptix->tmp('attendee_id'), 'tix_order_total', true ), false );
$reference = get_post_meta( $camptix->tmp('attendee_id'), 'tix_banktransfer_token', true );
$attendee = get_post($camptix->tmp('attendee_id'));
$bankdetails = __( "Please transfer your money to the following bank account:", 'camptixpaymentbanktransfer' )."\n";
$bankdetails .= $this->options['bankdetails'];
$bankdetails .= sprintf( "\n" . __( "Reference: %s", 'camptixpaymentbanktransfer' ), $reference);
$bankdetails .= sprintf( "\n" . __( "Amount: %s", 'camptixpaymentbanktransfer' ), $price );
if($this->options['term_duration'])
$bankdetails .= sprintf( "\n" . __( "Please pay before: %s", 'camptixpaymentbanktransfer' ), $this->printable_payment_date($attendee->post_date) );
return $bankdetails;
}
function printable_payment_date($purchasedate) {
$purchasetime = strtotime($purchasedate);
$lasttime = strtotime($this->options['term_lasttime']);
$endofterm = $purchasetime + ($this->options['term_duration'] * 3600 * 24);
if($endofterm > $lasttime) $endofterm = $lasttime;
return date_i18n(get_option('date_format'), $endofterm);
}
function payment_pending_information() {
global $camptix;
if ( 'edit_attendee' == get_query_var( 'tix_action' ) ) {
$attendee_id = intval( $_REQUEST['tix_attendee_id'] );
$attendee = get_post( $attendee_id );
} else if ( 'access_tickets' == get_query_var( 'tix_action' ) ) {
$access_token = $_REQUEST['tix_access_token'];
$is_refundable = false;
// Let's get one attendee
$attendees = get_posts( array(
'posts_per_page' => 1,
'post_type' => 'tix_attendee',
'post_status' => array( 'publish', 'pending' ),
'meta_query' => array(
array(
'key' => 'tix_access_token',
'value' => $access_token,
'compare' => '=',
'type' => 'CHAR',
),
),
'cache_results' => false,
) );
$attendee = $attendees[0];
$attendee_id = $attendee->ID;
} else {
return;
}
if ( $attendee->post_status != 'pending' )
return;
if ( get_post_meta( $attendee_id, 'tix_payment_method', true ) == $this->id ){
$price = $camptix->append_currency( get_post_meta( $attendee_id, 'tix_order_total', true ) );
$bankdetails = nl2br($this->options['bankdetails']);
$bankdetails .= sprintf( "<br />" . __( "Reference: <strong>%s</strong>", 'camptixpaymentbanktransfer' ), get_post_meta( $attendee_id, 'tix_banktransfer_token', true ));
$bankdetails .= sprintf( "<br />" . __( "Amount: %s", 'camptixpaymentbanktransfer' ), $price );
if($this->options['term_duration'])
$bankdetails .= sprintf( "<br />" . __( "Please pay before: %s", 'camptixpaymentbanktransfer' ), $this->printable_payment_date($attendee->post_date) );
echo '<p class="tix-notice">';
echo __( "Please transfer your money to the following bank account:", 'camptixpaymentbanktransfer' ) . "<br />" . $bankdetails;
echo '</p>';
}
}
// This is also called by CampTix when rendering your payment method sections
function payment_settings_fields() {
global $camptix;
// You can use the helper function along with the helper callback function
$this->add_settings_field_helper( 'bankdetails', __('Bank details', 'camptixpaymentbanktransfer'), array( $camptix, 'field_textarea' ) );
$this->add_settings_field_helper( 'term_duration', __('Payment term duration (days)', 'camptixpaymentbanktransfer'), array( $camptix, 'field_text' ) );
$this->add_settings_field_helper( 'term_lasttime', __('Last date for payment', 'camptixpaymentbanktransfer'), array( $camptix, 'field_text' ) );
}
// Called by CampTix when your payment settings are being saved
function validate_options( $input ) {
$output = $this->options;
if ( isset( $input['bankdetails'] ) )
$output['bankdetails'] = $input['bankdetails'];
if ( isset( $input['term_duration'] ) )
$output['term_duration'] = $input['term_duration'];
if ( isset( $input['term_lasttime'] ) )
$output['term_lasttime'] = $input['term_lasttime'];
return $output;
}
function admin_menu() {
global $camptix;
add_submenu_page( 'edit.php?post_type=tix_ticket', __( 'Bank Transfer', 'camptixpaymentbanktransfer' ), __( 'Bank Transfer', 'camptixpaymentbanktransfer' ), $camptix->caps['manage_attendees'], 'camptix_banktransfer', array( $this, 'menu_tools' ) );
}
function markpayed($payment_token) {
global $camptix;
$attendees = get_posts( array(
'posts_per_page' => 100,
'post_type' => 'tix_attendee',
'post_status' => array( 'pending' ),
'meta_query' => array(
array(
'key' => 'tix_payment_token',
'value' => esc_sql($payment_token),
'compare' => '=',
'type' => 'CHAR',
),
),
'cache_results' => false,
) );
foreach ($attendees as $attendee) {
$attendee->post_status = 'publish';
wp_update_post( $attendee );
$camptix->log( sprintf( 'Attendee status has been changed to %s', $attendee->post_status ), $attendee->ID );
}
$camptix->email_tickets($payment_token, 'pending', 'publish');
}
function menu_tools() {
global $camptix;
if(isset($_POST['reference'])) {
if ( !wp_verify_nonce($_POST['single_reference_nonce'],'camptix_banktransfer') ) {
print 'Sorry, your nonce did not verify.';
exit;
}
else {
$attendees = get_posts( array(
'posts_per_page' => 100,
'post_type' => 'tix_attendee',
'post_status' => array( 'pending' ),
'meta_query' => array(
array(
'key' => 'tix_banktransfer_token',
'value' => esc_sql($_POST['reference']),
'compare' => '=',
'type' => 'CHAR',
),
),
'cache_results' => false,
) );
if(count($attendees) > 0){
$_POST['amount'] = str_replace(',', '.', $_POST['amount']);
$pt = get_post_meta( $attendees[0]->ID, 'tix_payment_token', true );
$pf = floatval (get_post_meta( $attendees[0]->ID, 'tix_order_total', true ));
$price = $camptix->append_currency( $pf, false );
if ( $pf !== floatval($_POST['amount']) ) {
add_settings_error(
'',
'tixBanktransferFailed',
sprintf( __( 'Value %s does not match order total of %s!', 'camptixpaymentbanktransfer' ), floatval($_POST['amount']), $price),
'error'
);
} else {
$this->markpayed($pt);
add_settings_error(
'',
'tixBanktransferSuccess',
sprintf( __( '<strong>%d</strong> Tickets for an order total of <strong>%s</strong> marked as payed.', 'camptixpaymentbanktransfer' ), count($attendees), $price),
'updated'
);
}
} else {
add_settings_error(
'',
'tixBanktransferNotFound',
__( 'Payment reference not found or already marked as payed.', 'camptixpaymentbanktransfer' ),
'error'
);
}
}
}
?>
<div class="wrap">
<?php screen_icon( 'tools' ); ?>
<h2><?php _e( 'Bank Transfer payment status import', 'camptixpaymentbanktransfer' ); ?></h2>
<?php settings_errors(); ?>
<p><?php _e( 'This page is a tool to mark payments which you recieved via wire transfer as payed. You can either enter a single payment reference or import a whole CSV from your bank. Actions on this page can take long because many emails might be to send...', 'camptixpaymentbanktransfer' ); ?></p>
<h3><?php _e( 'Single reference', 'camptixpaymentbanktransfer' ); ?></h3>
<form action="" method="post">
<input type="text" name="reference" placeholder="<?php _e( 'Payment reference key', 'camptixpaymentbanktransfer' ) ?>" value="" />
<input type="text" name="amount" placeholder="<?php _e( 'Amount (e.g. 100.5) in your currency', 'camptixpaymentbanktransfer' ) ?>" value="" />
<input type="submit" value="<?php _e( 'Mark as payed', 'camptixpaymentbanktransfer' ); ?>" />
<?php wp_nonce_field('camptix_banktransfer','single_reference_nonce'); ?>
</form>
<h3><?php _e( 'CSV import', 'camptixpaymentbanktransfer' ); ?></h3>
<p><?php _e( 'Attention: This is very beta. If you\'re not the one I made this for, better don\'t use in a real environment. Also, this takes LONG! Make sure your server allows PHP scripts to run long. <strong>AND DO BACKUPS!</strong>', 'camptixpaymentbanktransfer' ); ?></p>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="csv" value="" />
<input type="submit" value="<?php _e( 'Mark as payed', 'camptixpaymentbanktransfer' ); ?>" />
<?php wp_nonce_field('camptix_banktransfer','csv_reference_nonce'); ?>
</form>
<?php
if(isset($_FILES['csv'])) {
?>
<h3><?php _e( 'CSV import results', 'camptixpaymentbanktransfer' ); ?></h3>
<?php
if ( !wp_verify_nonce($_POST['csv_reference_nonce'],'camptix_banktransfer') ) {
print 'Sorry, your nonce did not verify.';
exit;
} else {
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");
echo '<table class="tix-bank-import">';
while ( $row = fgets ( $handle ) ) {
$row = utf8_encode(trim($row));
if ( $row == "" ) continue;
$money = null;
$res_class = '';
$srow = '<td colspan="2"></td>';
if ( preg_match ( '/[^a-zA-Z0-9]([abcdefABCDEF0-9]{14,17})[^a-zA-Z0-9]/', $row, $sub ) ) {
$html_reference = $reference = $sub[1];
if ( preg_match ( '/["\';,]([0-9]+[.,]?[0-9]*)["\';,]/', $row, $subm ) ) {
$money = floatval( str_replace( ",", ".", $subm[1] ) );
$attendees = get_posts( array(
'posts_per_page' => 1,
'post_type' => 'tix_attendee',
'post_status' => array( 'pending', 'publish' ),
'meta_query' => array(
array(
'key' => 'tix_banktransfer_token',
'value' => esc_sql($reference),
'compare' => '=',
'type' => 'CHAR',
),
),
'cache_results' => false,
) );
if ( count( $attendees ) > 0 ) {
if( $attendees[0]->post_status == "publish" ) {
$res_text = __( 'Already payed', 'camptixpaymentbanktransfer' );
$res_class = "payed";
} else {
$pf = floatval ( get_post_meta( $attendees[0]->ID, 'tix_order_total', true ) );
$price = $camptix->append_currency( $pf, false );
if($pf == $money) {
$pt = get_post_meta( $attendees[0]->ID, 'tix_payment_token', true );
$this->markpayed($pt);
$res_text = sprintf ( __( 'Marked as payed.', 'camptixpaymentbanktransfer' ), $price );
$res_class = "success";
} else {
$res_text = sprintf ( __( 'Wrong amount of money. Order total is %s!', 'camptixpaymentbanktransfer' ), $price );
$res_class = "error";
}
}
} else {
$res_text = __( 'Invalid payment reference', 'camptixpaymentbanktransfer' );
$res_class = "error";
}
} else {
$res_text = __( 'No money amount found', 'camptixpaymentbanktransfer' );
$res_class = "error";
}
$srow = '<td>'.$html_reference.'</td>';
if($money)
$srow .= '<td>' . $camptix->append_currency( $money ) . '</td>';
} else {
$res_text = __( 'No payment reference found', 'camptixpaymentbanktransfer' );
}
echo '<tr class="raw">';
echo '<td colspan="2">'.$row.'</td>';
echo '<td rowspan="2" class="'.$res_class.'">'.$res_text.'</td>';
echo '</tr>';
echo '<tr>';
echo $srow;
echo '</tr>';
}
echo '</table>';
}
}
?>
</div>
<?php
}
}
load_plugin_textdomain( 'camptixpaymentbanktransfer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
camptix_register_addon( 'CampTix_Payment_Method_Banktransfer' );