| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hyperpay\Gateways\App; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 8 |
use Hyperpay\Gateways\Main; |
| 9 |
use WC_Order; |
| 10 |
|
| 11 |
/** |
| 12 |
* Dummy Payments Blocks integration |
| 13 |
* |
| 14 |
* @since 1.0.3 |
| 15 |
*/ |
| 16 |
class Hyperpay_Blocks_Support extends AbstractPaymentMethodType |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Payment method name/id/slug. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
|
| 24 |
public $gateways, $id = "hyperpay_blocks"; |
| 25 |
|
| 26 |
protected $name = 'hyperpay_blocks'; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Initializes the payment method type. |
| 33 |
*/ |
| 34 |
public function initialize() |
| 35 |
{ |
| 36 |
$this->gateways = $this->get_gateways(); |
| 37 |
|
| 38 |
foreach ($this->gateways as $gateway) { |
| 39 |
$gateway->initialize(); |
| 40 |
} |
| 41 |
|
| 42 |
add_action('wc_ajax_hyperpay_can_make_payment', [$this, 'canMakePayment']); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public function get_gateways() |
| 47 |
{ |
| 48 |
$gateways = WC()->payment_gateways->payment_gateways(); |
| 49 |
$enabled_gateways = []; |
| 50 |
|
| 51 |
foreach ($gateways as $gateway) { |
| 52 |
if (str_starts_with($gateway->id, "hyperpay") && $gateway->enabled == "yes") { |
| 53 |
$enabled_gateways[$gateway->id] = $gateway; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return $enabled_gateways; |
| 58 |
} |
| 59 |
|
| 60 |
public function canMakePayment() |
| 61 |
{ |
| 62 |
|
| 63 |
if (!isset($_GET["payment_method"]) || !\array_key_exists(sanitize_text_field(wp_unslash($_GET["payment_method"])), $this->gateways) || !isset($_GET['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['nonce'])), 'hyperpay_can_make_payment')) { |
| 64 |
return wp_send_json(["canMakePayment" => false]); |
| 65 |
} |
| 66 |
|
| 67 |
$payment_method = sanitize_text_field(wp_unslash($_GET["payment_method"])); |
| 68 |
|
| 69 |
|
| 70 |
return $this->gateways[$payment_method]->canMakePayment(); |
| 71 |
} |
| 72 |
/** |
| 73 |
* Returns if this payment method should be active. If false, the scripts will not be enqueued. |
| 74 |
* |
| 75 |
* @return boolean |
| 76 |
*/ |
| 77 |
public function is_active() |
| 78 |
{ |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public function process_payment($order_id) |
| 84 |
{ |
| 85 |
|
| 86 |
$order = new WC_Order($order_id); |
| 87 |
|
| 88 |
|
| 89 |
return [ |
| 90 |
'result' => 'success', |
| 91 |
'redirect' => $order->get_checkout_payment_url(true) |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns an array of scripts/handles to be registered for this payment method. |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function get_payment_method_script_handles() |
| 101 |
{ |
| 102 |
$script_path = '/src/assets/js/blocks/blocks.js'; |
| 103 |
$script_url = HYPERPAY_PLUGIN_DIR . $script_path; |
| 104 |
$script_asset_path = HYPERPAY_PLUGIN_DIR . '/src/assets/js/blocks/blocks.asset.php'; |
| 105 |
|
| 106 |
$script_asset = file_exists($script_asset_path) |
| 107 |
? require($script_asset_path) |
| 108 |
: array( |
| 109 |
'dependencies' => array(), |
| 110 |
'version' => '2.1.2' |
| 111 |
); |
| 112 |
|
| 113 |
wp_register_script( |
| 114 |
$this->id, |
| 115 |
$script_url, |
| 116 |
$script_asset['dependencies'], |
| 117 |
$script_asset['version'], |
| 118 |
true |
| 119 |
); |
| 120 |
|
| 121 |
|
| 122 |
return [$this->id]; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Returns an array of key=>value pairs of data made available to the payment methods script. |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function get_payment_method_data() |
| 132 |
{ |
| 133 |
$data = []; |
| 134 |
|
| 135 |
foreach ($this->gateways as $gateway) { |
| 136 |
$gateWayData = [ |
| 137 |
'name' => $gateway->id, |
| 138 |
'title' => $gateway->title, |
| 139 |
'supports' => array_filter($gateway->supports, [$gateway, 'supports']), |
| 140 |
'icon' => $gateway->iconSrc(), |
| 141 |
'description' => $gateway->description, |
| 142 |
'gatewayMerchantId' => $gateway->entityId, |
| 143 |
'currencyCode' => $gateway->currency, |
| 144 |
'brands' => implode(' ', $gateway->brands), |
| 145 |
'nonce' => wp_create_nonce('hyperpay_can_make_payment'), |
| 146 |
'isDynamicCheck' => $gateway->isDynamicCheck, |
| 147 |
'isExpress' => $gateway->isExpress, |
| 148 |
'action_button' => $gateway->action_button, |
| 149 |
'extraScriptData' => $gateway->extraScriptData(), |
| 150 |
'site_url' => site_url(), |
| 151 |
'script_url' => $gateway->script_url, |
| 152 |
]; |
| 153 |
|
| 154 |
$key = $gateway->isExpress ? "express" : "blocks"; |
| 155 |
$data[$key][] = $gateWayData; |
| 156 |
} |
| 157 |
return $data; |
| 158 |
} |
| 159 |
} |
| 160 |
|