|
1
|
|
|
<?php |
|
2
|
|
|
defined( 'ABSPATH' ) or die( 'This plugin must be run within the scope of WordPress.' ); |
|
3
|
|
|
|
|
4
|
|
|
require_once( __DIR__ . '/class-edu-sveawebpay-config.php' ); |
|
5
|
|
|
|
|
6
|
|
|
use Svea\WebPay\WebPay; |
|
7
|
|
|
use Svea\WebPay\WebPayItem; |
|
8
|
|
|
use Svea\WebPay\Config\ConfigurationService; |
|
9
|
|
|
use Svea\WebPay\Response\SveaResponse; |
|
10
|
|
|
|
|
11
|
|
|
if ( ! class_exists( 'EDU_SveaWebPay' ) ): |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* EDU_SveaWebPay integrates EduAdmin-WordPress plugin with SveaWebPay as payment gateway |
|
15
|
|
|
*/ |
|
16
|
|
|
class EDU_SveaWebPay extends EDU_Integration { |
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() { |
|
21
|
|
|
$this->id = 'eduadmin-sveawebpay'; |
|
22
|
|
|
$this->displayName = __( 'Svea Webpay (Checkout)', 'eduadmin-sveawebpay' ); |
|
23
|
|
|
$this->description = ''; |
|
24
|
|
|
|
|
25
|
|
|
$this->init_form_fields(); |
|
26
|
|
|
$this->init_settings(); |
|
27
|
|
|
|
|
28
|
|
|
add_action( 'eduadmin-checkpaymentplugins', array( $this, 'intercept_booking' ) ); |
|
29
|
|
|
|
|
30
|
|
|
add_action( 'eduadmin-processbooking', array( $this, 'process_booking' ) ); |
|
31
|
|
|
|
|
32
|
|
|
add_action( 'wp_loaded', array( $this, 'process_svearesponse' ) ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $bookingInfo EduAdminBookingInfo |
|
37
|
|
|
*/ |
|
38
|
|
|
public function intercept_booking( $bookingInfo = null ) { |
|
39
|
|
|
if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
if ( isset( $_POST['act'] ) && 'bookCourse' === $_POST['act'] ) { |
|
43
|
|
|
$bookingInfo->NoRedirect = true; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Initializes the settingsfields |
|
49
|
|
|
*/ |
|
50
|
|
|
public function init_form_fields() { |
|
51
|
|
|
$this->setting_fields = array( |
|
52
|
|
|
'enabled' => array( |
|
53
|
|
|
'title' => __( 'Enabled', 'eduadmin-sveawebpay' ), |
|
54
|
|
|
'type' => 'checkbox', |
|
55
|
|
|
'description' => __( 'Enables/Disables the integration with Svea WebPay', 'eduadmin-sveawebpay' ), |
|
56
|
|
|
'default' => 'no', |
|
57
|
|
|
), |
|
58
|
|
|
'testrun' => array( |
|
59
|
|
|
'title' => __( 'Sandbox mode', 'eduadmin-sveawebpay' ), |
|
60
|
|
|
'type' => 'checkbox', |
|
61
|
|
|
'description' => __( 'Activate sandbox mode', 'eduadmin-sveawebpay' ), |
|
62
|
|
|
'default' => 'no', |
|
63
|
|
|
), |
|
64
|
|
|
'merchant_key' => array( |
|
65
|
|
|
'title' => __( 'Merchant key', 'eduadmin-sveawebpay' ), |
|
66
|
|
|
'type' => 'text', |
|
67
|
|
|
'description' => __( 'Please enter your merchant key from Svea WebPay.', 'eduadmin-sveawebpay' ), |
|
68
|
|
|
'placeholder' => __( 'Merchant key', 'eduadmin-sveawebpay' ), |
|
69
|
|
|
), |
|
70
|
|
|
'merchant_secret' => array( |
|
71
|
|
|
'title' => __( 'Merchant secret', 'eduadmin-sveawebpay' ), |
|
72
|
|
|
'type' => 'password', |
|
73
|
|
|
'description' => __( 'Please enter your merchant secret from Svea WebPay', 'eduadmin-sveawebpay' ), |
|
74
|
|
|
'placeholder' => __( 'Merchant secret', 'eduadmin-sveawebpay' ), |
|
75
|
|
|
), |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
*/ |
|
82
|
|
|
public function process_svearesponse() { |
|
83
|
|
|
|
|
84
|
|
|
if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
if ( ( isset( $_REQUEST['edu-thankyou'] ) && isset( $_REQUEST['svea'] ) ) || ( isset( $_REQUEST['edu-cancel'] ) && isset( $_REQUEST['svea'] ) ) ) { |
|
88
|
|
|
|
|
89
|
|
|
$eclId = ( isset( $_REQUEST['edu-thankyou'] ) ? $_REQUEST['edu-thankyou'] : $_REQUEST['edu-cancel'] ); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$filter = new XFiltering(); |
|
92
|
|
|
$f = new XFilter( 'EventCustomerLnkID', '=', $eclId ); |
|
93
|
|
|
$filter->AddItem( $f ); |
|
94
|
|
|
|
|
95
|
|
|
$eventBooking = EDU()->api->GetEventBookingV2( EDU()->get_token(), '', $filter->ToString() )[0]; |
|
96
|
|
|
|
|
97
|
|
|
$filter = new XFiltering(); |
|
98
|
|
|
$f = new XFilter( 'CustomerID', '=', $eventBooking->CustomerID ); |
|
99
|
|
|
$filter->AddItem( $f ); |
|
100
|
|
|
|
|
101
|
|
|
$_customer = EDU()->api->GetCustomerV3( EDU()->get_token(), '', $filter->ToString(), false )[0]; |
|
102
|
|
|
|
|
103
|
|
|
$filter = new XFiltering(); |
|
104
|
|
|
$f = new XFilter( 'CustomerContactID', '=', $eventBooking->CustomerContactID ); |
|
105
|
|
|
$filter->AddItem( $f ); |
|
106
|
|
|
|
|
107
|
|
|
$_contact = EDU()->api->GetCustomerContactV2( EDU()->get_token(), '', $filter->ToString(), false )[0]; |
|
108
|
|
|
|
|
109
|
|
|
$ebi = new EduAdminBookingInfo( $eventBooking, $_customer, $_contact ); |
|
110
|
|
|
|
|
111
|
|
|
$countries = EDU()->api->GetCountries( EDU()->get_token(), 'Swedish' ); |
|
112
|
|
|
|
|
113
|
|
|
$selectedCountry = 'SE'; |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$invoiceCountry = $ebi->Customer->InvoiceCountry; |
|
116
|
|
|
if ( empty( $invoiceCountry ) ) { |
|
117
|
|
|
$invoiceCountry = $ebi->Customer->Country; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
foreach ( $countries as $country ) { |
|
121
|
|
|
if ( $invoiceCountry == $country->CountryName ) { |
|
122
|
|
|
$selectedCountry = $country->CountryCode; |
|
|
|
|
|
|
123
|
|
|
break; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
View Code Duplication |
if ( 'no' !== $this->get_option( 'testrun', 'no' ) ) { |
|
|
|
|
|
|
128
|
|
|
$wpConfig = new EduSveaWebPayTestConfig( $this ); |
|
129
|
|
|
} else { |
|
130
|
|
|
$wpConfig = new EduSveaWebPayProductionConfig( $this ); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
//$response = ( new SveaResponse( $_REQUEST, $selectedCountry, $wpConfig ) )->getResponse(); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$sveaOrderId = get_transient( 'eduadmin-sveaorderid-' . $ebi->EventBooking->EventCustomerLnkID, - 1 ); |
|
136
|
|
|
|
|
137
|
|
|
if ( $sveaOrderId > 0 ) { |
|
138
|
|
|
$wpOrder = WebPay::checkout( $wpConfig ); |
|
139
|
|
|
|
|
140
|
|
|
$wpOrder->setCheckoutOrderId( $sveaOrderId ); |
|
141
|
|
|
|
|
142
|
|
|
$order = $wpOrder->getOrder(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ( 'Cancelled' !== $order['Status'] ) { |
|
|
|
|
|
|
146
|
|
|
EDU()->api->SetValidPayment( EDU()->get_token(), $ebi->EventBooking->EventCustomerLnkID ); |
|
147
|
|
|
} else { |
|
148
|
|
|
EDU()->api->SetInvalidPayment( EDU()->get_token(), $ebi->EventBooking->EventCustomerLnkID ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$surl = get_home_url(); |
|
152
|
|
|
$cat = get_option( 'eduadmin-rewriteBaseUrl' ); |
|
153
|
|
|
$baseUrl = $surl . '/' . $cat; |
|
154
|
|
|
|
|
155
|
|
|
delete_transient( 'eduadmin-sveaorderid-' . $ebi->EventBooking->EventCustomerLnkID ); |
|
156
|
|
|
|
|
157
|
|
|
wp_redirect( $baseUrl . '/profile/myprofile?payment=' . ( 'Cancelled' !== $order['Status'] ? '1' : '0' ) ); |
|
158
|
|
|
exit(); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param $bookingInfo EduAdminBookingInfo |
|
164
|
|
|
*/ |
|
165
|
|
|
public function process_booking( $bookingInfo = null ) { |
|
166
|
|
|
if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
|
167
|
|
|
return; |
|
168
|
|
|
} |
|
169
|
|
|
if ( isset( $_POST['act'] ) && 'bookCourse' === $_POST['act'] ) { |
|
170
|
|
|
$bookingInfo->NoRedirect = true; |
|
171
|
|
|
|
|
172
|
|
|
$countries = EDU()->api->GetCountries( EDU()->get_token(), 'Swedish' ); |
|
173
|
|
|
|
|
174
|
|
|
$selectedCountry = 'SE'; |
|
175
|
|
|
$selectedLocale = 'sv-SE'; |
|
176
|
|
|
|
|
177
|
|
|
$invoiceCountry = $bookingInfo->Customer->InvoiceCountry; |
|
178
|
|
|
if ( empty( $invoiceCountry ) ) { |
|
179
|
|
|
$invoiceCountry = $bookingInfo->Customer->Country; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
foreach ( $countries as $country ) { |
|
183
|
|
|
if ( $invoiceCountry == $country->CountryName ) { |
|
184
|
|
|
$selectedCountry = $country->CountryCode; |
|
185
|
|
|
if ( ! empty( $country->CultureName ) ) { |
|
186
|
|
|
$selectedLocale = $country->CultureName; |
|
187
|
|
|
} |
|
188
|
|
|
break; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
//$selectedLocale = explode( '-', $selectedLocale )[0]; |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
$currency = get_option( 'eduadmin-currency', 'SEK' ); |
|
195
|
|
|
|
|
196
|
|
View Code Duplication |
if ( 'no' !== $this->get_option( 'testrun', 'no' ) ) { |
|
|
|
|
|
|
197
|
|
|
$wpConfig = new EduSveaWebPayTestConfig( $this ); |
|
198
|
|
|
} else { |
|
199
|
|
|
$wpConfig = new EduSveaWebPayProductionConfig( $this ); |
|
200
|
|
|
} |
|
201
|
|
|
#$wpOrder = WebPay::createOrder( $wpConfig ); |
|
|
|
|
|
|
202
|
|
|
$wpOrder = WebPay::checkout( $wpConfig ); |
|
203
|
|
|
|
|
204
|
|
|
$orderRow = WebPayItem::orderRow(); |
|
205
|
|
|
$orderRow->setName( $bookingInfo->EventBooking->EventDescription ); |
|
206
|
|
|
$orderRow->setQuantity( 1 ); |
|
207
|
|
|
|
|
208
|
|
|
$vatPercent = ( $bookingInfo->EventBooking->VatSum / $bookingInfo->EventBooking->TotalPriceExVat ) * 100; |
|
209
|
|
|
$orderRow->setVatPercent( $vatPercent ); |
|
210
|
|
|
$orderRow->setAmountIncVat( (float) $bookingInfo->EventBooking->TotalPriceIncVat ); |
|
211
|
|
|
|
|
212
|
|
|
$customer = WebPayItem::companyCustomer(); |
|
213
|
|
|
|
|
214
|
|
|
if ( ! empty( $bookingInfo->Customer->InvoiceName ) ) { |
|
215
|
|
|
$customer->setCompanyName( $bookingInfo->Customer->InvoiceName ); |
|
216
|
|
|
} else { |
|
217
|
|
|
$customer->setCompanyName( $bookingInfo->Customer->CustomerName ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if ( ! empty( $bookingInfo->Customer->InvoiceAddress1 ) ) { |
|
221
|
|
|
$customer->setStreetAddress( $bookingInfo->Customer->InvoiceAddress1 ); |
|
222
|
|
|
} else { |
|
223
|
|
|
$customer->setStreetAddress( $bookingInfo->Customer->Address1 ); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if ( ! empty( $bookingInfo->Customer->InvoiceZip ) ) { |
|
227
|
|
|
$customer->setZipCode( $bookingInfo->Customer->InvoiceZip ); |
|
228
|
|
|
} else { |
|
229
|
|
|
$customer->setZipCode( $bookingInfo->Customer->Zip ); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$zipPreset = WebPayItem::presetValue() |
|
233
|
|
|
->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::POSTAL_CODE ) |
|
234
|
|
|
->setValue( ! empty( $bookingInfo->Customer->InvoiceZip ) ? $bookingInfo->Customer->InvoiceZip : $bookingInfo->Customer->Zip ) |
|
235
|
|
|
->setIsReadonly( false ); |
|
236
|
|
|
$wpOrder->addPresetValue( $zipPreset ); |
|
237
|
|
|
|
|
238
|
|
|
if ( ! empty( $bookingInfo->Customer->InvoiceCity ) ) { |
|
239
|
|
|
$customer->setLocality( $bookingInfo->Customer->InvoiceCity ); |
|
240
|
|
|
} else { |
|
241
|
|
|
$customer->setLocality( $bookingInfo->Customer->City ); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if ( ! empty( $bookingInfo->Customer->Phone ) ) { |
|
245
|
|
|
$customer->setPhoneNumber( $bookingInfo->Customer->Phone ); |
|
246
|
|
|
$phonePreset = WebPayItem::presetValue() |
|
247
|
|
|
->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::PHONE_NUMBER ) |
|
248
|
|
|
->setValue( $bookingInfo->Customer->Phone ) |
|
249
|
|
|
->setIsReadonly( false ); |
|
250
|
|
|
$wpOrder->addPresetValue( $phonePreset ); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if ( ! empty( $bookingInfo->Customer->InvoiceEmail ) ) { |
|
254
|
|
|
$customer->setEmail( $bookingInfo->Customer->InvoiceEmail ); |
|
255
|
|
|
} else { |
|
256
|
|
|
$customer->setEmail( $bookingInfo->Customer->Email ); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$emailPreset = WebPayItem::presetValue() |
|
260
|
|
|
->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::EMAIL_ADDRESS ) |
|
261
|
|
|
->setValue( ! empty( $bookingInfo->Customer->InvoiceEmail ) ? $bookingInfo->Customer->InvoiceEmail : $bookingInfo->Customer->Email ) |
|
262
|
|
|
->setIsReadonly( false ); |
|
263
|
|
|
$wpOrder->addPresetValue( $emailPreset ); |
|
264
|
|
|
|
|
265
|
|
|
$customer->setIpAddress( EDU()->get_ip_adress() ); |
|
266
|
|
|
|
|
267
|
|
|
$surl = get_home_url(); |
|
268
|
|
|
$cat = get_option( 'eduadmin-rewriteBaseUrl' ); |
|
269
|
|
|
$baseUrl = $surl . '/' . $cat; |
|
270
|
|
|
|
|
271
|
|
|
$defaultThankYou = @get_page_link( get_option( 'eduadmin-thankYouPage', '/' ) ) . "?edu-thankyou=" . $bookingInfo->EventBooking->EventCustomerLnkID . '&svea=1&svea_order_id={checkout.order.uri}'; |
|
|
|
|
|
|
272
|
|
|
$defaultCancel = $baseUrl . "?edu-cancel=" . $bookingInfo->EventBooking->EventCustomerLnkID . '&svea=1&svea_order_id={checkout.order.uri}'; |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
$defaultPushUrl = $baseUrl . '?edu-thankyou=' . $bookingInfo->EventBooking->EventCustomerLnkID . '&svea=1&svea_order_id={checkout.order.uri}'; |
|
275
|
|
|
|
|
276
|
|
|
$defaultTermsUrl = get_option( 'eduadmin-bookingTermsLink' ); |
|
277
|
|
|
|
|
278
|
|
|
//$defaultCheckoutUrl = $surl . $_SERVER['REQUEST_URI']; |
|
|
|
|
|
|
279
|
|
|
|
|
280
|
|
|
$wpBuild = $wpOrder |
|
281
|
|
|
->setCurrency( $currency ) |
|
282
|
|
|
->setCountryCode( $selectedCountry ) |
|
283
|
|
|
//->setOrderDate( date( 'c' ) ) |
|
|
|
|
|
|
284
|
|
|
->setClientOrderNumber( $bookingInfo->EventBooking->EventCustomerLnkID ) |
|
285
|
|
|
->addOrderRow( $orderRow ) |
|
286
|
|
|
//->addCustomerDetails( $customer ) |
|
|
|
|
|
|
287
|
|
|
//->usePayPage() |
|
288
|
|
|
//->setPayPageLanguage( $selectedLocale ) |
|
|
|
|
|
|
289
|
|
|
->setLocale( $selectedLocale ) |
|
290
|
|
|
//->setReturnUrl( apply_filters( 'eduadmin-thankyou-url', $defaultThankYou ) ) |
|
|
|
|
|
|
291
|
|
|
//->setCancelUrl( apply_filters( 'eduadmin-cancel-url', $defaultCancel ) ); |
|
|
|
|
|
|
292
|
|
|
->setTermsUri( $defaultTermsUrl ) |
|
293
|
|
|
->setConfirmationUri( $defaultThankYou ) |
|
294
|
|
|
->setPushUri( $defaultPushUrl ) |
|
295
|
|
|
->setCheckoutUri( $defaultCancel ); // We have no "checkout"-url.. So we just cancel the booking instead. |
|
296
|
|
|
//$wpForm = $wpBuild->getPaymentUrl(); |
|
|
|
|
|
|
297
|
|
|
$wpForm = $wpBuild->createOrder(); |
|
298
|
|
|
|
|
299
|
|
|
set_transient( 'eduadmin-sveaorderid-' . $bookingInfo->EventBooking->EventCustomerLnkID, $wpForm['OrderId'], HOUR_IN_SECONDS ); |
|
300
|
|
|
|
|
301
|
|
|
if ( array_key_exists( 'Gui', $wpForm ) ) { |
|
302
|
|
|
echo $wpForm['Gui']['Snippet']; |
|
|
|
|
|
|
303
|
|
|
} |
|
304
|
|
|
/*if ( $wpForm->accepted ) { |
|
|
|
|
|
|
305
|
|
|
if ( 'no' === $this->get_option( 'testrun', 'no' ) ) { |
|
306
|
|
|
echo '<script type="text/javascript">location.href = "' . $wpForm->url . '";</script>'; |
|
307
|
|
|
} else { |
|
308
|
|
|
echo '<script type="text/javascript">location.href = "' . $wpForm->testurl . '";</script>'; |
|
309
|
|
|
} |
|
310
|
|
|
} else { |
|
311
|
|
|
add_filter( 'edu-booking-error', function( $errors ) use ( &$wpForm ) { |
|
312
|
|
|
$errors[] = $wpForm->errormessage; |
|
313
|
|
|
} ); |
|
314
|
|
|
}*/ |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
endif; |