forked from open-pay/openpay-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-openpay.php
executable file
·1126 lines (954 loc) · 36 KB
/
woocommerce-openpay.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: OpenPay WooCommerce Plugin
Plugin URI: http://www.openpay.com.mx
Description: Plugin that allows you to enable Open Pay as a Payment Gateway in your site
Author: Red Core Technologies
Version: 1.0
Author URI: http://www.redcore.com.mx
*/
add_action( 'plugins_loaded', 'woocommerce_openpay_init', 0 );
function woocommerce_openpay_init() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
};
DEFINE ('PLUGIN_DIR', plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) . '/' );
DEFINE ('DIR_PATH', basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) . '/' );
/**
* WooCommerce Gateway Documentation
* http://docs.woothemes.com/document/payment-gateway-api/
*/
class WC_OpenPay extends WC_Payment_Gateway {
public function __construct() {
global $woocommerce;
$this->id = 'openpay';//Required
$this->method_title = __( 'Open Pay', 'woocommerce' );//Required
$this->has_fields = false;//Required
$this->icon = apply_filters( 'woocommerce_techprocess_icon', $woocommerce->plugin_url() . '-openpay/assets/images/icons/openpay.png' );//Required
// Load the form fields.
$this->init_form_fields(); //Required
// Load the settings.
$this->init_settings(); //Required
$this->title = $this->get_option('title');
$this->description = $this->get_option('description'); //Required
$this->gatewayurl = $this->get_option('gatewayurl');
$this->responseurl = $this->get_option('responseurl');
$this->email = $this->get_option('email');
$this->openpay_private_key = $this->get_option("openpay_private_key");
$this->openpay_public_key = $this->get_option("openpay_public_key");
$this->openpay_id = $this->get_option("openpay_id");
$this->openpay_sandbox = $this->get_option("openpay_sandbox");
//must be lowercase
//you MUST use the NAME OF YOUR CLASS
//Callback function should be named check_response but you may use a different one
//Usage: http://myurl.com/?wc-api=WC_OpenPay
// Where WC_OpenPay is the name of my class issued
add_action('woocommerce_api_'.strtolower(get_class($this)), array($this, 'check_response') );
add_action('woocommerce_receipt_openpay', array(&$this, 'receipt_page'));
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
if ( !$this->is_valid_for_use() ) $this->enabled = false;
}
/**
* Initialise Gateway Settings Form Fields
*
* @access public
* @return void
*/
function init_form_fields()
{
global $woocommerce;
$order = new WC_Order( $order_id );
$this->form_fields = array(
'enabled' => array
(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Openpay', 'woocommerce' ),
'default' => 'yes'
),
'openpay_sandbox' => array
(
'title' => __( 'Enable/Disable Sandbox', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Sandbox Mode', 'woocommerce' ),
'default' => 'no'
),
'title' => array
(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This is the title the customer can see when checking out', 'woocommerce' ),
'default' => __( 'Openpay', 'woocommerce' )
),
'description' => array
(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This is the description the customer can see when checking out', 'woocommerce' ),
'default' => __("Tarjetas de crédito y débito, Tienda de conveniencia, Transferencias interbancarias", 'woocommerce')
),
'email' => array
(
'title' => __( 'Email for send Notification', 'woocommerce' ),
'type' => 'email',
'description' => __( '', 'woocommerce' ),
'default' => 'example@mail.com'
),
'responseurl' => array
(
'title' => __( 'Response URL', 'woocommerce' ),
'type' => 'url',
'disabled' => true,
'description' => __( 'This is the URL which needs to be configured into the Merchant Administration Console - Response URL', 'woocommerce' ),
'default' => __(home_url() . "/?wc-api=wc_openpay" , 'woocommerce')
),
'gatewayurl' => array
(
'title' => __( 'Gateway URL', 'woocommerce' ),
'type' => 'url',
'description' => __( 'This is obtained through the Merchant Administration Console - Gateway URL', 'woocommerce' ),
'default' => ''
),
'openpay_id' => array
(
'title' => __( 'OpenPay ID', 'woocommerce' ),
'type' => 'text',
'description' => __( 'OpenPay user ID' ),
'default' => ''
),
'openpay_public_key' => array
(
'title' => __( 'OpenPay Public Key', 'woocommerce' ),
'type' => 'text',
'description' => __( 'OpenPay Public Key' ),
'default' => ''
),
'openpay_private_key' => array
(
'title' => __( 'OpenPay Private Key', 'woocommerce' ),
'type' => 'text',
'description' => __( 'OpenPay user Private Key' ),
'default' => ''
)
);
}
function successful_request( $posted ) {
global $woocommerce;
$order_id_key=$posted['customerReference'];
$order_id_key=explode("-",$order_id_key);
$order_id=$order_id_key[0];
$order_key=$order_id_key[1];
$responseCode=$posted['responseCode'];
$responseText=$posted['responseText'];
$txnreference=$posted['txnreference'];
$order = new WC_Order( $order_id );
if ( $order->order_key !== $order_key ) :
echo 'Error: Order Key does not match invoice.';
exit;
endif;
if ( $order->get_total() != $posted['amount'] ) {
echo 'Error: Amount not match.';
$order->update_status( 'on-hold', sprintf( __( 'Validation error: Techprocess amounts do not match (%s).', 'woocommerce' ), $posted['amount'] ) );
exit;
}
// if TXN is approved
if($responseCode=="00" || $responseCode=="08" || $responseCode=="77")
{
// Payment completed
$order->add_order_note( __('payment completed', 'woocommerce') );
// Mark order complete
$order->payment_complete();
// Empty cart and clear session
$woocommerce->cart->empty_cart();
// Redirect to thank you URL
wp_redirect( $this->get_return_url( $order ) );
exit;
}
else // TXN has declined
{
// Change the status to pending / unpaid
$order->update_status('pending', __('Payment declined', 'woothemes'));
// Add a note with the IPG details on it
$order->add_order_note(__('Techprocess payment Failed - TransactionReference: ' . $txnreference . " - ResponseCode: " .$responseCode, 'woocommerce')); // FAILURE NOTE
// Add error for the customer when we return back to the cart
$woocommerce->add_error(__('TRANSACTION DECLINED: ', 'woothemes') . $posted['responseText'] . "<br/>Reference: " . $txnreference);
// Redirect back to the last step in the checkout process
wp_redirect( $woocommerce->cart->get_checkout_url());
exit;
}
}
/**
* Check if this gateway is enabled and available in the user's country
*
* @access public
* @return bool
*/
function is_valid_for_use() {
if (!in_array(get_woocommerce_currency(), array('MXN'))) return false;
return true;
}
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
*
* @since 1.0.0
*/
public function admin_options() {
?>
<h3><?php _e('OpenPay', 'woocommerce'); ?></h3>
<table class="form-table">
<?php
if ( $this->is_valid_for_use() ) :
// Generate the HTML For the settings form
$this->generate_settings_html();
else :
?>
<div class="inline error"><p><strong><?php _e( 'Gateway Disabled', 'woocommerce' ); ?></strong>: <?php _e( 'OpenPay está diseñado para México y solo soporta pago en pesos mexicanos. (MXN)', 'woocommerce' ); ?></p></div>
<?php
endif;
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="verification_code">Código de verificación</label></th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span>Open Pay Código de Verificación</span></legend>
<label for="woocommerce_openpay_code_private">
<input id="verification_code" type="text" disabled value="<?php echo get_option('openpay_code_private'); ?>" />
<p class="description">Utilice este código para validar la página en OpenPay</p>
</fieldset>
</td>
</tr>
</table><!--/.form-table-->
<?php
}
/**
* Process the payment and return the result
*
* @access public
* @param int $order_id
* @return array
*/
function process_payment( $order_id ){
global $woocommerce;
$order = new WC_Order( $order_id );
return array
(
'result' => 'success',
'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay'))))
);
}
function receipt_page( $order )
{
echo '<p>'.__('Thank you for your order, click on submit to process Techprocess payment.', 'woocommerce').'</p>';
echo $this->generate_openpay_form( $order );
}
function generate_openpay_form( $order_id )
{
global $woocommerce;
$postdata = http_build_query(
array(
'actionURL' => __(home_url()). "/?wc-api=wc_openpay",
'order_id' => $order_id,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$formTemplate = file_get_contents(PLUGIN_DIR.'template/form.php', false, $context);
/*Load API KEY of openpay*/
$formTemplate = '<input type="hidden" name="id_openpay" id="id_openpay" value="'.$this->openpay_id.'">'.'<input type="hidden" id="api_key_openpay" name="api_key_openpay" value="'.$this->openpay_public_key.'">'.$formTemplate;
return $formTemplate;
}
/**
* This is for send email notification
* (From)email of the plugin, (to) email of the order
*/
function send_notification($order_id, $type){
$order = new WC_Order( $order_id );
$to_email = $order->billing_email;
$headers = 'From: OpenPay <'.$this->email.'>' . "\r\n";
wp_mail($to_email, $type, 'Mensaje', $headers );
}
/**
* This is the callback we declared
* For sanity we respond HTTP Status 200
* You should only print if necesary
*/
function check_response() {
global $woocommerce;
global $wpdb;
// Load the settings.
$this->init_settings();
if($_POST){
if($_POST['action']==='openpay_pay_creditcard'){
$this->openpay_pay_creditcard();
$woocommerce->cart->empty_cart();
//exit();
wp_redirect( $this->get_return_url( $order ) );
}elseif($_POST['action']==='openpay_pay_store'){
$this->openpay_pay_store();
$woocommerce->cart->empty_cart();
exit();
wp_redirect( $this->get_return_url( $order ) );
}elseif ($_POST['action']==='openpay_pay_transfer') {
$this->openpay_pay_transfer();
$woocommerce->cart->empty_cart();
exit();
wp_redirect( $this->get_return_url( $order ) );
}
}else{
header('Content-Type: application/json;charset=utf-8;');
$data = json_decode(file_get_contents('php://input'));
$order = new WC_Order( $data->transaction->order_id );
$fetchOption = get_option('openpay_code_private');
if(update_option('openpay_code_private', $data->verification_code)){
$fetchOption2 = " Yes | ";
}else{
$fetchOption2 = " No | ";
}
$fetchOption2 .= get_option('openpay_code_private');
$comma_delmited_list = "Response: ".print_r($data,true). "\n Data:".print_r($fetchOption,true)." ".print_r($fetchOption2,true);
$fp = fopen("array.txt","a");
if( $fp == false ){
//do debugging or logging here
}else{
fwrite($fp,$comma_delmited_list);
fclose($fp);
}
if($data->verification_code){
wp_die( "Openpay IPN Request Verification Code ->".$data->verification_code, "Openpay IPN", array( 'response' => 200 ) );
}else{
$status = "";
$note = "";
$flag = 0;
switch ($data->type) {
case "charge.succeeded":
$flag = 1;
$status = "processing";
$note = "Payment received and stock has been reduced- the order is awaiting fulfilment";
break;
case "charge.created":
$flag = 1;
$status = "on-hold";
$note = "Awaiting payment – stock is reduced, but you need to confirm payment";
break;
case "charge.refunded":
$flag = 1;
$status = "refunded";
$note = "Refunded by an admin - no further action required";
break;
case "spei.received":
$flag = 1;
$status = "processing";
$note = "Payment received and stock has been reduced- the order is awaiting fulfilment";
break;
}
$pays_table = $wpdb->prefix . 'woocommerce_openpay_pays';
$wpdb->insert(
$pays_table,
array(
'ID_ORDER' => $data->transaction->order_id,
'ID_OPENPAY_CHARGE' => $data->transaction->id,
'METHOD' => $data->transaction->method,
'STATUS' => $data->transaction->status,
'TYPE' => $data->type,
'CREATED' => $data->event_date
),
array(
'%d',
'%s',
'%s',
'%s',
'%s'
)
);
if($flag==1){
//$order = new WC_Order( $data->transaction->order_id );
$order->update_status($status, 'order_note');
send_notification($order->id,$status);
}
wp_die( "Openpay IPN Request Failure", "Openpay IPN", array( 'response' => 200 ) );
}
}
}
/**
* This is for get Code from the country
*/
function get_country_code($country){
$countries = array
(
'AF' => 'Afghanistan',
'AX' => 'Aland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
'AD' => 'Andorra',
'AO' => 'Angola',
'AI' => 'Anguilla',
'AQ' => 'Antarctica',
'AG' => 'Antigua And Barbuda',
'AR' => 'Argentina',
'AM' => 'Armenia',
'AW' => 'Aruba',
'AU' => 'Australia',
'AT' => 'Austria',
'AZ' => 'Azerbaijan',
'BS' => 'Bahamas',
'BH' => 'Bahrain',
'BD' => 'Bangladesh',
'BB' => 'Barbados',
'BY' => 'Belarus',
'BE' => 'Belgium',
'BZ' => 'Belize',
'BJ' => 'Benin',
'BM' => 'Bermuda',
'BT' => 'Bhutan',
'BO' => 'Bolivia',
'BA' => 'Bosnia And Herzegovina',
'BW' => 'Botswana',
'BV' => 'Bouvet Island',
'BR' => 'Brazil',
'IO' => 'British Indian Ocean Territory',
'BN' => 'Brunei Darussalam',
'BG' => 'Bulgaria',
'BF' => 'Burkina Faso',
'BI' => 'Burundi',
'KH' => 'Cambodia',
'CM' => 'Cameroon',
'CA' => 'Canada',
'CV' => 'Cape Verde',
'KY' => 'Cayman Islands',
'CF' => 'Central African Republic',
'TD' => 'Chad',
'CL' => 'Chile',
'CN' => 'China',
'CX' => 'Christmas Island',
'CC' => 'Cocos (Keeling) Islands',
'CO' => 'Colombia',
'KM' => 'Comoros',
'CG' => 'Congo',
'CD' => 'Congo, Democratic Republic',
'CK' => 'Cook Islands',
'CR' => 'Costa Rica',
'CI' => 'Cote D\'Ivoire',
'HR' => 'Croatia',
'CU' => 'Cuba',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'DK' => 'Denmark',
'DJ' => 'Djibouti',
'DM' => 'Dominica',
'DO' => 'Dominican Republic',
'EC' => 'Ecuador',
'EG' => 'Egypt',
'SV' => 'El Salvador',
'GQ' => 'Equatorial Guinea',
'ER' => 'Eritrea',
'EE' => 'Estonia',
'ET' => 'Ethiopia',
'FK' => 'Falkland Islands (Malvinas)',
'FO' => 'Faroe Islands',
'FJ' => 'Fiji',
'FI' => 'Finland',
'FR' => 'France',
'GF' => 'French Guiana',
'PF' => 'French Polynesia',
'TF' => 'French Southern Territories',
'GA' => 'Gabon',
'GM' => 'Gambia',
'GE' => 'Georgia',
'DE' => 'Germany',
'GH' => 'Ghana',
'GI' => 'Gibraltar',
'GR' => 'Greece',
'GL' => 'Greenland',
'GD' => 'Grenada',
'GP' => 'Guadeloupe',
'GU' => 'Guam',
'GT' => 'Guatemala',
'GG' => 'Guernsey',
'GN' => 'Guinea',
'GW' => 'Guinea-Bissau',
'GY' => 'Guyana',
'HT' => 'Haiti',
'HM' => 'Heard Island & Mcdonald Islands',
'VA' => 'Holy See (Vatican City State)',
'HN' => 'Honduras',
'HK' => 'Hong Kong',
'HU' => 'Hungary',
'IS' => 'Iceland',
'IN' => 'India',
'ID' => 'Indonesia',
'IR' => 'Iran, Islamic Republic Of',
'IQ' => 'Iraq',
'IE' => 'Ireland',
'IM' => 'Isle Of Man',
'IL' => 'Israel',
'IT' => 'Italy',
'JM' => 'Jamaica',
'JP' => 'Japan',
'JE' => 'Jersey',
'JO' => 'Jordan',
'KZ' => 'Kazakhstan',
'KE' => 'Kenya',
'KI' => 'Kiribati',
'KR' => 'Korea',
'KW' => 'Kuwait',
'KG' => 'Kyrgyzstan',
'LA' => 'Lao People\'s Democratic Republic',
'LV' => 'Latvia',
'LB' => 'Lebanon',
'LS' => 'Lesotho',
'LR' => 'Liberia',
'LY' => 'Libyan Arab Jamahiriya',
'LI' => 'Liechtenstein',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'MO' => 'Macao',
'MK' => 'Macedonia',
'MG' => 'Madagascar',
'MW' => 'Malawi',
'MY' => 'Malaysia',
'MV' => 'Maldives',
'ML' => 'Mali',
'MT' => 'Malta',
'MH' => 'Marshall Islands',
'MQ' => 'Martinique',
'MR' => 'Mauritania',
'MU' => 'Mauritius',
'YT' => 'Mayotte',
'MX' => 'Mexico',
'FM' => 'Micronesia, Federated States Of',
'MD' => 'Moldova',
'MC' => 'Monaco',
'MN' => 'Mongolia',
'ME' => 'Montenegro',
'MS' => 'Montserrat',
'MA' => 'Morocco',
'MZ' => 'Mozambique',
'MM' => 'Myanmar',
'NA' => 'Namibia',
'NR' => 'Nauru',
'NP' => 'Nepal',
'NL' => 'Netherlands',
'AN' => 'Netherlands Antilles',
'NC' => 'New Caledonia',
'NZ' => 'New Zealand',
'NI' => 'Nicaragua',
'NE' => 'Niger',
'NG' => 'Nigeria',
'NU' => 'Niue',
'NF' => 'Norfolk Island',
'MP' => 'Northern Mariana Islands',
'NO' => 'Norway',
'OM' => 'Oman',
'PK' => 'Pakistan',
'PW' => 'Palau',
'PS' => 'Palestinian Territory, Occupied',
'PA' => 'Panama',
'PG' => 'Papua New Guinea',
'PY' => 'Paraguay',
'PE' => 'Peru',
'PH' => 'Philippines',
'PN' => 'Pitcairn',
'PL' => 'Poland',
'PT' => 'Portugal',
'PR' => 'Puerto Rico',
'QA' => 'Qatar',
'RE' => 'Reunion',
'RO' => 'Romania',
'RU' => 'Russian Federation',
'RW' => 'Rwanda',
'BL' => 'Saint Barthelemy',
'SH' => 'Saint Helena',
'KN' => 'Saint Kitts And Nevis',
'LC' => 'Saint Lucia',
'MF' => 'Saint Martin',
'PM' => 'Saint Pierre And Miquelon',
'VC' => 'Saint Vincent And Grenadines',
'WS' => 'Samoa',
'SM' => 'San Marino',
'ST' => 'Sao Tome And Principe',
'SA' => 'Saudi Arabia',
'SN' => 'Senegal',
'RS' => 'Serbia',
'SC' => 'Seychelles',
'SL' => 'Sierra Leone',
'SG' => 'Singapore',
'SK' => 'Slovakia',
'SI' => 'Slovenia',
'SB' => 'Solomon Islands',
'SO' => 'Somalia',
'ZA' => 'South Africa',
'GS' => 'South Georgia And Sandwich Isl.',
'ES' => 'Spain',
'LK' => 'Sri Lanka',
'SD' => 'Sudan',
'SR' => 'Suriname',
'SJ' => 'Svalbard And Jan Mayen',
'SZ' => 'Swaziland',
'SE' => 'Sweden',
'CH' => 'Switzerland',
'SY' => 'Syrian Arab Republic',
'TW' => 'Taiwan',
'TJ' => 'Tajikistan',
'TZ' => 'Tanzania',
'TH' => 'Thailand',
'TL' => 'Timor-Leste',
'TG' => 'Togo',
'TK' => 'Tokelau',
'TO' => 'Tonga',
'TT' => 'Trinidad And Tobago',
'TN' => 'Tunisia',
'TR' => 'Turkey',
'TM' => 'Turkmenistan',
'TC' => 'Turks And Caicos Islands',
'TV' => 'Tuvalu',
'UG' => 'Uganda',
'UA' => 'Ukraine',
'AE' => 'United Arab Emirates',
'GB' => 'United Kingdom',
'US' => 'United States',
'UM' => 'United States Outlying Islands',
'UY' => 'Uruguay',
'UZ' => 'Uzbekistan',
'VU' => 'Vanuatu',
'VE' => 'Venezuela',
'VN' => 'Viet Nam',
'VG' => 'Virgin Islands, British',
'VI' => 'Virgin Islands, U.S.',
'WF' => 'Wallis And Futuna',
'EH' => 'Western Sahara',
'YE' => 'Yemen',
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe',
);
$code = "";
foreach ($countries as $k => $v) {
if($v == $country){
$code = $a[$k];
break;
}
}
return $code;
}
function openpay_pay_creditcard(){
global $woocommerce;
global $wpdb;
require_once 'lib/Openpay.php';
$order = new WC_Order( $_POST["order-id"] );
$openpay = Openpay::getInstance($this->openpay_id, $this->openpay_private_key);
//Custumer exist?
$customers_table = $wpdb->prefix . 'woocommerce_openpay_customers';
$customers_data = $wpdb->get_results('SELECT * FROM '.$customers_table.' WHERE ID_USER='.$order->user_id );
if($customers_data){
//exist Customer
$customer = $openpay->customers->get($customers_data[0]->ID_OPENPAY_CUSTOMER);
}else{
//not exist Customer
$customerData = array(
'external_id' => $order->user_id,
'name' => $order->billing_first_name,
'last_name' => $order->billing_last_name,
'email' => $order->billing_email,
'requires_account' => false,
'phone_number' => $order->billing_phone,
'address' => array(
'line1' => $order->billing_address_1,
'line2' => $order->billing_address_2,
'line3' => '',
'state' => $order->billing_state,
'city' => $order->billing_city,
'postal_code' => $order->billing_postcode,
'country_code' => $order->billing_country
)
);
$customer = $openpay->customers->add($customerData);
$wpdb->insert(
$customers_table,
array(
'ID_USER' => $order->user_id,
'ID_OPENPAY_CUSTOMER' => $customer->id
),
array(
'%d',
'%s'
)
);
}
//Getting order name and quantity
foreach($order->get_items() as $item)
{
$json['items'][] = array (
'name' => $item['name'], // Here appear the name of the product
'quantity' => $item['qty']); // here the quantity of the product
}
$item_string = json_encode($json).'';
if(strlen($item_string)> 250){
$item_string = substr($item_string,0,249);
}
$chargeData = array(
'method' => 'card',
'source_id' => $_POST["token_id"],
'amount' => (float)$order->get_total(),
'order_id' => $_POST["order-id"],
'description' => $item_string,
'device_session_id' => $_POST["deviceIdHiddenFieldName"]
);
//Charge with customer
//$charge = $openpay->charges->create($chargeData);
$charge = $customer->charges->create($chargeData);
echo "se cobró con tarjeta de crédito";
}
function openpay_pay_store(){
global $woocommerce;
require_once 'lib/Openpay.php';
$order = new WC_Order( $_POST["order-id"] );
$openpay = Openpay::getInstance($this->openpay_id, $this->openpay_private_key);
$chargeData = array(
'method' => 'store',
'amount' => (float)$order->get_total(),
'order_id' => $_POST["order-id"],
'description' => "Compra con Open Pay: " .$order->id );
$charge = $openpay->charges->create($chargeData);
/*Date manager*/
str_replace("T","",$charge->operation_date);
$date = substr($date, 0, -6);
$date = new DateTime($date);
$date = date_format($date, 'd F Y, g:i A');
echo '<meta charset="UTF-8">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/jumbotron-narrow.css" rel="stylesheet" type="text/css">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/print.css" rel="stylesheet" type="text/css" media="print">';
echo '<script src="'.$woocommerce->plugin_url() . '-openpay/assets/js/bootstrap.min.js" ></script>';
echo '<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<img class="img-responsive center-block" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/logo.png" alt="Logo">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<p class="Logo_paynet">Servicio a pagar</p>
<img class="img-responsive center-block Logo_paynet" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/paynet_logo.png" alt="Logo">
</div>
</div>
<div class="row data">
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
<div class="Big_Bullet">
<span></span>
</div>
<h1><strong>Código de barra</strong></h1>
<div class="col-lg-12 datos_pago">
<!--<h4>30 de Noviembre 2014, a las 2:30 AM</h4>-->
<img width="300" src="'. $charge->payment_method->barcode_url .'" alt="Código de Barras">
<span>'. $charge->payment_method->reference.'</span>
<br/>
<p>En caso de que el escáner no sea capaz de leer el código de barras, escribir la referencia tal como se muestra.</p>
</div>
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4 data_amount">
<h2>Total a pagar</h2>
<h2 class="amount">$'. (float)$charge->amount.'<small> MXN</small></h2>
<h2 class="S-margin">+8 pesos por comisión</h2>
</div>
</div>
<div class="row data">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="Big_Bullet">
<span></span>
</div>
<h1><strong>Detalles de la compra</strong></h1>
<div class="col-lg-12 datos_tiendas">
<p><strong>Orden: </strong> '. $charge->order_id.'</p>
<p><strong>Fecha y hora: </strong>'. $date.'</p>
<p><strong>Correo electrónico: </strong>'.$order->billing_email.'</p>
</div>
</div>
</div>
<div class="row data">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="Big_Bullet">
<span></span>
</div>
<h1><strong>Como realizar el pago</strong></h1>
<ol style="margin-left: 30px;">
<li>Acude a cualquier tienda afiliada</li>
<li>Entrega al cajero el código de barras y menciona que realizarás un pago de servicio Paynet</li>
<li>Realizar el pago en efectivo por $'. (float)$charge->amount.' MXN (más $8 pesos por comisión)</li>
<li>Conserva el ticket para cualquier aclaración</li>
</ol>
<small>'.$this->description.'</small>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h1><strong>Instrucciones para el cajero</strong></h1>
<ol>
<li>Ingresar al menú de Pago de Servicios</li>
<li>Seleccionar Paynet</li>
<li>Escanear el código de barras o ingresar el núm. de referencia</li>
<li>Ingresa la cantidad total a pagar</li>
<li>Cobrar al cliente el monto total más la comisión de $8 pesos</li>
<li>Confirmar la transacción y entregar el ticket al cliente</li>
</ol>
<small>Para cualquier duda sobre como cobrar, por favor llamar al teléfono 01 800 300 08 08 en un horario de 8am a 9pm de lunes a domingo</small>
</div>
</div>
<div class="row marketing">
<div class="col-lg-12" style="text-align:center;">
<img width="50" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/7eleven.png" alt="7elven">
<img width="90" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/extra.png" alt="7elven">
<img width="90" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/farmacia_ahorro.png" alt="7elven">
<img width="150" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/benavides.png" alt="7elven">
<div class="link_tiendas">¿Quieres pagar en otras tiendas? visita: <a target="_blank" href="http://www.openpay.mx/tiendas-de-conveniencia.html">www.openpay.mx/tiendas</a></div>
</div>
<div class="col-lg-12" style="text-align:center; margin-top:5px;">
<a type="button" class="btn btn-success btn-lg" onclick="window.print();">Imprimir</a>
<a type="button" class="btn btn-success btn-lg" href="'.home_url().'">Sigue comprando</a>
</div>
</div>
<div class="footer">
<img class="img-responsive center-block" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/powered_openpay.png" alt="Powered by Openpay">
</div>
</div> <!-- /container -->';
}
function openpay_pay_transfer(){
global $woocommerce;
require_once 'lib/Openpay.php';
$order = new WC_Order( $_POST["order-id"] );
$openpay = Openpay::getInstance($this->openpay_id, $this->openpay_private_key);
$chargeData = array(
'method' => 'bank_account',
'amount' => (float)$order->get_total(),
'description' => "Compra con Open Pay: " .$order->id,
'order_id' => $_POST["order-id"] );
$charge = $openpay->charges->create($chargeData);
/*Date manager*/
str_replace("T","",$charge->operation_date);
$date = substr($date, 0, -6);
$date = new DateTime($date);
$date = date_format($date, 'd F Y, g:i A');
echo '<meta charset="UTF-8">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/jumbotron-narrow.css" rel="stylesheet" type="text/css">';
echo '<link href="'.$woocommerce->plugin_url() . '-openpay/assets/css/print.css" rel="stylesheet" type="text/css" media="print">';
echo '<script src="'.$woocommerce->plugin_url() . '-openpay/assets/js/bootstrap.min.js" ></script>';
echo '<div class="container">
<div class="row header">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<img style="background-color: #fff;" class="img-responsive center-block" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/logo.png" alt="Logo">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<p class="Yellow2">Esperamos tu pago</p>
</div>
</div>
<div class="row">
<div class="col-xs-9 col-sm-8 col-md-8 col-lg-8">
<h1><strong>Cantidad a pagar</strong></h1>
<h2 class="amount">$'. (float)$charge->amount.'<small> MXN</small></h2>
<h1><strong>Fecha:</strong></h1>
<h1>'. $date.'</h1>
</div>
<div class="col-xs-3 col-sm-4 col-md-4 col-lg-4">
<a href="http://www.openpay.mx/bancos.html" target="_blank"><img class="img-responsive spei" src="'.$woocommerce->plugin_url() . '-openpay/assets/images/spei.gif" alt="SPEI"></a>
</div>
</div>
<div class="row marketing">
<h1 style="padding-bottom:20px;"><strong>Datos para transferencia electrónica</strong></h1>
<div class="col-lg-12 datos">
<p><strong>Nombre del banco: </strong>'.$charge->payment_method->bank.'</p>
<p><strong>CLABE: </strong>'. $charge->payment_method->clabe .'</p>
<p><strong>Referencia numérica: </strong>'. $charge->payment_method->name .'</p>
<p><strong>Orden: </strong>'. $charge->order_id.'</p>
</div>
<div class="col-lg-12">
<p>Tu correo: <strong>'.$order->billing_email.'</strong></p>