-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_postfinance.module
1590 lines (1459 loc) · 101 KB
/
payment_postfinance.module
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
/**
* @file
* Implements Postfinance payment services for use with Drupal Payment.
*/
/**
* Implements hook_menu().
*
* adds the callback uri
*/
function payment_postfinance_menu() {
$items = array();
$items['payment_postfinance/redirect/%entity_object'] = array(
'load arguments' => array('payment'),
'title' => 'Go to payment server',
'page callback' => 'drupal_get_form',
'page arguments' => array('payment_postfinance_form_redirect', 2),
'access callback' => 'payment_postfinance_form_redirect_access',
'access arguments' => array(2),
'type' => MENU_CALLBACK,
);
$items['payment_postfinance/return/%entity_object'] = array(
'load arguments' => array('payment'),
'title' => 'Postfinance return url',
'page callback' => 'payment_postfinance_return',
'page arguments' => array(2),
'access callback' => 'payment_postfinance_return_access',
'access arguments' => array(2),
'type' => MENU_CALLBACK,
);
$items['payment_postfinance/return/cancel/%entity_object/%'] = array(
'load arguments' => array('payment'),
'title' => 'Postfinance return url',
'page callback' => 'payment_postfinance_return_cancel',
'page arguments' => array(3, 4),
'access callback' => 'payment_postfinance_return_cancel_access',
'access arguments' => array(3, 4),
'type' => MENU_CALLBACK,
);
// Define an always accessible path to receive IPNs.
$items['payment_postfinance/IPN/%'] = array(
'page callback' => 'payment_postfinance_process_ipn',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_payment_method_controller_info().
*/
function payment_postfinance_payment_method_controller_info() {
return array(
'PostfinancePaymentMethodController',
'PostfinanceEFinancePaymentMethodController',
'PostfinancePostCardPaymentMethodController',
'PostfinanceAmexcoPaymentMethodController',
'PostfinanceMasterCardPaymentMethodController',
'PostfinanceVisaPaymentMethodController',
'PostfinancePaypalPaymentMethodController',
);
}
/**
* Implements hook_entity_load().
*/
function payment_postfinance_entity_load(array $entities, $entity_type) {
if ($entity_type == 'payment_method') {
$pmids = array();
$postfinance_payment_method_controller = payment_postfinance_payment_method_controller_info();
foreach ($entities as $payment_method) {
if( in_array($payment_method->controller->name, $postfinance_payment_method_controller) ){
//if ($payment_method->controller->name == 'PayPalPaymentPPSPaymentMethodController') {
$pmids[] = $payment_method->pmid;
}
}
if ($pmids) {
$query = db_select('payment_postfinance_payment_method')
->fields('payment_postfinance_payment_method')
->condition('pmid', $pmids);
$result = $query->execute();
while ($data = $result->fetchAssoc()) {
$payment_method = $entities[$data['pmid']];
$payment_method->controller_data = (array) $data;
unset($payment_method->controller_data['pmid']);
}
}
}
}
/**
* Implements hook_ENTITY_TYPE_ACTION().
*/
function payment_postfinance_payment_method_insert(PaymentMethod $payment_method) {
$postfinance_payment_method_controller = payment_postfinance_payment_method_controller_info();
if( in_array($payment_method->controller->name, $postfinance_payment_method_controller) ){
//if ($payment_method->controller->name == 'PostfinancePaymentMethodController') {
$values = $payment_method->controller_data += $payment_method->controller->controller_data_defaults;
$values['pmid'] = $payment_method->pmid;
drupal_write_record('payment_postfinance_payment_method', $values);
}
}
/**
* Implements hook_ENTITY_TYPE_ACTION().
*/
function payment_postfinance_payment_method_update(PaymentMethod $payment_method) {
$postfinance_payment_method_controller = payment_postfinance_payment_method_controller_info();
if( in_array($payment_method->controller->name, $postfinance_payment_method_controller) ){
//if ($payment_method->controller->name == 'PostfinancePaymentMethodController') {
$values = $payment_method->controller_data += $payment_method->controller->controller_data_defaults;
$values['pmid'] = $payment_method->pmid;
drupal_write_record('payment_postfinance_payment_method', $values, 'pmid');
}
}
/**
* Implements hook_ENTITY_TYPE_ACTION().
*/
function payment_postfinance_payment_method_delete($entity) {
$postfinance_payment_method_controller = payment_postfinance_payment_method_controller_info();
if( in_array($entity->controller->name, $postfinance_payment_method_controller) ){
//if ($entity->controller->name == 'PostfinancePaymentMethodController') {
db_delete('payment_postfinance_payment_method')
->condition('pmid', $entity->pmid)
->execute();
}
}
/**
* Form build callback: implements
* PaymentMethodController::payment_method_configuration_form_elements_callback.
*/
function payment_postfinance_payment_method_configuration_form_elements(array $form, array &$form_state) {
$payment_method = $form_state['payment_method'];
$controller = $payment_method->controller;
$controller_data = $payment_method->controller_data + $controller->controller_data_defaults;
//$form = array();
//dpm($controller_data);
$elements['pspid'] = array(
'#default_value' => $controller_data['pspid'],
'#required' => TRUE,
'#title' => t('PSPID'),
'#type' => 'textfield',
);
$elements['currency_code'] = array(
'#type' => 'select',
'#title' => t('Currency code'),
'#description' => t('Transactions can only be processed in one of the listed currencies.'),
'#options' => payment_postfinance_default_currencies(),
'#default_value' => $controller_data['currency_code'],
);
$elements['language'] = array(
'#type' => 'select',
'#title' => t('Default language submitted to Postfinance e-Payment'),
'#options' => payment_postfinance_default_languages(),
'#default_value' => $controller_data['language'],
);
$elements['server'] = array(
'#type' => 'radios',
'#title' => t('Postfinance server'),
'#options' => array(
'test' => ('Test'),
'prod' => ('Production'),
),
'#default_value' => $controller_data['server'],
);
/* UTF is more or less standard, maybe remove this */
$elements['server_encoding'] = array(
'#type' => 'radios',
'#title' => t('The encoding you entered in the Postfinance e-Payment configuration'),
'#options' => array(
'iso' => ('ISO-8859-1'),
'utf' => ('UTF-8'),
),
'#default_value' => $controller_data['server_encoding'],
);
$elements['payment_security'] = array(
'#type' => 'radios',
'#title' => t('Digest Encryption (SHA-IN and SHA-OUT)'),
'#options' => array(
'sha1' => t('SHA-1'),
'sha256' => t('SHA-256'),
'sha512' => t('SHA-512'),
),
'#default_value' => $controller_data['payment_security'],
);
$elements['payment_security_key'] = array(
'#type' => 'textfield',
'#title' => t('SHA-X-IN Key'),
'#description' => t('The Key you entered in the Postfinance e-Payment configuration'),
'#default_value' => $controller_data['payment_security_key'],
'#required' => TRUE,
);
$elements['reply_security_key'] = array(
'#type' => 'textfield',
'#title' => t('SHA-X-OUT Key'),
'#description' => t('The Key you entered in the Postfinance e-Payment configuration'),
'#default_value' => $controller_data['reply_security_key'],
'#required' => TRUE,
);
$elements['shop_uri'] = array(
'#type' => 'textfield',
'#title' => t('Shop URI'),
'#description' => t('Used as return url for the Merchant Shop button on Postfinance, URI after ' . url('<front>', array('absolute' => TRUE)) . ' (or leave empty to go to this main URL)' ),
'#default_value' => $controller_data['shop_uri'],
);
$elements['cancel_uri'] = array(
'#type' => 'textfield',
'#title' => t('Cancellation URI'),
'#description' => t('Used as return url if order is canceled by the user on Postfinance side, URI after ' . url('<front>', array('absolute' => TRUE)) . ' (or leave empty to go to this main URL)' ),
'#default_value' => $controller_data['cancel_uri'],
);
$elements['order_prefix'] = array(
'#type' => 'textfield',
'#title' => t('Orderprefix'),
'#description' => t('This will be placed in front of the order number submitted to Postfinance e-Payment.'),
'#default_value' => $controller_data['order_prefix']
);
$elements['template_page'] = array(
'#type' => 'textfield',
'#title' => t('Template Page'),
'#description' => t('This .html file will be used as template file by Postfinance'),
'#default_value' => $controller_data['template_page']
);
//XXX
$elements['bgcolor'] = array(
'#type' => 'textfield',
'#title' => t('Background color'),
'#description' => t('Background color on the payment page'),
'#default_value' => $controller_data['bgcolor']
);
$elements['txtcolor'] = array(
'#type' => 'textfield',
'#title' => t('Text color'),
'#description' => t('Text color on the payment page'),
'#default_value' => $controller_data['txtcolor']
);
$elements['title'] = array(
'#type' => 'textfield',
'#title' => t('Payment page title'),
'#description' => t('Custom payment page title'),
'#default_value' => $controller_data['title']
);
$elements['logo'] = array(
'#type' => 'textfield',
'#title' => t('Logo'),
'#description' => t('Custom logo. The file is hosted on the Postfinance server. Do not add slash in the file name.'),
'#default_value' => $controller_data['logo']
);
return $elements;
}
/**
* Implements form validate callback for
* payment_postfinance_payment_method_configuration_form_elements().
*/
function payment_postfinance_payment_method_configuration_form_elements_validate(array $element, array &$form_state) {
$values = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
$controller_data = &$form_state['payment_method']->controller_data;
$controller_data['pspid'] = $values['pspid'];
$controller_data['currency_code'] = $values['currency_code'];
$controller_data['language'] = $values['language'];
$controller_data['server'] = $values['server'];
$controller_data['server_encoding'] = $values['server_encoding'];
$controller_data['payment_security'] = $values['payment_security'];
$controller_data['payment_security_key'] = $values['payment_security_key'];
$controller_data['reply_security_key'] = $values['reply_security_key'];
$controller_data['shop_uri'] = $values['shop_uri'];
$controller_data['cancel_uri'] = $values['cancel_uri'];
$controller_data['order_prefix'] = $values['order_prefix'];
$controller_data['template_page'] = $values['template_page'];
$controller_data['bgcolor'] = $values['bgcolor'];
$controller_data['txtcolor'] = $values['txtcolor'];
$controller_data['title'] = $values['title'];
$controller_data['logo'] = $values['logo'];
//if (!valid_email_address($values['email_address'])) {
// form_error($element['email_address'], t('The email address is not valid.'));
//}
}
/**
* Access callback for the redirect page.
*
* @param Payment $payment
* The payment to check access to.
* @param object $user
* An optional user to check access for. If NULL, then the currently logged
* in user is used.
*
* @return bool
*/
function payment_postfinance_form_redirect_access(Payment $payment, $account = NULL) {
global $user;
/*
$postfinance_payment_method_controller = payment_postfinance_payment_method_controller_info();
if( in_array($payment->method->controller->name, $postfinance_payment_method_controller) ){
}
*/
return is_a($payment->method->controller, 'PostfinancePaymentMethodController')
&& payment_status_is_or_has_ancestor($payment->getStatus()->status, PAYMENT_STATUS_PENDING)
&& isset($_SESSION['payment_postfinance_pid']) && $_SESSION['payment_postfinance_pid'] == $payment->pid;
}
/**
* Payment method callback: redirect form, a wrapper around the module's general
* use function for building a form.
*
* @param $order
* The order object.
* @param $payment_method
* The payment method to be used.
*
* @return
* The form.
*/
function payment_postfinance_form_redirect(array $form, array &$form_state, Payment $payment) {
$controller_data = $payment->method->controller_data;
//dpm($payment);
//dpm($controller_data);
// Return an error if the enabling action's settings haven't been configured.
if (empty($controller_data['pspid'])) {
drupal_set_message(t('Postfinance e-Payment (Method: @method) is not configured for use. No PSPID has been specified.', array('@method' => $payment_method['instance_id'])), 'error');
return array();
}
$oid = $payment->context_data['oid'];
$settings = array(
// Return to the previous page when payment is canceled
//'back_return' => 'TODO-XXX',// url('checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),
'back_return' => url('payment_postfinance/return/cancel/' . $payment->pid . '/' . PostfinancePaymentMethodController::hashPID($payment->pid), array('absolute' => TRUE,)),
// Return to the payment redirect page for processing successful payments
//'return' => 'TODO-XXX',// url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),
'return' => url('payment_postfinance/return/' . $payment->pid, array('absolute' => TRUE)),
// Specify the current payment method instance ID in the notify_url
'payment_method' => $payment->method->pmid,
//'payment_method' => $payment->method->name,
//get the front-url
'home_url' => url('<front>', array('absolute' => TRUE)),
);
$settings+= $controller_data;
//$settings = $controller_data;
//check the active language
global $language;
$actLanguage = drupal_strtoupper($language->language);
if ($actLanguage == 'EN') {
//EN is en_US for postfinance not en_EN like other languages
$actLanguage = 'US';
}
$defLanguages = array_flip(payment_postfinance_default_languages());
if (in_array($actLanguage, array_keys($defLanguages))) {
$payment_method['settings']['language'] = $defLanguages[$actLanguage];
}
return payment_postfinance_order_form($form, $form_state, $payment, $settings);
}
/**
* needed for every paymentmethod too ..
*/
function payment_postfinance_efinance_form_redirect(array $form, array &$form_state, Payment $payment) {
$payment->method->controller_data['PM'] = 'PostFinance e-finance';
$payment->method->controller_data['BRAND'] = 'PostFinance e-finance';
return payment_postfinance_form_redirect($form, $form_state, $payment);
}
function payment_postfinance_postcard_form_redirect(array $form, array &$form_state, Payment $payment) {
$payment->method->controller_data['PM'] = 'PostFinance Card';
$payment->method->controller_data['BRAND'] = 'PostFinance + card';
return payment_postfinance_form_redirect($form, $form_state, $payment);
}
function payment_postfinance_amexco_form_redirect(array $form, array &$form_state, Payment $payment) {
$payment->method->controller_data['PM'] = 'CreditCard';
$payment->method->controller_data['BRAND'] = 'American Express';
return payment_postfinance_form_redirect($form, $form_state, $payment);
}
function payment_postfinance_mastercard_form_redirect(array $form, array &$form_state, Payment $payment) {
$payment->method->controller_data['PM'] = 'CreditCard';
$payment->method->controller_data['BRAND'] = 'MasterCard';
return payment_postfinance_form_redirect($form, $form_state, $payment);
}
function payment_postfinance_visa_form_redirect(array $form, array &$form_state, Payment $payment) {
$payment->method->controller_data['PM'] = 'CreditCard';
$payment->method->controller_data['BRAND'] = 'VISA';
return payment_postfinance_form_redirect($form, $form_state, $payment);
}
function payment_postfinance_paypal_form_redirect(array $form, array &$form_state, Payment $payment) {
$payment->method->controller_data['PM'] = 'PAYPAL';
$payment->method->controller_data['BRAND'] = 'PAYPAL';
return payment_postfinance_form_redirect($form, $form_state, $payment);
}
/**
* Redirect back needed for every paymentmethod too..
*/
/*
function payment_postfinance_efinance_redirect_form_back(Payment $payment) {
return payment_postfinance_redirect_form_back($payment);
}
function payment_postfinance_postcard_redirect_form_back(Payment $payment) {
return payment_postfinance_redirect_form_back($payment);
}
function payment_postfinance_amexco_redirect_form_back(Payment $payment) {
return payment_postfinance_redirect_form_back($payment);
}
function payment_postfinance_mastercard_redirect_form_back(Payment $payment) {
return payment_postfinance_redirect_form_back($payment);
}
function payment_postfinance_visa_redirect_form_back(Payment $payment) {
return payment_postfinance_redirect_form_back($payment);
}
function payment_postfinance_paypal_redirect_form_back(Payment $payment) {
return payment_postfinance_redirect_form_back($payment);
}
*/
/**
* Centralized callback to handle back, cancel, exception actions.
*/
function payment_postfinance_redirect_form_back(Payment $payment) {
// Check the status we got from postfinance.
$status = (isset($_REQUEST['STATUS'])) ? isset($_REQUEST['STATUS']) : NULL;
$controller_data = $payment->method->controller_data;
switch ($status) {
// Payment cancelled.
case 1:
// Takes care of evaluating the return parameter and updating the order.
ipn_valid('redirect_form_back', $_REQUEST);
// If a cancel redirect is configured send the user to that page.
if (!empty($controller_data['cancel_uri'])) {
drupal_goto($controller_data['cancel_uri']);
}
// As there's no point / access permission to display a cancelled order
// redirect anyway.
$shop_uri = '<front>';
if (!empty($controller_data['shop_uri'])) {
$shop_uri = $controller_data['shop_uri'];
}
drupal_goto($shop_uri);
break;
// Payment declined.
case 2:
case 93:
// Takes care of evaluating the return parameter and updating the order.
ipn_valid('redirect_form_back', $_REQUEST);
$msg = payment_postfinance_IPNStatusText($status);
if (!empty($_REQUEST['NCERROR'])) {
$arr_nc = payment_postfinance_NCErrorText($_REQUEST['NCERROR']);
$msg .= ': ' . $arr_nc['nctext'];
}
drupal_set_message($msg, 'error');
break;
// Payment exception.
case 52:
case 92:
// Takes care of evaluating the return parameter and updating the order.
ipn_valid('redirect_form_back', $_REQUEST);
$msg = payment_postfinance_IPNStatusText($status);
if (!empty($_REQUEST['NCERROR'])) {
$arr_nc = payment_postfinance_NCErrorText($_REQUEST['NCERROR']);
$msg .= ': ' . $arr_nc['nctext'];
}
drupal_set_message($msg, 'error');
break;
// Standard back action.
default:
// Nothing to do, standard handling is sufficient.
}
}
/**
* Return callback.
*/
function payment_postfinance_return(Payment $payment) {
payment_postfinance_redirect_form_back($payment);
}
/**
* Access callback for the return URL.
*
* @return bool
*/
function payment_postfinance_return_access(Payment $payment, $hash) {
return PostfinancePaymentMethodController::hashPID($payment->pid) == $hash;
}
/**
* Cancellation return callback.
*
* @return Payment
*
* @return NULL
*/
function payment_postfinance_return_cancel(Payment $payment) {
// $payment->setStatus(new PaymentStatusItem(PAYMENT_STATUS_CANCELLED));
// $payment->finish();
payment_postfinance_redirect_form_back($payment);
}
/**
* Access callback for the cancellation return URL.
*
* @param Payment $payment
* The Payment to check access to.
* @param string $hash
* The hash based on $payment->pid.
*
* @return bool
*/
function payment_postfinance_return_cancel_access(Payment $payment, $hash) {
return PostfinancePaymentMethodController::hashPID($payment->pid) == $hash;
}
/**
* Payment method callback: redirect form return validation.
*
* @param $order
* The order object.
* @param $payment_method
* The payment method to be used.
*
* @return
* a boolean if the form is valid.
*/
function payment_postfinance_form_redirect_validate($order, $payment_method) {
// TODO: Actually do what we can based on the POST information to validate
// this was a successful payment (although actual transaction completion will
// depend on the IPN).
return TRUE;
}
/**
* Builds a Website Payments Standard form from an order object.
*
* @param $order
* The fully loaded order being paid for.
* @param $settings
* An array of settings used to build out the form, including:
*
* @return
* A renderable form array.
*/
function payment_postfinance_order_form(array $form, array &$form_state, Payment $payment, $settings) {
//XXX function payment_postfinance_order_form($form, &$form_state, $order, $settings) {
//XXX $wrapper = entity_metadata_wrapper('commerce_order', $order);
global $language;
if($language->language==='de'){
$lang='de_DE';
}
elseif($language->language==='fr'){
$lang='fr_FR';
}
elseif($language->language==='it'){
$lang='it_IT';
}
elseif($language->language==='es'){
$lang='es_ES';
}
elseif($language->language==='en'){
$lang='en_US';
}
else {
$lang=$settings['language'];
}
$title = trim($settings['title']);
if(empty($title)) {
$title = variable_get('site_name');
}
$logo = trim($settings['logo']);
if(!empty($logo)) {
$logo = 'https://e-payment.postfinance.ch/images/merchant/' . $settings['pspid'] . '/' . $logo;
}
$oid = $payment->context_data['oid'];
$order = node_load($oid);
//dpm($order);
// Price.
$price = minishop_get_total_price();
// Lastname and firstname
// LastName
$field = field_get_items('node', $order, 'field_lastname');
$lastname = isset($field[0]['value']) ? check_plain($field[0]['value']) : '';
// FirstName
$field = field_get_items('node', $order, 'field_firstname');
$firstname = isset($field[0]['value']) ? check_plain($field[0]['value']) : '';
$fullname = trim($lastname . ' ' . $firstname);
// Email
$email = field_get_items('node', $order, 'field_email');
$email = isset($email[0]['email']) ? check_plain($email[0]['email']) : '';
//Address
$address = field_get_items('node', $order, 'field_address');
//dpm($address);
// Build the data array that will be translated into hidden form values.
$data = array(
// The store's PSPID
'PSPID' => $settings['pspid'],
// Paymentmethod / Brand
'PM' => (!empty($settings['PM']) ? $settings['PM'] : ''),
'BRAND' => (!empty($settings['BRAND']) ? $settings['BRAND'] : ''),
// Use the timestamp to generate a unique invoice number ?
'orderID' => $settings['order_prefix'] . $oid, // . ' - ' . time(),
// Set the currency and language codes
'currency' => $settings['currency_code'],
'language' => $lang,
'amount' => number_format($price->total, 2, '.', '') * 100, // watch format 347.00 has to be sent as 37400
//setting order and user info
'COM' => t('Order number @order_number of the @store store.', array('@order_number' => $oid, '@store' => variable_get('site_name', url('<front>', array('absolute' => TRUE))))),
'CN' => removeAccents( $fullname ) ,
'owneraddress' => isset($address[0]['thoroughfare']) ? check_plain($address[0]['thoroughfare']) : '',
'ownerZIP' => isset($address[0]['postal_code']) ? check_plain($address[0]['postal_code']) : '',
'ownertown' => isset($address[0]['locality']) ? check_plain($address[0]['locality']) : '',
'ownercty' => isset($address[0]['country']) ? check_plain($address[0]['country']) : '',
'email' => $email,
//template page
'tp' => $settings['template_page'],
//additional parameter - paymentmethod
'COMPLUS' => $settings['payment_method'],
//setting urls
'homeurl' => $settings['home_url'],
'accepturl' => $settings['return'],
'backurl' => $settings['back_return'],
'cancelurl' => ( array_key_exists('cancel_uri', $settings) ? $settings['home_url'] . $settings['shop_uri'] : $settings['back_return'] ), /* backwards compatible, cancel_uri was introduced in 7.x-1.4 */
'declineurl' => $settings['back_return'],
'catalogurl' => $settings['home_url'] . $settings['shop_uri'],
//XXX
'BGCOLOR' => trim($settings['bgcolor']),
'TXTCOLOR' => trim($settings['txtcolor']),
'TITLE' => $title,
'LOGO' => $logo,
);
//TODO:
//$form['#attached']['js'][] = drupal_get_path('module', 'commerce_payment') . '/commerce_payment.js';
$form['help']['#markup'] = '<div class="checkout-help">' . t('Please wait while you are redirected to the payment server. If nothing happens within 10 seconds, please click on the button below.') . '</div>';
$form['help']['#markup'] = '<div class="checkout-help">' . t('Use the button below to proceed to the payment server.') . '</div>';
$form['#action'] = payment_postfinance_server_url($settings['server'], $settings['server_encoding']);
// order alphabetically as specified -- they already need to be in order in the form not only for the digest calc
$dataKeys = array_keys($data);
natcasesort($dataKeys);
foreach ($dataKeys as $name) {
$value = $data[$name];
if (!empty($value)) {
$form[ strtoupper($name)] = array('#type' => 'hidden', '#value' => $value);
}
}
// Add the digest.
$form['SHASign'] = array('#type' => 'hidden', '#value' => payment_postfinance_shaDigest($data, $settings, 'OUT'));
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Do the payment'),
);
return $form;
}
function removeAccents($str){
$a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
$b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
return str_replace($a, $b, $str);
}
/**
* Processes an incoming IPN.
*
* @param $uri
* dynamic URI
* @param $debug_ipn
* Optionally specify an IPN array for debug purposes; if left empty, the IPN
* be pulled from the $_POST.
*
*/
function payment_postfinance_process_ipn($uri = NULL, $debug_ipn = array()) {
$ret = 'IPN failed';
if (ipn_valid($uri, $debug_ipn)) {
$ret = 'IPN OK';
}
//drupal_set_header('Content-Type: text/plain'); function not avail - dont want to include - maybe use php function directly ..
print $ret;
exit(0); // hook_exit will not be invoked !
}
/**
* Processes an incoming IPN.
*
* @param $uri
* dynamic URI
* @param $debug_ipn
* Optionally specify an IPN array for debug purposes; if left empty, the IPN
* be pulled from the $_POST.
*
* @return
* TRUE or FALSE indicating whether the IPN was successfully processed or not.
*/
function ipn_valid($uri = NULL, $debug_ipn = array()) {
// Retrieve the IPN from $_POST if the caller did not supply an IPN array.
if (empty($debug_ipn)) {
$ipn = $_POST;
}
else {
$ipn = $debug_ipn;
}
if (!empty($ipn)) {
//Check if the Paymentmethod is in the return data
if (empty($ipn['COMPLUS'])) {
watchdog('payment_postfinance', 'COMPLUS (payment_method) not set for an IPN', array(), WATCHDOG_ERROR);
return FALSE;
}
if (empty($ipn['SHASIGN'])) {
watchdog('payment_postfinance', 'SHASIGN not set for an IPN', array(), WATCHDOG_ERROR);
return FALSE;
}
//TODO-XXX $payment_method = commerce_payment_method_instance_load($ipn['COMPLUS']);
$payment_method = entity_load('payment_method', $ipn['COMPLUS']);
if ($payment_method == FALSE) {
watchdog('payment_postfinance', 'payment_method could not be loaded (COMPLUS: @complus)', array('@complus' => $ipn['COMPLUS']), WATCHDOG_ERROR);
return FALSE;
}
$settings = $payment_method->controller_data;
if (!payment_postfinance_valid_sha_settings($settings)) {
watchdog('payment_postfinance', 'payment_method is not configured properly (@instance_id)', array('@instance_id' => $payment_method['instance_id']), WATCHDOG_ERROR);
return FALSE;
}
$orderid = $ipn['orderID'];
$shaDigest = payment_postfinance_shaDigest($ipn, $settings, 'IN');
if ($ipn['SHASIGN'] == $shaDigest) {
$rStatus = $ipn['STATUS'];
watchdog('payment_postfinance', 'received Callback for @oid / @uri / (@state) @statetext', array('@oid' => $orderid, '@uri' => print_r($uri, TRUE), '@state' => $rStatus, '@statetext' => payment_postfinance_IPNStatusText($rStatus)), WATCHDOG_INFO);
$oid = drupal_substr($orderid, drupal_strlen($settings['order_prefix']));
$order = node_load($oid);
if ($rStatus == 1) { //canceled by user
//XXX commerce_order_status_update($order, 'canceled');
}
// XXX
//payment_postfinance_transaction($payment_method, $order, $ipn);
// Invoke the hook here so implementations have access to the order and
// payment method if available and a saved IPN array that includes the payment
// transaction ID if created in the payment method's default process callback.
module_invoke_all('payment_postfinance_process_ipn', $order, $payment_method, $ipn);
return TRUE;
}
else {
watchdog('payment_postfinance', 'IPN callback with wrong sign: Order @oid not updated - calculated digest @dig', array('@oid' => $orderid, '@dig' => $shaDigest), WATCHDOG_WARNING);
foreach ($ipn as $name => $value) {
watchdog('payment_postfinance', 'received > @name: @val', array('@name' => $name, '@val' => $value), WATCHDOG_DEBUG);
}
}
}
return FALSE;
}
/**
* Save the payment transaction for the order to the DB.
*
* @param $payment_method
* The used payment method.
* @param $order
* The order object.
* @param $response
* The response param array.
* @param $transaction_status
* The status of the transaction.
*/
function payment_postfinance_transaction($payment_method, $order, $response) {
/*
$transaction = commerce_payment_transaction_new('commerce_postfinance', $order->order_id);
$transaction->status = commerce_postfinance_get_commerce_transaction_status($response['STATUS'], $response['NCERROR']);
$statusTxt = commerce_postfinance_IPNStatusText($response['STATUS']);
$ncErr = '';
if ($transaction->status == COMMERCE_PAYMENT_STATUS_FAILURE) {
if (!empty($response['NCERROR'])) {
$arrNC = commerce_postfinance_NCErrorText($response['NCERROR']);
$ncErr = $arrNC['nctext'];
watchdog('payment_postfinance', 'NCERROR:@nc: @msgplus', array('@nc' => $response['NCERROR'], '@msgplus' => $ncErr), WATCHDOG_WARNING);
}
else {
watchdog('payment_postfinance', '@status: @msg', array('@status' => $response['STATUS'], '@msg' => $statusTxt), WATCHDOG_WARNING);
}
}
$transaction->instance_id = $payment_method['instance_id'];
$transaction->amount = $order->commerce_order_total[LANGUAGE_NONE][0]['amount'];
$transaction->currency_code = $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code'];
$transaction->remote_id = $response['PAYID'];
$transaction->remote_status = $response['STATUS'] . ': ' . $statusTxt . ' (' . $response['NCERROR'] . ':' . $ncErr . ')' ;
if ($transaction->status == COMMERCE_PAYMENT_STATUS_SUCCESS) {
$transaction->message = '@orderid: ' . $statusTxt;
commerce_payment_redirect_pane_next_page($order);
}
elseif ($transaction->status == COMMERCE_PAYMENT_STATUS_FAILURE) {
$transaction->message = '@orderid: ' . $statusTxt;
commerce_payment_redirect_pane_previous_page($order);
}
$transaction->message_variables = array(
'@orderid' => $response['orderID'],
);
commerce_payment_transaction_save($transaction);
*/
}
/**
* Checks the settings array fot sha key and sha algorithm.
*
* @param array
* a payment method settings array.
*
* @return bool
* TRUE, if settings seem fine.
*/
function payment_postfinance_valid_sha_settings($settings) {
if (!is_array($settings) || empty($settings['payment_security']) || empty($settings['payment_security_key']) || empty($settings['reply_security_key'])) {
return FALSE;
}
if (!in_array($settings['payment_security'], array('sha1', 'sha256', 'sha512'))) {
return FALSE;
}
if (!strlen($settings['payment_security_key']) || !strlen($settings['reply_security_key'])) {
return FALSE;
}
return TRUE;
}
/**
* Returns the URL to the specified Postfinance server.
*
* @param $server
* Either test or prod indicating which server to get the URL for.
* @param $encoding
* ISO of UTF to determine the used Server URL
*
* @return
* The URL to use to submit requests to the Postfinance server.
*/
function payment_postfinance_server_url($server, $encoding) {
switch ($encoding) {
case 'iso':
return 'https://e-payment.postfinance.ch/ncol/' . $server . '/orderstandard.asp';
case 'utf':
return 'https://e-payment.postfinance.ch/ncol/' . $server . '/orderstandard_utf8.asp';
}
}
/**
* Calculates the Digest
*
* @param $data
* The data string to be digested.
* @param $settings
* The settings array.
* @param $shaAct
* The Action IN or OUT.
*
* @return
* The calculated digest in uppercase.
*/
function payment_postfinance_shaDigest($data, $settings, $shaAct) {
$shaDigest = '';
$shavalue = '';
$shaKey = $settings['payment_security_key'];
$shaParams = payment_postfinance_shaOUTparams();
if ($shaAct == 'IN') {
$shaKey = $settings['reply_security_key'];
$shaParams = payment_postfinance_shaINparams();
}
// order alphabetically as specified
$dataKeys = array_keys($data);
natcasesort($dataKeys);
foreach ($dataKeys as $name) {
$nameU = drupal_strtoupper($name);
$value = $data[$name];
if (drupal_strlen($value) > 0 && in_array($nameU, array_keys($shaParams))) {
$shavalue .= $nameU . '=' . $value . $shaKey;
}/*
else {
watchdog('payment_postfinance', 'IGN > @name: @val', array('@name' => $name, '@val' => $value), WATCHDOG_DEBUG);
}*/
}
$shaDigest = hash($settings['payment_security'], $shavalue);
/* watchdog('payment_postfinance', 'received > @name: @val', array('@name' => $shaDigest, '@val' => $shavalue), WATCHDOG_DEBUG); */
return drupal_strtoupper($shaDigest);
}
/**
* Returns an array of all possible language codes.
*
* @return
* an array of languages, transformed
*/
function payment_postfinance_default_languages() {
return drupal_map_assoc(array( 'de_DE', 'fr_FR', 'it_IT', 'es_ES', 'en_US'), 'payment_postfinance_transform_languages');
}
/**
* Transforms language codes user-readalbe
*
* @param $n
* a POSTFINANCE language string
*
* @return
* the stripped language code
*/
function payment_postfinance_transform_languages($n) {
return drupal_substr($n, -2);
}
/**
* Returns an array of all possible currency codes.
*
* @return
* an array of currency codes
*/
function payment_postfinance_default_currencies() {
return drupal_map_assoc(array('CHF', 'EUR'));
}