forked from shopware5/SwagPaymentPaypal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.php
988 lines (916 loc) · 31.9 KB
/
Bootstrap.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
<?php
/*
* (c) shopware AG <info@shopware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Shopware_Plugins_Frontend_SwagPaymentPaypal_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
/**
* Installs the plugin
*
* @return bool
*/
public function install()
{
$this->createMyEvents();
$this->createMyMenu();
$this->createMyForm();
$this->createMyTranslations();
$this->fixOrderMail();
$this->createMyAttributes();
$this->fixPaymentLogo();
$this->createMyPayment();
return true;
}
/**
* @return bool
*/
public function uninstall()
{
$this->secureUninstall();
$this->removeMyAttributes();
return array('success' => true, 'invalidateCache' => array('config', 'backend', 'proxy', 'frontend'));
}
/**
* @return bool
*/
public function secureUninstall()
{
return true;
}
/**
* @param string $version
* @return bool|array
*/
public function update($version)
{
if ($version == '0.0.1') {
return false;
}
if (strpos($version, '2.0.') === 0) {
try {
$this->get('models')->removeAttribute(
's_user_attributes',
'swag_payal',
'billing_agreement_id'
);
} catch (Exception $e) {
}
try {
$this->get('models')->addAttribute(
's_order_attributes',
'swag_payal',
'billing_agreement_id',
'VARCHAR(255)'
);
} catch (Exception $e) {
}
$this->get('models')->generateAttributeModels(array('s_order_attributes', 's_user_attributes'));
$this->Form()->removeElement('paypalAllowGuestCheckout');
}
if (version_compare($version, '2.1.5', '<=')) {
$this->fixOrderMail();
}
if (version_compare($version, '2.1.6', '<=')) {
$this->createMyAttributes();
}
if (version_compare($version, '3.0.0', '<=')) {
$this->Form()->removeElement('paypalPaymentActionPending');
}
if (version_compare($version, '3.1.0', '<=')) {
$this->createMyMenu();
}
if (version_compare($version, '3.1.0', '<=')) {
$sql = 'ALTER TABLE `s_order_attributes`
CHANGE `swag_payal_express` `swag_payal_express` INT( 11 ) NULL DEFAULT NULL';
$this->get('db')->exec($sql);
}
if (version_compare($version, '3.3.2', '<=')) {
//always remove unneeded settings
$em = $this->get('models');
$form = $this->Form();
$em->remove($form->getElement('paypalLogInApi'));
$em->remove($form->getElement('paypalSeamlessCheckout'));
$em->flush();
}
if (version_compare($version, '3.3.4', '<')) {
$this->fixPaymentLogo();
$this->fixPluginDescription();
}
//Update form
$this->createMyForm();
$this->createMyEvents();
return array(
'success' => true,
'invalidateCache' => array('config', 'backend', 'proxy', 'frontend')
);
}
/**
* @param string $name
* @return mixed
*/
public function get($name)
{
if (version_compare(Shopware::VERSION, '4.2.0', '<') && Shopware::VERSION != '___VERSION___') {
$name = ucfirst($name);
return $this->Application()->Bootstrap()->getResource($name);
}
return parent::get($name);
}
private function fixOrderMail()
{
// Make sure, that the additionadescription field is evaluated by smarty
$sql = <<<'EOD'
UPDATE `s_core_config_mails`
SET
content=REPLACE(content, '{$additional.payment.additionaldescription}', '{include file="string:`$additional.payment.additionaldescription`"}'),
contentHTML=REPLACE(contentHTML, '{$additional.payment.additionaldescription}', '{include file="string:`$additional.payment.additionaldescription`"}')
WHERE name='sORDER';
EOD;
$this->get('db')->query($sql);
}
private function fixPluginDescription()
{
$payment = $this->getPayment();
if ($payment === null) {
return;
}
$description = $payment->getAdditionalDescription();
$description = preg_replace('#<!-- PayPal Logo -->.+<!-- PayPal Logo -->#msi', '', $description);
$description = str_replace('<p>PayPal. <em>Sicherererer.</em></p>', '<br><br>', $description);
$payment->setAdditionalDescription($description);
$this->get('models')->flush($payment);
}
/**
* Check if paypal logo exists in "unsorted" album otherwise create it and remove old logo
*
*/
private function fixPaymentLogo()
{
$logo = 'paypal_logo.png';
$mediaPath = $this->Application()->DocPath() . 'media/image/' . $logo;
$mediaRepo = $this->get('models')->getRepository('Shopware\Models\Media\Media');
$image = $mediaRepo->findOneBy(array('name' => 'paypal_logo'));
if ($image) {
$this->get('models')->remove($image);
$this->get('models')->flush();
}
//Remove file if don't have it in media manager but we have it under media/image folder
if (file_exists($mediaPath)) {
unlink($mediaPath);
}
}
/**
* Fetches and returns paypal payment row instance.
*
* @return \Shopware\Models\Payment\Payment
*/
public function getPayment()
{
return $this->Payments()->findOneBy(
array('name' => 'paypal')
);
}
/**
* Activate the plugin paypal plugin.
* Sets the active flag in the payment row.
*
* @return bool
*/
public function enable()
{
$payment = $this->getPayment();
if ($payment !== null) {
$payment->setActive(true);
$this->get('models')->flush($payment);
}
return array(
'success' => true,
'invalidateCache' => array('config', 'backend', 'proxy', 'frontend')
);
}
/**
* Disable plugin method and sets the active flag in the payment row
*
* @return bool
*/
public function disable()
{
$payment = $this->getPayment();
if ($payment !== null) {
$payment->setActive(false);
$this->get('models')->flush($payment);
}
return array(
'success' => true,
'invalidateCache' => array('config', 'backend')
);
}
/**
* Creates and subscribe the events and hooks.
*/
private function createMyEvents()
{
$this->subscribeEvent(
'Enlight_Controller_Dispatcher_ControllerPath_Frontend_PaymentPaypal',
'onGetControllerPathFrontend'
);
$this->subscribeEvent(
'Enlight_Controller_Dispatcher_ControllerPath_Backend_PaymentPaypal',
'onGetControllerPathBackend'
);
$this->subscribeEvent(
'Enlight_Controller_Action_PostDispatch',
'onPostDispatch',
110
);
$this->subscribeEvent(
'Enlight_Bootstrap_InitResource_PaypalClient',
'onInitResourcePaypalClient'
);
$this->subscribeEvent(
'Enlight_Bootstrap_InitResource_PaypalRestClient',
'onInitResourcePaypalRestClient'
);
$this->subscribeEvent(
'Enlight_Bootstrap_InitResource_paypalCurrency',
'onInitResourceCurrency'
);
$this->subscribeEvent(
'Enlight_Controller_Action_PostDispatch_Backend_Index',
'onExtendBackendIndex'
);
$this->subscribeEvent(
'Theme_Compiler_Collect_Plugin_Less',
'addLessFiles'
);
}
/**
* Creates and save the payment row.
*/
private function createMyPayment()
{
$this->createPayment(
array(
'name' => 'paypal',
'description' => 'PayPal',
'action' => 'payment_paypal',
'active' => 0,
'position' => 0,
'additionalDescription' => 'Bezahlung per PayPal - einfach, schnell und sicher.'
)
);
}
/**
* Creates and stores a payment item.
*/
private function createMyMenu()
{
$parent = $this->Menu()->findOneBy(array('label' => 'Zahlungen'));
$this->createMenuItem(
array(
'label' => 'PayPal',
'controller' => 'PaymentPaypal',
'action' => 'Index',
'class' => 'paypal--icon',
'active' => 1,
'parent' => $parent
)
);
}
/**
* Creates and stores the payment config form.
*/
private function createMyForm()
{
$form = $this->Form();
// API settings
$form->setElement(
'text',
'paypalUsername',
array(
'label' => 'API-Benutzername',
'required' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'stripCharsRe' => ' '
)
);
$form->setElement(
'text',
'paypalPassword',
array(
'label' => 'API-Passwort',
'required' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'stripCharsRe' => ' '
)
);
$form->setElement(
'text',
'paypalSignature',
array(
'label' => 'API-Unterschrift',
'required' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'stripCharsRe' => ' '
)
);
$form->setElement(
'text',
'paypalVersion',
array(
'label' => 'API-Version',
'value' => '113.0',
'required' => true,
'readOnly' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'button',
'paypalButtonApi',
array(
'label' => '<strong>Jetzt API-Signatur erhalten</strong>',
'handler' => "function(btn) {
var sandbox = btn.up('panel').down('[elementName=paypalSandbox]').getValue();
if(sandbox) {
var link = 'https://www.sandbox.paypal.com/';
} else {
var link = 'https://www.paypal.com/';
}
link += 'de/cgi-bin/webscr?cmd=_get-api-signature&generic-flow=true';
window.open(link, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=540');
}"
)
);
$form->setElement(
'text',
'paypalClientId',
array(
'label' => 'REST-API Client ID',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'stripCharsRe' => ' '
)
);
$form->setElement(
'text',
'paypalSecret',
array(
'label' => 'REST-API Secret',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'stripCharsRe' => ' '
)
);
$form->setElement(
'button',
'paypalButtonRestApi',
array(
'label' => '<strong>Jetzt Daten für REST-API erhalten</strong>',
'handler' => "function(btn) {
var link = document.location.pathname + 'paymentPaypal/downloadRestDocument';
window.open(link, '');
}"
)
);
$form->setElement(
'boolean',
'paypalSandbox',
array(
'label' => 'Sandbox-Modus aktivieren',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'number',
'paypalTimeout',
array(
'label' => 'API-Timeout in Sekunden',
'emptyText' => 'Default (60 Sekunden)',
'value' => null,
'scope' => 0
)
);
$form->setElement(
'boolean',
'paypalCurl',
array(
'label' => '<a href="http://php.net/manual/de/book.curl.php" target="_blank">Curl</a> verwenden (wenn es verfügbar ist)',
'value' => true
)
);
$form->setElement(
'select',
'paypalSslVersion',
array(
'label' => 'SSL-Version (<a href="http://curl.haxx.se/libcurl/c/CURLOPT_SSLVERSION.html" target="_blank">CURLOPT_SSLVERSION</a>)',
'value' => 0,
'store' => array(
array(0, 'Default (Keine Vorgabe)'),
array(1, 'TLSv1'),
array(4, 'TLSv1_0 (Available since PHP 5.5.19 and 5.6.3)'),
array(5, 'TLSv1_1 (Available since PHP 5.5.19 and 5.6.3)'),
array(6, 'TLSv1_2 (Available since PHP 5.5.19 and 5.6.3)'),
),
'description' => 'Funktioniert nur zusammen mit Curl',
)
);
if (is_file(__DIR__ . '/Views/backend/plugins/paypal/test.js')) {
$form->setElement(
'button',
'paypalButtonClientTest',
array(
'label' => '<strong>Jetzt API testen<strong>',
'handler' => "function(btn) {"
. file_get_contents(__DIR__ . '/Views/backend/plugins/paypal/test.js') . "}"
)
);
}
$form->setElement(
'boolean',
'paypalErrorMode',
array(
'label' => 'Fehlermeldungen ausgeben',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
// Payment page settings
$form->setElement(
'text',
'paypalBrandName',
array(
'label' => 'Alternativer Shop-Name auf der PayPal-Seite',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'text',
'paypalLocaleCode',
array(
'label' => 'Alternative Sprache (LocaleCode)',
'emptyText' => 'Beispiel: de_DE',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'media',
'paypalLogoImage',
array(
'label' => 'Shop-Logo auf der PayPal-Seite',
'value' => null,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'readOnly' => false,
)
);
$form->setElement(
'color',
'paypalCartBorderColor',
array(
'label' => 'Farbe des Warenkorbs auf der PayPal-Seite',
'value' => '#E1540F',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
// Frontend settings
$form->setElement(
'boolean',
'paypalFrontendLogo',
array(
'label' => 'Payment-Logo im Frontend ausgeben',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
// Payment settings
$form->setElement(
'select',
'paypalPaymentAction',
array(
'label' => 'Zahlungsabschluss',
'value' => 'Sale',
'store' => array(
array('Sale', 'Zahlung sofort abschließen (Sale)'),
array('Authorization', 'Zeitverzögerter Zahlungseinzug (Auth-Capture)'),
array('Order', 'Zeitverzögerter Zahlungseinzug (Order-Auth-Capture)')
),
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'boolean',
'paypalBillingAgreement',
array(
'label' => 'Zahlungsvereinbarung treffen / „Sofort-Kaufen“ aktivieren',
'description' => 'Achtung: Diese Funktion muss erst für Ihren PayPal-Account von PayPal aktiviert werden.',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
// $form->setElement('boolean', 'paypalLogIn', array(
// 'label' => '„Login mit PayPal“ aktivieren',
// 'description' => 'Achtung: Für diese Funktion müssen Sie erst die Daten für die REST-API hinterlegen.',
// 'value' => false,
// 'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
// ));
// $form->setElement('boolean', 'paypalFinishRegister', array(
// 'label' => 'Nach dem ersten „Login mit PayPal“ auf die Registrierung umleiten',
// 'value' => true,
// 'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
// ));
$form->setElement(
'boolean',
'paypalTransferCart',
array(
'label' => 'Warenkorb an PayPal übertragen',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'boolean',
'paypalExpressButton',
array(
'label' => '„Direkt zu PayPal Button“ im Warenkorb anzeigen',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'boolean',
'paypalExpressButtonLayer',
array(
'label' => '„Direkt zu PayPal Button“ in der Modal-Box anzeigen',
'value' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'select',
'paypalStatusId',
array(
'label' => 'Zahlungsstatus nach der kompletten Zahlung',
'value' => 12,
'store' => 'base.PaymentStatus',
'displayField' => 'description',
'valueField' => 'id',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'select',
'paypalPendingStatusId',
array(
'label' => 'Zahlungsstatus nach der Autorisierung',
'value' => 18,
'store' => 'base.PaymentStatus',
'displayField' => 'description',
'valueField' => 'id',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'boolean',
'paypalSendInvoiceId',
array(
'label' => 'Bestellnummer an PayPal übertragen',
'description' => 'Ist ggf. für einige Warenwirtschaften erforderlich. Stellen Sie in diesem Fall sicher, dass ihr Nummernkreis für Bestellnummern sich nicht mit anderen/vorherigen Shops überschneidet, die Sie ebenfalls über ihren PayPal-Account betreiben.',
'value' => false,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
)
);
$form->setElement(
'text',
'paypalPrefixInvoiceId',
array(
'label' => 'Bestellnummer für PayPal mit einem Shop-Prefix versehen',
'description' => 'Wenn Sie Ihren PayPal-Account für mehrere Shops nutzen, können Sie vermeiden, dass es Überschneidungen bei den Bestellnummern gibt, indem Sie hier ein eindeutiges Prefix definieren.',
'value' => 'MeinShop_',
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'vtype' => 'alphanum'
)
);
//Currencies
$currenciesArray = array();
$currencyRepository = Shopware()->Models()->getRepository('\Shopware\Models\Shop\Currency');
$currencies = $currencyRepository->findAll();
foreach ($currencies as $currency) {
$currenciesArray[] = array($currency->getId(), $currency->getName());
}
$form->setElement(
'select',
'paypalCurrency',
array(
'label' => 'Convert to currency',
'description' => 'Convert prices to selected one and send to paypal. This is used when paypal do not support used shopware currency.',
'store' => $currenciesArray,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP,
'required' => false,
'multiSelect' => false
)
);
}
private function createMyTranslations()
{
$form = $this->Form();
$translations = array(
'en_GB' => array(
'paypalUsername' => 'API username',
'paypalPassword' => 'API password',
'paypalSignature' => 'API signature',
'paypalVersion' => 'API version',
'paypalSandbox' => 'Activate sandbox mode',
'paypalBrandName' => 'Alternative shop name on PayPal\'s site',
'paypalLogoImage' => 'Shop image for PayPal',
'paypalCartBorderColor' => 'Color of the basket on PayPal',
'paypalFrontendLogo' => 'Show payment logo on frontend',
'paypalBillingAgreement' => 'Billing agreement / Activate "Buy it now"',
'paypalTransferCart' => 'Transfer basket to PayPal',
'paypalExpressButton' => 'Show express-purchase button in basket',
'paypalExpressButtonLayer' => 'Show express-purchase button in modal box',
'paypalStatusId' => 'Payment state after completing the payment',
'paypalPendingStatusId' => 'Payment state after being authorized',
'paypalSendInvoiceId' => 'Transfer invoice id to paypal',
'paypalPrefixInvoiceId' => 'Add shop prefix to the invoice id',
'paypalCurrency' => 'Convert to currency'
)
);
$shopRepository = Shopware()->Models()->getRepository('\Shopware\Models\Shop\Locale');
foreach ($translations as $locale => $snippets) {
$localeModel = $shopRepository->findOneBy(array('locale' => $locale));
foreach ($snippets as $element => $snippet) {
if ($localeModel === null) {
continue;
}
$elementModel = $form->getElement($element);
if ($elementModel === null) {
continue;
}
$translationModel = new \Shopware\Models\Config\ElementTranslation();
$translationModel->setLabel($snippet);
$translationModel->setLocale($localeModel);
$elementModel->addTranslation($translationModel);
}
}
}
private function createMyAttributes()
{
try {
$this->get('models')->addAttribute(
's_order_attributes',
'swag_payal',
'billing_agreement_id',
'VARCHAR(255)'
);
} catch (Exception $e) {
}
try {
$this->get('models')->addAttribute('s_order_attributes', 'swag_payal', 'express', 'int(11)');
} catch (Exception $e) {
}
$this->get('models')->generateAttributeModels(array('s_order_attributes'));
}
private function removeMyAttributes()
{
/** @var $modelManager \Shopware\Components\Model\ModelManager */
$modelManager = $this->get('models');
try {
$modelManager->removeAttribute('s_order_attributes', 'swag_payal', 'billing_agreement_id');
$modelManager->removeAttribute('s_order_attributes', 'swag_payal', 'express');
$modelManager->generateAttributeModels(array('s_order_attributes'));
} catch (Exception $e) {
}
}
/**
* @param bool $responsive
*/
public function registerMyTemplateDir($responsive = false)
{
if ($responsive) {
$this->get('template')->addTemplateDir(__DIR__ . '/Views/responsive/', 'paypal_responsive');
}
$this->get('template')->addTemplateDir(__DIR__ . '/Views/', 'paypal');
}
/**
* Returns the path to a frontend controller for an event.
*
* @return string
*/
public function onGetControllerPathFrontend()
{
$this->registerMyTemplateDir();
return __DIR__ . '/Controllers/Frontend/PaymentPaypal.php';
}
/**
* Returns the path to a backend controller for an event.
*
* @return string
*/
public function onGetControllerPathBackend()
{
$this->registerMyTemplateDir();
$this->Application()->Snippets()->addConfigDir(__DIR__ . '/Snippets/');
return __DIR__ . '/Controllers/Backend/PaymentPaypal.php';
}
/**
* @param Enlight_Event_EventArgs $args
*/
public function onPostDispatch(Enlight_Event_EventArgs $args)
{
static $subscriber;
if (!isset($subscriber)) {
require_once __DIR__ . '/Subscriber/Frontend.php';
$subscriber = new \Shopware\SwagPaymentPaypal\Subscriber\Frontend($this);
}
$subscriber->onPostDispatch($args);
}
/**
* @param $args
*/
public function onExtendBackendIndex($args)
{
static $subscriber;
if (!isset($subscriber)) {
require_once __DIR__ . '/Subscriber/BackendIndex.php';
$subscriber = new \Shopware\SwagPaymentPaypal\Subscriber\BackendIndex($this);
}
$subscriber->onPostDispatchBackendIndex($args);
}
/**
* Provide the file collection for less
*
* @param Enlight_Event_EventArgs $args
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function addLessFiles(Enlight_Event_EventArgs $args)
{
$less = new \Shopware\Components\Theme\LessDefinition(
array(),
array(__DIR__ . '/Views/responsive/frontend/_public/src/less/all.less'),
__DIR__
);
return new Doctrine\Common\Collections\ArrayCollection(array($less));
}
/**
* @param string $paymentStatus
* @return int
*/
public function getPaymentStatusId($paymentStatus)
{
switch ($paymentStatus) {
case 'Completed':
$paymentStatusId = $this->Config()->get('paypalStatusId', 12);
break;
case 'Pending':
case 'In-Progress':
$paymentStatusId = $this->Config()->get('paypalPendingStatusId', 18);
break; //Reserviert
case 'Processed':
$paymentStatusId = 18;
break; //In Bearbeitung > Reserviert
case 'Refunded':
$paymentStatusId = 20;
break; //Wiedergutschrift
case 'Partially-Refunded':
$paymentStatusId = 20;
break; //Wiedergutschrift
case 'Cancelled-Reversal':
$paymentStatusId = 12;
break;
case 'Expired':
case 'Denied':
case 'Voided':
$paymentStatusId = 17;
break; //Offen
case 'Reversed':
default:
$paymentStatusId = 21;
break;
}
return $paymentStatusId;
}
/**
* @param string $transactionId
* @param string $paymentStatus
* @param string|null $note
* @return void
*/
public function setPaymentStatus($transactionId, $paymentStatus, $note = null)
{
$paymentStatusId = $this->getPaymentStatusId($paymentStatus);
$sql = '
SELECT id FROM s_order WHERE transactionID=? AND status!=-1
';
$orderId = $this->get('db')->fetchOne($sql, array($transactionId));
$order = Shopware()->Modules()->Order();
$order->setPaymentStatus($orderId, $paymentStatusId, false, $note);
if ($paymentStatusId == 21) {
$sql = 'UPDATE s_order
SET internalcomment = CONCAT(internalcomment, :pStatus)
WHERE transactionID = :transactionId';
$this->get('db')->query(
$sql,
array('pStatus' => "\nPayPal Status: " . $paymentStatus, 'transactionId' => $transactionId)
);
}
if ($paymentStatus == 'Completed') {
$sql = '
UPDATE s_order SET cleareddate=NOW()
WHERE transactionID=?
AND cleareddate IS NULL LIMIT 1
';
$this->get('db')->query($sql, array($transactionId));
}
}
/**
* @param bool $short
* @return string
*/
public function getLocaleCode($short = false)
{
$locale = $this->Config()->get('paypalLocaleCode');
if (empty($locale)) {
$locale = $this->get('locale')->toString();
list($l, $c) = explode('_', $locale);
if ($short && $c !== null) {
$locale = $c;
} elseif ($l == 'de') {
$locale = 'de_DE';
}
}
return $locale;
}
/**
* @return string
*/
public function getLabel()
{
return 'PayPal';
}
/**
* Returns the version of plugin as string.
*
* @throws Exception
* @return string
*/
public function getVersion()
{
$info = json_decode(file_get_contents(__DIR__ . '/plugin.json'), true);
if ($info) {
return $info['currentVersion'];
} else {
throw new Exception('The plugin has an invalid version file.');
}
}
/**
* @return array
*/
public function getInfo()
{
return array(
'version' => $this->getVersion(),
'label' => $this->getLabel(),
'description' => file_get_contents(__DIR__ . '/info.txt')
);
}
/**
* Creates and returns the paypal client for an event.
*
* @return \Shopware_Components_Paypal_Client
*/
public function onInitResourcePaypalClient()
{
require_once __DIR__ . '/Components/Paypal/RestClient.php';
require_once __DIR__ . '/Components/Paypal/Client.php';
$client = new Shopware_Components_Paypal_Client($this->Config());
return $client;
}
/**
* Creates and returns the paypal rest client for an event.
*
* @return \Shopware_Components_Paypal_Client
*/
public function onInitResourcePaypalRestClient()
{
require_once __DIR__ . '/Components/Paypal/RestClient.php';
$client = new Shopware_Components_Paypal_RestClient($this->Config());
return $client;
}
/**
* Creates and returns the currency converter component.
*
* @return \Shopware_Components_Currency
*/
public function onInitResourceCurrency()
{
require_once __DIR__ . '/Components/Currency.php';
$currency = new Shopware_Components_Paypal_Currency($this->Config());
return $currency;
}
/**
* @return bool
*/
public function isShopware51()
{
return $this->assertMinimumVersion("5.1.0");
}
}