-
Notifications
You must be signed in to change notification settings - Fork 3
/
gateway-buckaroo-klarna.php
195 lines (162 loc) · 5.88 KB
/
gateway-buckaroo-klarna.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
require_once __DIR__ . '/library/api/paymentmethods/klarna/klarna.php';
/**
* @package Buckaroo
*/
class WC_Gateway_Buckaroo_Klarna extends WC_Gateway_Buckaroo {
const PAYMENT_CLASS = BuckarooKlarna::class;
protected $type;
protected $vattype;
protected $klarnaPaymentFlowId = '';
public function __construct() {
$this->has_fields = true;
$this->type = 'klarna';
$this->setIcon( '24x24/klarna.svg', 'svg/klarna.svg' );
$this->setCountry();
parent::__construct();
$this->addRefundSupport();
}
/** @inheritDoc */
protected function setProperties() {
parent::setProperties();
$this->vattype = $this->get_option( 'vattype' );
}
public function getKlarnaSelector() {
return str_replace( '_', '-', $this->id );
}
public function getKlarnaPaymentFlow() {
return $this->klarnaPaymentFlowId;
}
/**
* Can the order be refunded
*
* @param integer $order_id
* @param integer $amount defaults to null
* @param string $reason
* @return callable|string function or error
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
return $this->processDefaultRefund( $order_id, $amount, $reason );
}
/**
* Validate payment fields on the frontend.
*
* @access public
* @return void
*/
public function validate_fields() {
$gender = $this->request( $this->getKlarnaSelector() . '-gender' );
if ( ! in_array( $gender, array( 'male', 'female' ) ) ) {
wc_add_notice( __( 'Unknown gender', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
if ( $this->request( 'ship_to_different_address' ) !== null ) {
$countryCode = $this->request( 'shipping_country' ) == 'NL' ? $this->request( 'shipping_country' ) : '';
$countryCode = $this->request( 'billing_country' ) == 'NL' ? $this->request( 'billing_country' ) : $countryCode;
if ( ! empty( $countryCode )
&& strtolower( $this->klarnaPaymentFlowId ) !== 'pay' ) {
return wc_add_notice( __( 'Payment method is not supported for country ' . '(' . esc_html( $countryCode ) . ')', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
} elseif (
( $this->request( 'billing_country' ) == 'NL' )
&& strtolower( $this->klarnaPaymentFlowId ) !== 'pay'
) {
return wc_add_notice( __( 'Payment method is not supported for country ' . '(' . esc_html( $this->request( 'billing_country' ) ) . ')', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
}
/**
* Process payment
*
* @param integer $order_id
* @return callable|void fn_buckaroo_process_response() or void
*/
public function process_payment( $order_id ) {
$this->setOrderCapture( $order_id, 'Klarna' );
$order = getWCOrder( $order_id );
/** @var BuckarooKlarna */
$klarna = $this->createDebitRequest( $order );
$klarna->setType( $this->type );
$klarna->invoiceId = (string) getUniqInvoiceId(
preg_replace( '/\./', '-', $order->get_order_number() )
);
$order_details = new Buckaroo_Order_Details( $order );
$klarna = $this->get_billing_info( $order_details, $klarna );
$klarna = $this->get_shipping_info( $order_details, $klarna );
$klarna = $this->handleThirdPartyShippings( $klarna, $order, $this->country );
$klarna->CustomerIPAddress = getClientIpBuckaroo();
$klarna->Accept = 'TRUE';
$klarna->returnUrl = $this->notify_url;
$klarna->setPaymentFlow( $this->getKlarnaPaymentFlow() );
$response = $klarna->paymentAction(
$this->get_products_for_payment( $order_details )
);
return fn_buckaroo_process_response( $this, $response, $this->mode );
}
/**
* Get billing info for pay request
*
* @param Buckaroo_Order_Details $order_details
* @param BuckarooKlarna $method
* @param string $birthdate
*
* @return BuckarooKlarna $method
*/
protected function get_billing_info( $order_details, $method ) {
/** @var BuckarooKlarna */
$method = $this->set_billing( $method, $order_details );
$method->BillingGender = $this->request( $this->getKlarnaSelector() . '-gender' ) ?? 'Unknown';
$method->BillingFirstName = $order_details->getBilling( 'first_name' );
if ( empty( $method->BillingPhoneNumber ) ) {
$method->BillingPhoneNumber = $this->request( $this->getKlarnaSelector() . '-phone' );
}
$billingCompany = $order_details->getBilling( 'company' );
$method->setBillingCategory( $billingCompany );
$method->setShippingCategory( $billingCompany );
return $method;
}
/**
* Get shipping info for pay request
*
* @param Buckaroo_Order_Details $order_details
* @param BuckarooKlarna $method
*
* @return BuckarooKlarna $method
*/
protected function get_shipping_info( $order_details, $method ) {
$method->AddressesDiffer = 'FALSE';
if ( $this->request( $this->getKlarnaSelector() . '-shipping-differ' ) ) {
$method->AddressesDiffer = 'TRUE';
$shippingCompany = $order_details->getShipping( 'company' );
$method->setShippingCategory( $shippingCompany );
/** @var BuckarooKlarna */
$method = $this->set_shipping( $method, $order_details );
$method->ShippingFirstName = $order_details->getShipping( 'first_name' );
}
return $method;
}
public function getProductImage( $product ) {
$imgTag = $product->get_image();
$doc = new DOMDocument();
$doc->loadHTML( $imgTag );
$xpath = new DOMXPath( $doc );
$imageUrl = $xpath->evaluate( 'string(//img/@src)' );
return $imageUrl;
}
public function get_product_data( Buckaroo_Order_Item $order_item ) {
$product = parent::get_product_data( $order_item );
if ( $order_item->get_type() === 'line_item' ) {
$img = $this->getProductImage( $order_item->get_order_item()->get_product() );
if ( ! empty( $img ) ) {
$product['imgUrl'] = $img;
}
$product['url'] = get_permalink( $order_item->get_id() );
}
return $product;
}
/** @inheritDoc */
public function init_form_fields() {
parent::init_form_fields();
if ( $this->id !== 'buckaroo_klarnapii' ) {
$this->add_financial_warning_field();
}
}
}