1 | <?php |
||
8 | |||
9 | class Create implements MethodInterface |
||
10 | { |
||
11 | const CARD = 'card'; |
||
12 | const RECURRING = 'recurring'; |
||
13 | |||
14 | private $attributes; |
||
15 | |||
16 | |||
17 | public function __construct(array $attributes) |
||
18 | { |
||
19 | $this->attributes = $attributes; |
||
20 | } |
||
21 | |||
22 | public function getAction() |
||
23 | { |
||
24 | return 'payments'; |
||
25 | } |
||
26 | |||
27 | public function getMethod() |
||
28 | { |
||
29 | return MethodInterface::POST; |
||
30 | } |
||
31 | |||
32 | public function getAttributes() |
||
33 | { |
||
34 | return $this->attributes; |
||
35 | } |
||
36 | |||
37 | public function createResultObject() |
||
38 | { |
||
39 | return new Payment(); |
||
40 | } |
||
41 | |||
42 | public function getValidationConstraints() |
||
43 | { |
||
44 | return new Assert\Collection([ |
||
45 | 'amount' => $this->buildElement('float', 1), |
||
46 | 'currency' => $this->buildElement('string', 1, ['min' => 3,'max' => 3]), |
||
47 | 'settle' => $this->buildElement('bool'), |
||
48 | 'order_id' => $this->buildElement('string', 0, ['min' => 2,'max' => 50]), |
||
49 | 'description' => $this->buildElement('string', 0, ['max' => 255]), |
||
50 | 'country' => $this->buildElement('string', 1, ['min' => 2,'max' => 2]), |
||
51 | 'payment_method' => new Assert\Required([ |
||
52 | new Assert\Type([ |
||
53 | 'type' => 'string', |
||
54 | 'message' => 'The value {{ value }} is not a valid {{ type }}.' |
||
55 | ]), |
||
56 | new Assert\Choice([ |
||
57 | 'choices' => [ |
||
58 | self::CARD, |
||
59 | self::RECURRING |
||
60 | ] |
||
61 | ]) |
||
62 | ]), |
||
63 | 'payment_instrument' => $this->getPaymentInstrumentConstraints( |
||
64 | $this->getAttributes()['payment_method'] |
||
65 | ), |
||
66 | 'threeds2_data' => new Assert\Optional( |
||
67 | $this->getThreeDS2DataConstraints() |
||
68 | ) |
||
69 | ]); |
||
70 | } |
||
71 | |||
72 | private function getPaymentInstrumentConstraints($method) |
||
73 | { |
||
74 | switch ($method) { |
||
75 | case self::CARD: |
||
76 | return new Assert\Collection([ |
||
77 | 'pan' => new Assert\Required([ |
||
78 | new Assert\NotBlank(), |
||
79 | new Assert\Luhn() |
||
80 | ]), |
||
81 | 'exp_year' => $this->buildElement( |
||
82 | 'integer', 1, |
||
83 | ['min' => 4,'max' => 4], |
||
84 | new Assert\Range(['min' => date('Y')]), |
||
85 | ), |
||
|
|||
86 | 'exp_month' => $this->buildElement('integer', 1), |
||
87 | 'cvc' => $this->buildElement('string', 1, ['min' => 3, 'max' => 4]), |
||
88 | 'holder' => $this->buildElement('string', 1, ['max' => 32]), |
||
89 | ]); |
||
90 | case self::RECURRING: |
||
91 | return new Assert\Collection([ |
||
92 | 'payment_id' => $this->buildElement('string', 1), |
||
93 | ]); |
||
94 | } |
||
95 | |||
96 | throw new \InvalidArgumentException( |
||
97 | sprintf( |
||
98 | 'Payment instrument for payment method "%s" is not expected', |
||
99 | $method |
||
100 | ) |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | private function getThreeDS2DataConstraints() |
||
105 | { |
||
106 | return new Assert\Collection([ |
||
107 | 'notification_url' => $this->buildElement('string', 1), |
||
108 | 'browser_info' => $this->getBrowserInfoConstraints(), |
||
109 | 'billing_address' => new Assert\Optional( |
||
110 | $this->getAdressConstraints() |
||
111 | ), |
||
112 | 'delivery_address' => new Assert\Optional( |
||
113 | $this->getAdressConstraints() |
||
114 | ), |
||
115 | 'cardholder_info' => new Assert\Optional( |
||
116 | $this->getCardHolderInfoConstraints() |
||
117 | ), |
||
118 | ]); |
||
119 | } |
||
120 | |||
121 | private function getBrowserInfoConstraints() |
||
122 | { |
||
123 | return new Assert\Collection([ |
||
124 | 'accept_header' => $this->buildElement('string', 1), |
||
125 | 'browser_language' => $this->buildElement('string', 1), |
||
126 | 'screen_width' => $this->buildElement('integer', 1), |
||
127 | 'screen_height' => $this->buildElement('integer', 1), |
||
128 | 'challenge_window_size' => $this->buildElement('string', 1), |
||
129 | 'user_agent' => $this->buildElement('string', 1), |
||
130 | 'color_depth' => $this->buildElement('integer', 1), |
||
131 | 'time_zone' => $this->buildElement('integer', 1), |
||
132 | 'ip_address' => new Assert\Optional($this->buildElement('string')), |
||
133 | 'javascript_enabled' => new Assert\Optional($this->buildElement('bool')), |
||
134 | 'java_enabled' => new Assert\Optional($this->buildElement('bool')), |
||
135 | ]); |
||
136 | } |
||
137 | |||
138 | private function getAdressConstraints() |
||
139 | { |
||
140 | return new Assert\Collection([ |
||
141 | 'address_line1' => $this->buildElement('string', 1, ['max'=>50]), |
||
142 | 'address_line2' => new Assert\Optional( |
||
143 | $this->buildElement('string', 1, ['max'=>50]) |
||
144 | ), |
||
145 | 'address_line3' => new Assert\Optional( |
||
146 | $this->buildElement('string', 0, ['max'=>50]) |
||
147 | ), |
||
148 | 'city' => $this->buildElement('string', 1, ['max'=>50]), |
||
149 | 'country' => $this->buildElement('string', 1, ['max'=>10]), |
||
150 | 'postal_code' => $this->buildElement('string', 1, ['max'=>16]), |
||
187 |