Passed
Push — master ( d4ad7c...cc3467 )
by Reüel
07:39 queued 12s
created

PaymentRequestHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 97
ccs 18
cts 45
cp 0.4
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B complement() 0 89 11
1
<?php
2
/**
3
 * Payment request helper
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 helper
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.5
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
	 */
30 1
	public static function complement( Payment $payment, AbstractPaymentRequest $request ) {
31
		// Channel.
32 1
		$request->set_channel( Channel::WEB );
33
34
		// Shopper.
35 1
		$request->set_shopper_statement( $payment->get_description() );
36
37
		// Customer.
38 1
		$customer = $payment->get_customer();
39
40 1
		if ( null !== $customer ) {
41 1
			$request->set_shopper_ip( $customer->get_ip_address() );
42 1
			$request->set_shopper_locale( $customer->get_locale() );
43 1
			$request->set_telephone_number( $customer->get_phone() );
44 1
			$request->set_shopper_email( $customer->get_email() );
45
46
			// Shopper name.
47 1
			$name = $customer->get_name();
48
49 1
			if ( null !== $name ) {
50
				$shopper_name = new Name(
51
					strval( $name->get_first_name() ),
52
					strval( $name->get_last_name() ),
53
					GenderTransformer::transform( $customer->get_gender() )
54
				);
55
56
				$request->set_shopper_name( $shopper_name );
57
			}
58
59
			// Date of birth.
60 1
			$request->set_date_of_birth( $customer->get_birth_date() );
61
		}
62
63
		// Billing address.
64 1
		$billing_address = $payment->get_billing_address();
65
66 1
		if ( null !== $billing_address ) {
67
			$address = AddressTransformer::transform( $billing_address );
68
69
			$request->set_billing_address( $address );
70
		}
71
72
		// Delivery address.
73 1
		$shipping_address = $payment->get_shipping_address();
74
75 1
		if ( null !== $shipping_address ) {
76
			$address = AddressTransformer::transform( $shipping_address );
77
78
			$request->set_delivery_address( $address );
79
		}
80
81
		// Lines.
82 1
		$lines = $payment->get_lines();
83
84 1
		if ( null !== $lines ) {
85
			$line_items = $request->new_line_items();
86
87
			$i = 1;
88
89
			foreach ( $lines as $line ) {
90
				// Description.
91
				$description = $line->get_description();
92
93
				// Use line item name as fallback for description.
94
				if ( null === $description ) {
95
					/* translators: %s: item index */
96
					$description = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i++ );
97
98
					if ( null !== $line->get_name() && '' !== $line->get_name() ) {
99
						$description = $line->get_name();
100
					}
101
				}
102
103
				$item = $line_items->new_item(
104
					(string) $description,
105
					(int) $line->get_quantity(),
106
					$line->get_total_amount()->get_including_tax()->get_minor_units()
107
				);
108
109
				$item->set_amount_excluding_tax( $line->get_total_amount()->get_excluding_tax()->get_minor_units() );
110
111
				$item->set_id( $line->get_id() );
112
113
				// Tax amount.
114
				$tax_amount = $line->get_total_amount()->get_tax_amount();
115
116
				if ( null !== $tax_amount ) {
117
					$item->set_tax_amount( $tax_amount->get_minor_units() );
118
					$item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 );
119
				}
120
			}
121
		}
122 1
	}
123
}
124