Failed Conditions
Push — develop ( ef0a62...79532f )
by Remco
03:16
created

src/PaymentRequestTransformer.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Payment request transformer
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 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 transformer
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.0
20
 * @since   1.0.0
21
 */
22
class PaymentRequestTransformer {
23
	/**
24
	 * Transform 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 AbstractPaymentRequest
29
	 */
30
	public static function transform( Payment $payment, AbstractPaymentRequest $request ) {
31
		// Channel.
32
		$request->set_channel( Channel::WEB );
33
34
		// Shopper.
35
		$request->set_shopper_statement( $payment->get_description() );
36
37
		if ( null !== $payment->get_customer() ) {
38
			$customer = $payment->get_customer();
39
40
			$request->set_shopper_ip( $customer->get_ip_address() );
41
			$request->set_shopper_locale( $customer->get_locale() );
42
			$request->set_telephone_number( $customer->get_phone() );
43
44
			// Shopper name.
45
			if ( null !== $customer->get_name() ) {
46
				$shopper_name = new Name(
47
					$customer->get_name()->get_first_name(),
48
					$customer->get_name()->get_last_name(),
49
					GenderTransformer::transform( $customer->get_gender() )
50
				);
51
52
				$request->set_shopper_name( $shopper_name );
53
			}
54
55
			// Date of birth.
56
			if ( null !== $customer->get_birth_date() ) {
57
				$request->set_date_of_birth( $customer->get_birth_date()->format( 'YYYY-MM-DD' ) );
0 ignored issues
show
$customer->get_birth_date()->format('YYYY-MM-DD') of type string is incompatible with the type DateTime|null expected by parameter $date_of_birth of Pronamic\WordPress\Pay\G...st::set_date_of_birth(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
				$request->set_date_of_birth( /** @scrutinizer ignore-type */ $customer->get_birth_date()->format( 'YYYY-MM-DD' ) );
Loading history...
58
			}
59
		}
60
61
		// Billing address.
62
		$billing_address = $payment->get_billing_address();
63
64
		if ( null !== $billing_address ) {
65
			$address = AddressTransformer::transform( $billing_address );
66
67
			$request->set_billing_address( $address );
68
		}
69
70
		// Delivery address.
71
		$shipping_address = $payment->get_shipping_address();
72
73
		if ( null !== $shipping_address ) {
74
			$address = AddressTransformer::transform( $shipping_address );
75
76
			$request->set_delivery_address( $address );
77
		}
78
79
		// Lines.
80
		$lines = $payment->get_lines();
81
82
		if ( null !== $lines ) {
83
			$line_items = $request->new_items();
84
85
			$i = 1;
86
87
			foreach ( $lines as $line ) {
88
				// Description.
89
				$description = $line->get_description();
90
91
				// Use line item name as fallback for description.
92
				if ( null === $description ) {
93
					/* translators: %s: item index */
94
					$description = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i ++ );
95
96
					if ( null !== $line->get_name() && '' !== $line->get_name() ) {
97
						$description = $line->get_name();
98
					}
99
				}
100
101
				$item = $line_items->new_item(
102
					$description,
103
					$line->get_quantity(),
104
					$line->get_total_amount()->get_including_tax()->get_minor_units()
105
				);
106
107
				$item->set_amount_excluding_tax( $line->get_total_amount()->get_excluding_tax()->get_minor_units() );
108
109
				$item->set_id( $line->get_id() );
110
111
				// Tax amount.
112
				$tax_amount = $line->get_unit_price()->get_tax_amount();
113
114
				if ( null !== $tax_amount ) {
115
					$item->set_tax_amount( $line->get_total_amount()->get_tax_amount()->get_minor_units() );
116
					$item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 );
117
				}
118
			}
119
		}
120
121
		// Return request.
122
		return $request;
123
	}
124
}
125