1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Payment request helper |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2020 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\Adyen |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
12
|
|
|
|
13
|
|
|
use Pronamic\WordPress\Pay\Payments\Payment; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Payment request helper |
17
|
|
|
* |
18
|
|
|
* @author Remco Tolsma |
19
|
|
|
* @version 1.1.1 |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class PaymentRequestHelper { |
23
|
|
|
/** |
24
|
|
|
* Complement WordPress Pay payment to Adyen payment request. |
25
|
|
|
* |
26
|
|
|
* @param Payment $payment WordPress Pay payment to convert. |
27
|
|
|
* @param AbstractPaymentRequest $request Adyen payment request. |
28
|
|
|
* @return void |
29
|
|
|
* @throws \Exception Throws exception on invalid metadata. |
30
|
|
|
*/ |
31
|
1 |
|
public static function complement( Payment $payment, AbstractPaymentRequest $request ) { |
32
|
|
|
// Channel. |
33
|
1 |
|
$request->set_channel( Channel::WEB ); |
34
|
|
|
|
35
|
|
|
// Shopper. |
36
|
1 |
|
$request->set_shopper_statement( $payment->get_description() ); |
37
|
|
|
|
38
|
|
|
// Customer. |
39
|
1 |
|
$customer = $payment->get_customer(); |
40
|
|
|
|
41
|
1 |
|
if ( null !== $customer ) { |
42
|
|
|
/* |
43
|
|
|
* When sending in the shopper reference we always create a recurring contract. If you would not |
44
|
|
|
* like to store the details, we recommend to exclude the shopper reference. |
45
|
|
|
* |
46
|
|
|
* $user_id = $customer->get_user_id(); |
47
|
|
|
* $request->set_shopper_reference( \is_null( $user_id ) ? null : \strval( $user_id ) ); |
48
|
|
|
*/ |
49
|
1 |
|
$request->set_shopper_ip( $customer->get_ip_address() ); |
50
|
1 |
|
$request->set_shopper_locale( $customer->get_locale() ); |
51
|
1 |
|
$request->set_telephone_number( $customer->get_phone() ); |
52
|
1 |
|
$request->set_shopper_email( $customer->get_email() ); |
53
|
|
|
|
54
|
|
|
// Shopper name. |
55
|
1 |
|
$name = $customer->get_name(); |
56
|
|
|
|
57
|
1 |
|
if ( null !== $name ) { |
58
|
|
|
$shopper_name = new Name( |
59
|
|
|
(string) $name->get_first_name(), |
60
|
|
|
(string) $name->get_last_name(), |
61
|
|
|
GenderTransformer::transform( $customer->get_gender() ) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$request->set_shopper_name( $shopper_name ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Date of birth. |
68
|
1 |
|
$request->set_date_of_birth( $customer->get_birth_date() ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Billing address. |
72
|
1 |
|
$billing_address = $payment->get_billing_address(); |
73
|
|
|
|
74
|
1 |
|
if ( null !== $billing_address ) { |
75
|
|
|
$address = AddressTransformer::transform( $billing_address ); |
76
|
|
|
|
77
|
|
|
$request->set_billing_address( $address ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Delivery address. |
81
|
1 |
|
$shipping_address = $payment->get_shipping_address(); |
82
|
|
|
|
83
|
1 |
|
if ( null !== $shipping_address ) { |
84
|
|
|
$address = AddressTransformer::transform( $shipping_address ); |
85
|
|
|
|
86
|
|
|
$request->set_delivery_address( $address ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Lines. |
90
|
1 |
|
$lines = $payment->get_lines(); |
91
|
|
|
|
92
|
1 |
|
if ( null !== $lines ) { |
93
|
|
|
$line_items = $request->new_line_items(); |
94
|
|
|
|
95
|
|
|
$i = 1; |
96
|
|
|
|
97
|
|
|
foreach ( $lines as $line ) { |
98
|
|
|
// Description. |
99
|
|
|
$description = $line->get_description(); |
100
|
|
|
|
101
|
|
|
// Use line item name as fallback for description. |
102
|
|
|
if ( null === $description ) { |
103
|
|
|
/* translators: %s: item index */ |
104
|
|
|
$description = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i++ ); |
105
|
|
|
|
106
|
|
|
if ( null !== $line->get_name() && '' !== $line->get_name() ) { |
107
|
|
|
$description = $line->get_name(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$item = $line_items->new_item( |
112
|
|
|
(string) $description, |
113
|
|
|
(int) $line->get_quantity(), |
114
|
|
|
$line->get_total_amount()->get_including_tax()->get_minor_units() |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$item->set_amount_excluding_tax( $line->get_total_amount()->get_excluding_tax()->get_minor_units() ); |
118
|
|
|
|
119
|
|
|
$item->set_id( $line->get_id() ); |
120
|
|
|
|
121
|
|
|
// Tax amount. |
122
|
|
|
$tax_amount = $line->get_total_amount()->get_tax_amount(); |
123
|
|
|
|
124
|
|
|
if ( null !== $tax_amount ) { |
125
|
|
|
$item->set_tax_amount( $tax_amount->get_minor_units() ); |
126
|
|
|
$item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 ); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Metadata. |
132
|
1 |
|
$metadata = array(); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Filters the Adyen checkout configuration. |
136
|
|
|
* |
137
|
|
|
* @param array $metadata Payment request metadata. |
138
|
|
|
* @since 1.1.1 |
139
|
|
|
*/ |
140
|
1 |
|
$metadata = apply_filters( 'pronamic_pay_adyen_payment_metadata', $metadata, $payment ); |
141
|
|
|
|
142
|
|
|
/* |
143
|
|
|
* Maximum 20 key-value pairs per request. When exceeding, the "177" error occurs: "Metadata size exceeds limit". |
144
|
|
|
* |
145
|
|
|
* @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/payments__reqParam_metadata |
146
|
|
|
* @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession__reqParam_metadata |
147
|
|
|
*/ |
148
|
1 |
|
if ( ! \is_array( $metadata ) ) { |
149
|
|
|
throw new \Exception( 'Adyen metadata must be an array.' ); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
if ( count( $metadata ) > 20 ) { |
153
|
|
|
throw new \Exception( 'Adyen metadata exceeds maximum of 20 items.' ); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
$request->set_metadata( $metadata ); |
157
|
1 |
|
} |
158
|
|
|
} |
159
|
|
|
|