geminilabs

BankID API custom validation

Jun 9th, 2021 (edited)
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. /**
  2.  * Validate the custom social field value with the BankID API
  3.  * @param bool $isValid
  4.  * @param GeminiLabs\SiteReviews\Request $request
  5.  * @return bool|string
  6.  */
  7. add_filter('site-reviews/validate/custom', function ($isValid, $request) {
  8.     $apiResponse = wp_remote_post('https://api.banksignering.se/api/auth', [
  9.         'body' => [
  10.             'apiUser' => 'xxx',
  11.             'password' => 'yyy',
  12.             'companyApiGuid' => 'zzz',
  13.             'personalNumber' => $request->social,
  14.             'endUserIp' => $request->ip_address,
  15.         ],
  16.     ]);
  17.     if (is_wp_error($apiResponse)) {
  18.         // WordPress encountered an error when making the API request.
  19.         // You can either return the WordPress error message here,
  20.         // or return a custom validation error message
  21.         return $apiResponse->get_message();
  22.     }
  23.     // Get the response body of the API request
  24.     $apiResponseBody = json_decode($apiResponse['body']);
  25.     if (wp_validate_boolean(glsr_get($apiResponseBody, 'authResponse.Success'))) {
  26.         return true; // validation was successful!
  27.     }
  28.     // The API request returned an error, so return an appropriate
  29.     // validation error message here
  30.     return 'BankID validation failed, please check your social number.';
  31. }, 10, 2);
  32.  
  33. /**
  34.  * Remove the submitted custom social field value before the review is saved
  35.  * @param array $values
  36.  * @return array
  37.  */
  38. add_filter('site-reviews/create/review-values', function ($values) {
  39.     if (isset($values['custom']['social'])) {
  40.         // Replace the submitted social security value
  41.         $values['custom']['social'] = 'Validated with BankID';
  42.         $values['request']->set('social', 'Validated with BankID');
  43.     }
  44.     return $values;
  45. });
Add Comment
Please, Sign In to add comment