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