Skip to content

Commit

Permalink
PayU - secureform via WooCommerce Blocks, version update
Browse files Browse the repository at this point in the history
  • Loading branch information
regdos committed Sep 21, 2024
1 parent 1377424 commit df13eb5
Show file tree
Hide file tree
Showing 63 changed files with 3,852 additions and 4,414 deletions.
11 changes: 11 additions & 0 deletions Payu/Blocks/PayuSecureFormBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Payu\PaymentGateway\Blocks;

class PayuSecureFormBlock extends PayuBlocks {
protected $name = 'payusecureform';

public function __construct() {
parent::__construct( true );
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php

use Payu\PaymentGateway\Gateways\WC_Payu_Gateways;
namespace Payu\PaymentGateway\Gateways;

use OpenPayU_Result;

class WC_Gateway_PayuSecureForm extends WC_Payu_Gateways {
protected string $paytype = 'c';
private string $payu_sdk_url;

function __construct() {
parent::__construct( 'payusecureform' );

$this->payu_sdk_url = $this->sandbox ? 'https://secure.snd.payu.com/javascript/sdk' : 'https://secure.payu.com/javascript/sdk';

if ( $this->is_enabled() ) {
$this->icon = apply_filters( 'woocommerce_payu_icon', plugins_url( '/assets/images/card-visa-mc.svg', PAYU_PLUGIN_FILE ) );

Expand All @@ -28,10 +33,16 @@ public function is_available(): bool {
return parent::is_available();
}

/**
* @return null
*/
function minicart_checkout_refresh_script() {
// Additional data for Blocks
public function get_additional_data(): array {
return [
'posId' => $this->pos_id,
'sdkUrl' => $this->payu_sdk_url,
'lang' => explode( '_', get_locale() )[0]
];
}

function minicart_checkout_refresh_script(): void {
if ( is_checkout() || is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
Expand Down Expand Up @@ -63,12 +74,7 @@ public function payment_fields(): void {
}
}

/**
* @param OpenPayU_Result $response
*
* @return null
*/
private function retrieve_methods( $response ) {
private function retrieve_methods( OpenPayU_Result $response ): void {
$payMethods = $response->getResponse();
if ( $payMethods->payByLinks ) {
$payByLinks = $this->process_pay_methods( $payMethods->payByLinks );
Expand Down Expand Up @@ -114,13 +120,10 @@ private function retrieve_methods( $response ) {
protected function get_payu_pay_method(): array {
$token = sanitize_text_field( $_POST['payu_sf_token'] );

return $this->get_payu_pay_method_array( 'CARD_TOKEN', $token ? $token : - 1, $this->paytype );
return $this->get_payu_pay_method_array( 'CARD_TOKEN', $token );
}

/**
* @return void
*/
public function include_payu_sf_scripts() {
public function include_payu_sf_scripts(): void {
$payu_sdk_url = $this->sandbox ? 'https://secure.snd.payu.com/javascript/sdk' : 'https://secure.payu.com/javascript/sdk';
wp_enqueue_script( 'payu-sfsdk', $payu_sdk_url, [], null );
wp_enqueue_script( 'payu-promise-polyfill', plugins_url( '/assets/js/es6-promise.auto.min.js', PAYU_PLUGIN_FILE ), [], null );
Expand Down
110 changes: 42 additions & 68 deletions Payu/Gateways/WC_Payu_Gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
use WC_Payment_Gateway;
use WC_Shipping_Zone;

require_once WC_PAYU_PLUGIN_PATH . 'includes/lib/openpayu.php';

abstract class WC_Payu_Gateways extends WC_Payment_Gateway implements WC_PayuGateway {
public static $paymethods = [];

public $pos_id;
public $pos_widget_key;
public string $pos_id;
public string $pos_widget_key;
public $enable_for_shipping;
public $enable_for_virtual;

Expand Down Expand Up @@ -305,7 +303,8 @@ private function get_form_field_config( array $currencies = [] ): array {
if ( count( $currencies ) < 2 ) {
$currencies = [ '' ];
}
$config = [];
$config = [];
$payuSettings = get_option( 'payu_settings_option_name', [] );

foreach ( $currencies as $code ) {
$idSuffix = ( $code ? '_' : '' ) . $code;
Expand All @@ -321,8 +320,8 @@ private function get_form_field_config( array $currencies = [] ): array {
'desc_tip' => true,
'custom_attributes' => [
'data-global' => 'can-be-global',
'global-value' => $this->get_payu_option( [ 'payu_settings_option_name', 'global_' . $field ] ),
'local-value' => $this->get_payu_option( [ 'woocommerce_' . $this->id . '_settings', $field ] )
'global-value' => $payuSettings[ 'global_' . $field ] ?? '',
'local-value' => $payuSettings[ $field ] ?? ''
],
];
}
Expand All @@ -332,25 +331,6 @@ private function get_form_field_config( array $currencies = [] ): array {
return $config;
}

/**
* @param array $key
*
* @return string|false
*/
public function get_payu_option( $key ) {
if ( ! is_array( $key ) ) {
return false;
}

$option = get_option( $key[0] );

if ( ! is_array( $option ) || ! array_key_exists( $key[1], $option ) ) {
return false;
}

return $option[ $key[1] ];
}

private function get_form_field_info(): array {
return [
'description' => [
Expand Down Expand Up @@ -470,13 +450,9 @@ private function is_accessing_settings() {
}

/**
* @param string|null $currency
*
* @return void
* @throws
*
*/
public function init_OpenPayU( $currency = null ) {
public function init_OpenPayU( string $currency = null ): void {
$isSandbox = 'yes' === $this->get_option( 'sandbox' );

if ( woocommerce_payu_is_wmpl_active_and_configure() || woocommerce_payu_is_currency_custom_config() ) {
Expand All @@ -486,39 +462,27 @@ public function init_OpenPayU( $currency = null ) {
}

$optionPrefix = $isSandbox ? 'sandbox_' : '';
$payuSettings = get_option( 'payu_settings_option_name', [] );

OpenPayU_Configuration::setEnvironment( $isSandbox ? 'sandbox' : 'secure' );
if ( $this->get_option( 'use_global' ) === 'yes' || ! $this->get_option( 'use_global' ) ) {
$this->pos_id = $this->get_payu_option( [
'payu_settings_option_name',
'global_' . $optionPrefix . 'pos_id' . $optionSuffix
] );
$client_secret = $this->get_payu_option( [
'payu_settings_option_name',
'global_' . $optionPrefix . 'client_secret' . $optionSuffix
] );
if ( $this->get_option( 'use_global', 'yes' ) === 'yes' ) {
$this->pos_id = $payuSettings[ 'global_' . $optionPrefix . 'pos_id' . $optionSuffix ] ?? '';
$client_secret = $payuSettings[ 'global_' . $optionPrefix . 'client_secret' . $optionSuffix ] ?? '';
$this->pos_widget_key = substr( $client_secret, 0, 2 );
OpenPayU_Configuration::setMerchantPosId( $this->pos_id );
OpenPayU_Configuration::setSignatureKey( $this->get_payu_option( [
'payu_settings_option_name',
'global_' . $optionPrefix . 'md5' . $optionSuffix
] ) );
OpenPayU_Configuration::setOauthClientId( $this->get_payu_option( [
'payu_settings_option_name',
'global_' . $optionPrefix . 'client_id' . $optionSuffix
] ) );
OpenPayU_Configuration::setSignatureKey( $payuSettings[ 'global_' . $optionPrefix . 'md5' . $optionSuffix ] ?? '' );
OpenPayU_Configuration::setOauthClientId( $payuSettings[ 'global_' . $optionPrefix . 'client_id' . $optionSuffix ] ?? '' );
OpenPayU_Configuration::setOauthClientSecret( $client_secret );
} else {
$this->pos_id = $this->get_option( $optionPrefix . 'pos_id' . $optionSuffix );
$client_secret = $this->get_option( $optionPrefix . 'client_secret' . $optionSuffix );
$this->pos_id = $this->get_option( $optionPrefix . 'pos_id' . $optionSuffix, '' );
$client_secret = $this->get_option( $optionPrefix . 'client_secret' . $optionSuffix, '' );
$this->pos_widget_key = substr( $client_secret, 0, 2 );
OpenPayU_Configuration::setMerchantPosId( $this->pos_id );
OpenPayU_Configuration::setSignatureKey( $this->get_option( $optionPrefix . 'md5' . $optionSuffix ) );
OpenPayU_Configuration::setOauthClientId( $this->get_option( $optionPrefix . 'client_id' . $optionSuffix ) );
OpenPayU_Configuration::setSignatureKey( $this->get_option( $optionPrefix . 'md5' . $optionSuffix, '' ) );
OpenPayU_Configuration::setOauthClientId( $this->get_option( $optionPrefix . 'client_id' . $optionSuffix, '' ) );
OpenPayU_Configuration::setOauthClientSecret( $client_secret );
}


OpenPayU_Configuration::setOauthTokenCache( new OauthCache() );
OpenPayU_Configuration::setSender( 'Wordpress ver ' . get_bloginfo( 'version' ) . ' / WooCommerce ver ' . WC()->version . ' / Plugin ver ' . PAYU_PLUGIN_VERSION );
}
Expand Down Expand Up @@ -878,10 +842,7 @@ private function getThreeDsAuthentication( $order, $orderData ) {
}
}

if ( isset( $orderData['payMethods']['payMethod']['type'] ) && $orderData['payMethods']['payMethod']['type'] === 'CARD_TOKEN'
&& isset( $_POST['payu_browser'] )
&& is_array( $_POST['payu_browser'] )
) {
if ( isset( $orderData['payMethods']['payMethod']['type'] ) && $orderData['payMethods']['payMethod']['type'] === 'CARD_TOKEN' ) {
$possibleBrowserData = [
'screenWidth',
'javaEnabled',
Expand All @@ -891,22 +852,35 @@ private function getThreeDsAuthentication( $order, $orderData ) {
'colorDepth',
'language'
];
$browserData = [
'requestIP' => $this->getIP()
];

foreach ( $possibleBrowserData as $bd ) {
$browserData[ $bd ] = isset( $_POST['payu_browser'][ $bd ] ) ? sanitize_text_field( $_POST['payu_browser'][ $bd ] ) : '';
}
$browserData = [];

if ( empty( $browserData['userAgent'] ) ) {
$headers = array_change_key_case( getallheaders(), CASE_LOWER );
if ( $headers['user-agent'] ) {
$browserData['userAgent'] = $headers['user-agent'];
if ( isset( $_POST['payu_browser'] ) && is_array( $_POST['payu_browser'] ) ) {
foreach ( $possibleBrowserData as $bd ) {
$browserData[ $bd ] = isset( $_POST['payu_browser'][ $bd ] ) ? sanitize_text_field( $_POST['payu_browser'][ $bd ] ) : '';
}
} else {
foreach ( $possibleBrowserData as $bd ) {
$name = strtolower( 'payuBrowser_' . $bd );
if ( isset( $_POST[ $name ] ) ) {
$browserData[ $bd ] = sanitize_text_field( $_POST[ $name ] );
}
}
}

$threeDsAuthentication['browser'] = $browserData;
if ( count( $browserData ) > 0 ) {
$browserData['requestIP'] = $this->getIP();


if ( empty( $browserData['userAgent'] ) ) {
$headers = array_change_key_case( getallheaders(), CASE_LOWER );
if ( $headers['user-agent'] ) {
$browserData['userAgent'] = $headers['user-agent'];
}
}

$threeDsAuthentication['browser'] = $browserData;
}
}

return $threeDsAuthentication;
Expand Down
2 changes: 1 addition & 1 deletion assets/css/payu-gateway.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions assets/css/payu-gateway.scss
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,40 @@ label[for*="payment_method_payu"] {
}
}
}

.block-payu-card {
display: flex;
flex-wrap: wrap;
gap: 0 10px;
margin: 10px 0;
padding: 10px;

.payu-sf-validation-error {
color: #b21b0f;
font-size: 13px;
}

> .block-payu-card-number {
flex: 1 0 100%;
}

> .block-payu-card-date, .block-payu-card-cvv {
flex: 1 0 calc(50% - 10px);
}

.payu-card-form {
background-color: #FFFFFF;
border: 1px solid #999999;
border-radius: 5px;
padding: 8px 6px;

&.payu-secure-form-focus {
border-color: #666666;
box-shadow: 0 0 0 1px #666666;
}

&.payu-secure-form-invalid:not(.payu-secure-form-focus) {
border-color: #cc1818;
}
}
}
5 changes: 3 additions & 2 deletions blocks_translates_map.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"src/js/payublik.js": "build/js/payublik.js",
"src/js/payulistbanks.js": "build/js/payulistbanks.js"
"src/js/payublik.js": "build/js/payublik.js",
"src/js/payulistbanks.js": "build/js/payulistbanks.js",
"src/js/payusecureform.js": "build/js/payusecureform.js"
}
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
*** Changelog ***
= 2.6.0 - 2024-09-21 =
* [Add] PayU - secure form via WooCommerce Blocks

= 2.5.0 - 2024-07-23 =
* [Add] PayU - bank list via WooCommerce Blocks

Expand Down
39 changes: 21 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{
"name": "payu/payment_gateway",
"version": "2.5.0",
"description": "PayU GPO Payment for WooCommerce",
"type": "woocommerce-plugin",
"license": "Apache License 2.0",
"autoload": {
"psr-4": {
"Payu\\PaymentGateway\\": "Payu/"
}
},
"authors": [
{
"name": "PayU GPO"
}
],
"config": {
"optimize-autoloader": true
}
"name": "payu/payment_gateway",
"version": "2.6.0",
"description": "PayU GPO Payment for WooCommerce",
"type": "woocommerce-plugin",
"license": "Apache License 2.0",
"autoload": {
"psr-4": {
"Payu\\PaymentGateway\\": "Payu/"
}
},
"authors": [
{
"name": "PayU GPO"
}
],
"config": {
"optimize-autoloader": true
},
"require": {
"openpayu/openpayu": "2.3.*"
}
}
Loading

0 comments on commit df13eb5

Please sign in to comment.