| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Controller; |
| 4 |
|
| 5 |
use Payplug\Exception\HttpException; |
| 6 |
use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData; |
| 7 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 8 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 9 |
use Payplug\Resource\Payment as PaymentResource; |
| 10 |
|
| 11 |
class Bancontact extends PayplugGateway |
| 12 |
{ |
| 13 |
|
| 14 |
protected $enable = false; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
|
| 19 |
parent::__construct(); |
| 20 |
|
| 21 |
/** @var \WC_Settings_API override $id */ |
| 22 |
$this->id = 'bancontact'; |
| 23 |
|
| 24 |
/** @var \WC_Payment_Gateway overwrite for bancontact settings */ |
| 25 |
$this->method_title = __('payplug_bancontact_title', 'payplug'); |
| 26 |
$this->method_description = __('payplug_bancontact_description', 'payplug'); |
| 27 |
|
| 28 |
$this->title = __('payplug_bancontact_title', 'payplug'); |
| 29 |
$this->description = ''; |
| 30 |
|
| 31 |
if(!$this->checkBancontact()) |
| 32 |
$this->enabled = 'no'; |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
private function checkBancontact(){ |
| 37 |
$account = PayplugWoocommerceHelper::get_account_data_from_options(); |
| 38 |
|
| 39 |
if (isset($account['payment_methods']['bancontact']['enabled']) ) { |
| 40 |
|
| 41 |
if( !empty($account['bancontact']) && $account['bancontact'] === 'yes' ) |
| 42 |
return $account['payment_methods']['bancontact']['enabled']; |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Get payment icons. |
| 52 |
* |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
public function get_icon() |
| 56 |
{ |
| 57 |
$available_img = 'lg-bancontact-checkout.png'; |
| 58 |
$icons = apply_filters('payplug_payment_icons', [ |
| 59 |
'payplug' => sprintf('<img src="%s" alt="Oney 4x" class="payplug-payment-icon" />', esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/' . $available_img)), |
| 60 |
]); |
| 61 |
$icons_str = ''; |
| 62 |
foreach ($icons as $icon) { |
| 63 |
$icons_str .= $icon; |
| 64 |
} |
| 65 |
return $icons_str; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @param \WC_Order $order |
| 70 |
* @param int $amount |
| 71 |
* @param int $customer_id |
| 72 |
* |
| 73 |
* @return array |
| 74 |
* @throws \Exception |
| 75 |
*/ |
| 76 |
public function process_standard_payment($order, $amount, $customer_id) |
| 77 |
{ |
| 78 |
$order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id(); |
| 79 |
try { |
| 80 |
if (!$this->checkBancontact()) { |
| 81 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 82 |
} |
| 83 |
|
| 84 |
$address_data = PayplugAddressData::from_order($order); |
| 85 |
|
| 86 |
$return_url = esc_url_raw($order->get_checkout_order_received_url()); |
| 87 |
|
| 88 |
if (!(substr( $return_url, 0, 4 ) === "http")) { |
| 89 |
$return_url = get_site_url().$return_url; |
| 90 |
} |
| 91 |
|
| 92 |
$payment_data = [ |
| 93 |
'amount' => $amount, |
| 94 |
'currency' => get_woocommerce_currency(), |
| 95 |
'payment_method' => $this->id, |
| 96 |
'billing' => $address_data->get_billing(), |
| 97 |
'shipping' => $address_data->get_shipping(), |
| 98 |
'hosted_payment' => [ |
| 99 |
'return_url' => $return_url, |
| 100 |
'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()), |
| 101 |
], |
| 102 |
'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')), |
| 103 |
'metadata' => [ |
| 104 |
'order_id' => $order_id, |
| 105 |
'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest', |
| 106 |
'domain' => $this->limit_length(esc_url_raw(home_url()), 500), |
| 107 |
], |
| 108 |
"save_card"=> false, |
| 109 |
"force_3ds"=> false |
| 110 |
]; |
| 111 |
|
| 112 |
/** |
| 113 |
* Filter the payment data before it's used |
| 114 |
* |
| 115 |
* @param array $payment_data |
| 116 |
* @param int $order_id |
| 117 |
* @param array $customer_details |
| 118 |
* @param PayplugAddressData $address_data |
| 119 |
*/ |
| 120 |
$payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data); |
| 121 |
$payment = $this->api->payment_create($payment_data); |
| 122 |
|
| 123 |
// Save transaction id for the order |
| 124 |
PayplugWoocommerceHelper::is_pre_30() |
| 125 |
? update_post_meta($order_id, '_transaction_id', $payment->id) |
| 126 |
: $order->set_transaction_id($payment->id); |
| 127 |
|
| 128 |
if (is_callable([$order, 'save'])) { |
| 129 |
$order->save(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Fires once a payment has been created. |
| 134 |
* |
| 135 |
* @param int $order_id Order ID |
| 136 |
* @param PaymentResource $payment Payment resource |
| 137 |
*/ |
| 138 |
\do_action('payplug_gateway_payment_created', $order_id, $payment); |
| 139 |
|
| 140 |
$metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); |
| 141 |
PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); |
| 142 |
|
| 143 |
PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id)); |
| 144 |
|
| 145 |
return [ |
| 146 |
'result' => 'success', |
| 147 |
'redirect' => $payment->hosted_payment->payment_url, |
| 148 |
'cancel' => $payment->hosted_payment->cancel_url, |
| 149 |
]; |
| 150 |
|
| 151 |
} catch (HttpException $e) { |
| 152 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error'); |
| 153 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 154 |
} catch (\Exception $e) { |
| 155 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error'); |
| 156 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @return bool|void |
| 163 |
*/ |
| 164 |
public function process_admin_options() { |
| 165 |
$data = $this->get_post_data(); |
| 166 |
if (isset($data['woocommerce_payplug_mode'])) { |
| 167 |
if ( $data['woocommerce_payplug_mode'] === '0' ) { |
| 168 |
$options = get_option( 'woocommerce_payplug_settings', [] ); |
| 169 |
$options['bancontact'] = 'no'; |
| 170 |
update_option( 'woocommerce_payplug_settings', apply_filters( 'woocommerce_settings_api_sanitized_fields_payplug', $options ) ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if (isset($data['woocommerce_payplug_bancontact'])) { |
| 175 |
if (($data['woocommerce_payplug_bancontact'] == 1) && (!$this->checkBancontact())) { |
| 176 |
$options = get_option('woocommerce_payplug_settings', []); |
| 177 |
$options['bancontact'] = 'no'; |
| 178 |
update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) ); |
| 179 |
add_action( 'admin_notices', [$this ,"display_notice"] ); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Display unauthorized error |
| 188 |
* |
| 189 |
* @return void |
| 190 |
*/ |
| 191 |
public static function display_notice() { |
| 192 |
?> |
| 193 |
<div class="notice notice-error is-dismissible"> |
| 194 |
<p><?php echo __( 'payplug_bancontact_unauthorized_message', 'payplug' ); ?></p> |
| 195 |
</div> |
| 196 |
<?php |
| 197 |
} |
| 198 |
|
| 199 |
} |
| 200 |
|