This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bankID-registration.php
296 lines (238 loc) · 10.2 KB
/
bankID-registration.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/*
Plugin Name: Registration Bank Id
Version: 1.0
Author: Halo-lab
Author URI: https://halo-lab.com/
Description: Registration and authorization in WordPress using the API https://zignsec.com/
*/
// don't load directly
if ( !defined( 'ABSPATH' ) ) {
die( '-1' );
}
if ( is_admin() ) {
require_once plugin_dir_path(__FILE__) . 'admin/class-bankid-registration-admin.php';
}
$enable_test_api = get_option('bankid-registration_test_api');
$authorization_key = get_option('bankid-registration_key');
$api_version = 'v2';
if($enable_test_api){
$bankID_api_url = 'https://test.zignsec.com';
}else{
$bankID_api_url = 'https://api.zignsec.com/';
}
function custom_login_function() {
if (isset($_POST['submit']) || isset($_GET['submit'])) {
$personal_number = $_POST['personal_number'];
if(!empty($personal_number) ){
$personal_info = get_user_data($personal_number);
}
}
if(!$_POST['personal_number']){
get_login_form(
$_POST['personal_number'] ? $_POST['personal_number'] : ''
);
}
}
function custom_registration_function() {
if (isset($_POST['submit']) || isset($_GET['submit'])) {
$personal_number = $_POST['personal_number'];
if(!empty($personal_number) ){
$personal_info = get_user_data($personal_number);
}
}
if(!$_POST['personal_number']){
get_registration_form(
$_POST['personal_number'] ? $_POST['personal_number'] : ''
);
}
}
function get_login_form( $personal_number ) {
set_query_var( 'personal_number', $personal_number );
if ( file_exists( get_theme_file_path('bankID/login.php')) ) {
load_template(get_theme_file_path('bankID/login.php'));
}else{
load_template(dirname( __FILE__ ).'/templates/bankID/login.php');
}
}
function get_registration_form( $personal_number ) {
if ( is_user_logged_in() && !is_admin() ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
set_query_var( 'personal_number', $personal_number );
if ( file_exists( get_theme_file_path('bankID/registration.php')) ) {
load_template(get_theme_file_path('bankID/registration.php'));
}else{
load_template(dirname( __FILE__ ).'/templates/bankID/registration.php');
}
}
// Work if user is login
add_action( 'wp_ajax_get_user_data', 'get_user_data' );
// Work if user is not login
add_action("wp_ajax_nopriv_get_user_data", "get_user_data");
function get_user_data($personal_number){
$url = $bankID_api_url.'/'.$api_version.'/eid/sbid-another';
$args = array(
'headers' => array('Authorization' => $authorization_key),
'body' => array( 'CountryCode' => 'SE', 'IdentityNumber' => $personal_number , 'lookupPersonAddress' => 'true')
);
$response = wp_remote_post( $url, $args );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo $error_message;
} else {
$person = json_decode($response['body']);
authorization_method($person, $personal_number);
}
}
function authorization_method($url_redirect, $personal_number){
$tocen = autostart_tocen($personal_number);
set_query_var( 'tocen', $tocen );
set_query_var( 'personal_number', $personal_number );
set_query_var( 'url_redirect', $url_redirect->redirect_url );
set_query_var( 'redirect_id', $url_redirect->id );
if ( file_exists( get_theme_file_path('bankID/bankId-button.php')) ) {
load_template(get_theme_file_path('bankID/bankId-button.php'));
}else{
load_template(dirname( __FILE__ ).'/templates/bankID/bankId-button.php');
}
}
function autostart_tocen($personal_number){
$url = $bankID_api_url.'/'.$api_version.'/BankIDSE/Authenticate';
$args = array(
'headers' => array('Content-Type' => 'application/json; charset=UTF-8','Authorization' => $authorization_key,'PersonalNumber' => $personal_number),
'body' => "{'lookupPersonAddress':true}"
);
$response = wp_remote_post( $url, $args );
$response_decode = json_decode($response['body']);
return $response_decode;
}
// Work if user is login
add_action( 'wp_ajax_getProgressStatus', 'getProgressStatus' );
// Work if user is not login
add_action("wp_ajax_nopriv_getProgressStatus", "getProgressStatus");
function getProgressStatus(){
$orderRef = $_POST['orderRef'] ? $_POST['orderRef'] : "";
$url = $bankID_api_url.'/'.$api_version.'/BankIDSE/Collect';
$args = array(
'headers' => array('Content-Type' => 'application/json; charset=UTF-8','Authorization' => $authorization_key),
'body' => array('orderRef' => $orderRef)
);
$response = wp_remote_get( $url, $args);
$response_decode = json_decode($response['body']);
wp_send_json($response['body']);
}
// Work if user is login
add_action( 'wp_ajax_getProgressStatusAnother', 'getProgressStatusAnother' );
// Work if user is not login
add_action("wp_ajax_nopriv_getProgressStatusAnother", "getProgressStatusAnother");
function getProgressStatusAnother(){
$redirect_id = $_POST['redirect_id'] ? $_POST['redirect_id'] : "";
$url = $bankID_api_url.'/'.$api_version.'/eid/'.$redirect_id;
$args = array(
'headers' => array('Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => $authorization_key),
);
$response = wp_remote_get( $url, $args);
$response_decode = json_decode($response['body']);
wp_send_json($response['body']);
}
// Work if user is login
add_action( 'wp_ajax_registerBankIdUser', 'registerBankIdUser' );
// Work if user is not login
add_action("wp_ajax_nopriv_registerBankIdUser", "registerBankIdUser");
function registerBankIdUser(){
$user_info = $_POST['user_info'];
$fullname = $user_info['FullName'] ? $user_info['FullName'] : $user_info['FirstName'].' '.$user_info['LastName'];
$userdata = array(
'user_login' => str_replace(' ', '', $fullname),
'user_email' => $user_info['Email'],
'user_pass' => $user_info['PersonalNumber'],
'first_name' => $user_info['FirstName'],
'last_name' => $user_info['LastName'],
);
$user_id = wp_insert_user( $userdata );
if(!empty($user_id)){
update_user_meta( $user_id, 'billing_phone', $user_info['PhoneNumbers'][0] ? $user_info['PhoneNumbers'][0] : "" );
update_user_meta( $user_id, 'billing_address_1', $user_info['Address'] );
update_user_meta( $user_id, 'billing_address_2', $user_info['Address2'] );
update_user_meta( $user_id, 'billing_city', $user_info['City'] );
update_user_meta( $user_id, 'billing_country', 'Sweden' );
update_user_meta( $user_id, 'billing_state', $user_info['CountryCode'] );
update_user_meta( $user_id, 'billing_postcode', $user_info['PostalCode'] );
update_user_meta( $user_id, 'billing_email', $user_info['Email'] );
update_user_meta( $user_id, 'bankID', $user_info['PersonalNumber'] );
}
autorizationBankIdUser(str_replace(' ', '', $fullname), $user_info['PersonalNumber']);
wp_die('success');
}
function autorizationBankIdUser($username, $password){
$auth = wp_authenticate($username, $password);
if ( is_wp_error( $auth ) ) {
$error_string = $auth->get_error_message();
echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
} else {
wp_set_auth_cookie( $auth->ID );
wp_die('authenticate');
}
}
function complete_registration($username, $password, $email, $first_name, $last_name) {
global $reg_errors, $username, $password, $email, $first_name, $last_name;
if ( count($reg_errors->get_error_messages()) < 1 ) {
$userdata = array(
'user_login' => str_replace(' ', '', $username),
'user_email' => $email,
'user_pass' => $password,
'first_name' => $first_name,
'last_name' => $last_name,
);
$user = wp_insert_user( $userdata );
echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';
}
}
add_shortcode('bank_id_login', 'custom_login_shortcode');
function custom_login_shortcode() {
ob_start();
custom_login_function();
return ob_get_clean();
}
add_shortcode('bank_id_registration', 'custom_registration_shortcode');
function custom_registration_shortcode() {
ob_start();
custom_registration_function();
return ob_get_clean();
}
// Work if user is login
add_action( 'wp_ajax_check_user', 'check_user' );
// Work if user is not login
add_action("wp_ajax_nopriv_check_user", "check_user");
function check_user(){
$personalNumber = $_POST['personalNumber'];
$user = get_users(array('meta_key' => 'bankID', 'meta_value' => $personalNumber));
setcookie('BankID', $personalNumber, (time()+60), COOKIEPATH, COOKIE_DOMAIN);
return $user;
}
// Add field bankid to user
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="bankID"><?php _e("BankID"); ?></label></th>
<td>
<input type="text" name="bankID" id="bankID" value="<?php echo esc_attr( get_the_author_meta( 'bankID', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your BankID."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_customer_meta_fields' );
add_action( 'edit_user_profile_update', 'save_customer_meta_fields' );
function save_customer_meta_fields( $user_id ) {
if ($bank_id = filter_var($_POST['bankID'], FILTER_SANITIZE_STRIPPED)) {
update_user_meta( $user_id, 'bankID', $bank_id );
} else {
update_user_meta( $user_id, 'bankID', '' );
}
}