/
CreditCardExtension.php
90 lines (80 loc) · 2.75 KB
/
CreditCardExtension.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
<?php
/**
* This file is part of Stripe4
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\Stripe4\Form\Extension;
use Eccube\Entity\Order;
use Eccube\Form\Type\Shopping\OrderType;
use Eccube\Form\Type\ToggleSwitchType;
use Plugin\Stripe4\Form\Type\CreditCardType;
use Plugin\Stripe4\Service\Method\CreditCard;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
class CreditCardExtension extends AbstractTypeExtension
{
/**
* @var SessionInterface
*/
private $session;
public function __construct(
SessionInterface $session
)
{
$this->session = $session;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['skip_add_form']) {
return;
}
$builder
->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
/** @var FormInterface $form */
$form = $event->getForm();
/** @var Order $order */
$order = $event->getData();
if ($order->getPayment()->getMethodClass() === CreditCard::class) {
$form
->add('stripe_payment_method_id', HiddenType::class, [
'mapped' => true,
'constraints' => [
new NotBlank()
]
]);
if ($Customer = $order->getCustomer()) {
$form
->add('stripe_saving_card', ToggleSwitchType::class, [
'mapped' => true,
'label' => 'カード情報を保存する'
])
->add('cards', CreditCardType::class, [
'mapped' => false,
'expanded' => true,
'choices' => $order->getCustomer()->getCreditCards()
]);
}
}
});
}
/**
* @inheritDoc
*/
public function getExtendedType()
{
// TODO: Implement getExtendedType() method.
return OrderType::class;
}
}