Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion html/admin/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function unzerRefreshData() {
document.getElementById('unzer-totals-row').innerHTML = totalsHtml;

let captureHtml = '';
if (data.remainingPlain && data.paymentMethod !== 'unzer_prepayment' && data.status !== 'canceled') {
if (data.remainingPlain && data.paymentMethod !== 'unzer_prepayment' && data.paymentMethod !== 'unzer_open_banking' && data.status !== 'canceled') {
captureHtml = `<input type="number" step="0.01" min="0.01" max="${data.remainingPlain}" value="${data.remainingPlain}" id="unzer-capture-amount-input" />
<a href="#" id="unzer-capture-btn" onclick="unzerCaptureOrder(unzerOrderId, document.getElementById('unzer-capture-amount-input').value, '<?php echo esc_attr( Util::getNonce() ); ?>'); return false;" class="button button-small"><?php echo esc_html__( 'Capture', 'unzer-payments' ); ?></a>`;
}
Expand Down
113 changes: 56 additions & 57 deletions includes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Main {
const ORDER_META_KEY_CANCELLATION_ID = 'unzer_cancellation_id';
const ORDER_META_KEY_DATE_OF_BIRTH = 'unzer_dob';
const ORDER_META_KEY_COMPANY_TYPE = 'unzer_company_type';
const ORDER_CHARGE_AUTOMATICALLY_DONE = 'unzer_charge_automatically_done';
const ORDER_CHARGE_AUTOMATICALLY_DONE = 'unzer_charge_automatically_done';
const ORDER_META_KEYS = array(
self::ORDER_META_KEY_AUTHORIZATION_ID,
self::ORDER_META_KEY_CHARGE_ID,
Expand Down Expand Up @@ -103,7 +103,7 @@ public function registerEvents(): void {
add_action( 'admin_notices', array( new DashboardService(), 'showNotifications' ) );
add_action( 'woocommerce_blocks_loaded', array( $this, 'addCheckoutBlocks' ) );
add_action( 'before_woocommerce_pay_form', array( $this, 'orderPayPaymentMethod' ), 20, 4 );
add_action( 'woocommerce_order_status_changed', array($this, 'unzerPaymentStatusChange'), 10, 4);
add_action( 'woocommerce_order_status_changed', array( $this, 'unzerPaymentStatusChange' ), 10, 4 );
add_action(
'admin_enqueue_scripts',
function () {
Expand Down Expand Up @@ -277,27 +277,27 @@ public function addGlobalSettings( $settings, $currentSection ): array {
update_option( 'unzer_authorized_order_status', OrderService::ORDER_STATUS_AUTHORIZED );
}
$settings = array(
'title' => array(
'title' => array(
'type' => 'title',
'desc' => '',
),
'public_key' => array(
'public_key' => array(
'title' => __( 'Public Key', 'unzer-payments' ),
'type' => 'text',
'desc' => '',
'id' => 'unzer_public_key',
'value' => get_option( 'unzer_public_key' ),
'default' => '',
),
'private_key' => array(
'private_key' => array(
'title' => __( 'Private Key', 'unzer-payments' ),
'type' => 'text',
'desc' => '',
'id' => 'unzer_private_key',
'value' => get_option( 'unzer_private_key' ),
'default' => '',
),
'authorized_order_status' => array(
'authorized_order_status' => array(
'title' => __( 'Order status for authorized payments', 'unzer-payments' ),
'label' => '',
'type' => 'select',
Expand All @@ -307,7 +307,7 @@ public function addGlobalSettings( $settings, $currentSection ): array {
'value' => get_option( 'unzer_authorized_order_status' ),
'default' => OrderService::ORDER_STATUS_AUTHORIZED,
),
'captured_order_status' => array(
'captured_order_status' => array(
'title' => __( 'Order status for captured payments', 'unzer-payments' ),
'label' => '',
'type' => 'select',
Expand All @@ -317,7 +317,7 @@ public function addGlobalSettings( $settings, $currentSection ): array {
'value' => get_option( 'unzer_captured_order_status' ),
'default' => '',
),
'chargeback_order_status' => array(
'chargeback_order_status' => array(
'title' => __( 'Order status for chargebacks', 'unzer-payments' ),
'label' => '',
'type' => 'select',
Expand All @@ -327,26 +327,26 @@ public function addGlobalSettings( $settings, $currentSection ): array {
'value' => get_option( 'unzer_chargeback_order_status' ),
'default' => OrderService::ORDER_STATUS_CHARGEBACK,
),
'capture_trigger_order_status' => array(
'title' => __( 'Order status to trigger capture', 'unzer-payments' ),
'label' => '',
'type' => 'select',
'desc' => __( 'This status triggers an automatic capture action', 'unzer-payments' ),
'options' => array_merge( array( '' => __( '[No automatic capture]', 'unzer-payments' ) ), wc_get_order_statuses() ),
'id' => 'unzer_capture_trigger_order_status',
'value' => get_option( 'unzer_capture_trigger_order_status' ),
'default' => '',
),
'capture_order_status_lock' => array(
'title' => __( 'Lock order state', 'unzer-payments' ),
'label' => '',
'type' => 'checkbox',
'desc' => __( 'When checked, order state will not change to order state defined in “Order status for captured payments” after automatic capture.', 'unzer-payments' ),
'id' => 'unzer_capture_order_status_lock',
'value' => get_option( 'unzer_capture_order_status_lock' ),
'default' => '',
),
'sectionend' => array(
'capture_trigger_order_status' => array(
'title' => __( 'Order status to trigger capture', 'unzer-payments' ),
'label' => '',
'type' => 'select',
'desc' => __( 'This status triggers an automatic capture action', 'unzer-payments' ),
'options' => array_merge( array( '' => __( '[No automatic capture]', 'unzer-payments' ) ), wc_get_order_statuses() ),
'id' => 'unzer_capture_trigger_order_status',
'value' => get_option( 'unzer_capture_trigger_order_status' ),
'default' => '',
),
'capture_order_status_lock' => array(
'title' => __( 'Lock order state', 'unzer-payments' ),
'label' => '',
'type' => 'checkbox',
'desc' => __( 'When checked, order state will not change to order state defined in “Order status for captured payments” after automatic capture.', 'unzer-payments' ),
'id' => 'unzer_capture_order_status_lock',
'value' => get_option( 'unzer_capture_order_status_lock' ),
'default' => '',
),
'sectionend' => array(
'type' => 'sectionend',
),
);
Expand Down Expand Up @@ -399,35 +399,34 @@ public function getPaymentGateway( $key ): ?AbstractGateway {
return null;
}

public function unzerPaymentStatusChange($order_id, $from, $to, $order)
{
if (!AbstractGateway::isUnzerPaymentMethod($order->get_payment_method())) {
return;
}
if ( $order->get_payment_method() === Prepayment::GATEWAY_ID ) {
return;
}
if ( $order->get_payment_method() === OpenBanking::GATEWAY_ID ) {
return;
}

try {
if (in_array($to, [str_replace('wc-', '', get_option( 'unzer_capture_trigger_order_status' ))], true)) {
if (!$order->is_paid()) {
$unzer = ( new PaymentService() )->getUnzerManagerForOrder( $order );
$paymentId = $order->get_meta( Main::ORDER_META_KEY_PAYMENT_ID, true );
$payment = $unzer->fetchPayment( $paymentId );
if ( $payment->getAmount()->getCharged() == 0 ) {
$order->update_meta_data( Main::ORDER_CHARGE_AUTOMATICALLY_DONE, 'yes' );
$order->save_meta_data();
( new PaymentService() )->performChargeOnAuthorization( $order_id );
}
}
}
} catch (\Exception $e) {
// silent
}
}
public function unzerPaymentStatusChange( $order_id, $from, $to, $order ) {
if ( ! AbstractGateway::isUnzerPaymentMethod( $order->get_payment_method() ) ) {
return;
}
if ( $order->get_payment_method() === Prepayment::GATEWAY_ID ) {
return;
}
if ( $order->get_payment_method() === OpenBanking::GATEWAY_ID ) {
return;
}

try {
if ( in_array( $to, array( str_replace( 'wc-', '', get_option( 'unzer_capture_trigger_order_status' ) ) ), true ) ) {
if ( ! $order->is_paid() ) {
$unzer = ( new PaymentService() )->getUnzerManagerForOrder( $order );
$paymentId = $order->get_meta( self::ORDER_META_KEY_PAYMENT_ID, true );
$payment = $unzer->fetchPayment( $paymentId );
if ( $payment->getAmount()->getCharged() == 0 ) {
$order->update_meta_data( self::ORDER_CHARGE_AUTOMATICALLY_DONE, 'yes' );
$order->save_meta_data();
( new PaymentService() )->performChargeOnAuthorization( $order_id );
}
}
}
} catch ( \Exception $e ) {
// silent
}
}

public function addCheckoutBlocks() {
if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
Expand Down
67 changes: 33 additions & 34 deletions includes/controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function receiveWebhook() {
'msg' => 'event not relevant',
)
);
die;
}

// TODO: evaluate if we have better options to prevent the race condition
Expand Down Expand Up @@ -176,41 +175,41 @@ private function handleChargeSucceeded( $paymentId, $orderId ) {
'orderId' => $orderId,
)
);
$order = wc_get_order( $orderId );
$previous_status = $order->get_status();
$order->payment_complete( $paymentId );
$order = wc_get_order( $orderId );
$previous_status = $order->get_status();
$order->payment_complete( $paymentId );
$order->set_transaction_id( $paymentId );
if ( get_option( 'unzer_captured_order_status' ) ) {
$update_order_status = true;
if ( get_option( 'unzer_capture_order_status_lock' ) === 'yes' ) {
if ($order->get_meta( Main::ORDER_CHARGE_AUTOMATICALLY_DONE) === 'yes') {
$update_order_status = false;
}
}
if (get_option( 'unzer_capture_trigger_order_status' )) {
$triggerStatus = str_replace(
'wc-',
'',
get_option( 'unzer_capture_trigger_order_status' )
);

if (
in_array(
$order->get_payment_method(),
array(
Prepayment::GATEWAY_ID,
OpenBanking::GATEWAY_ID,
),
true
)
) {
$update_order_status = false;
}
}

if ($update_order_status) {
$order->set_status( str_replace( 'wc-', '', get_option( 'unzer_captured_order_status' ) ) );
}
$update_order_status = true;
if ( get_option( 'unzer_capture_order_status_lock' ) === 'yes' ) {
if ( $order->get_meta( Main::ORDER_CHARGE_AUTOMATICALLY_DONE ) === 'yes' ) {
$update_order_status = false;
}
}
if ( get_option( 'unzer_capture_trigger_order_status' ) ) {
$triggerStatus = str_replace(
'wc-',
'',
get_option( 'unzer_capture_trigger_order_status' )
);

if (
in_array(
$order->get_payment_method(),
array(
Prepayment::GATEWAY_ID,
OpenBanking::GATEWAY_ID,
),
true
)
) {
$update_order_status = false;
}
}

if ( $update_order_status ) {
$order->set_status( str_replace( 'wc-', '', get_option( 'unzer_captured_order_status' ) ) );
}
}
$order->save();
}
Expand Down
102 changes: 51 additions & 51 deletions includes/gateways/Blocks/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ public function get_payment_method_data() {
/** @var AbstractGateway $gateway */
$gateway = new $gatewayClass();
return array(
'id' => static::GATEWAY_ID,
'title' => $gateway->title,
'description' => $gateway->description,
'allowedCountries' => $gateway->allowedCountries,
'allowedCurrencies' => $gateway->allowedCurrencies,
'publicKey' => $gateway->get_public_key(),
'locale' => get_locale(),
'nonce' => Util::getNonce(),
'id' => static::GATEWAY_ID,
'title' => $gateway->title,
'description' => $gateway->description,
'allowedCountries' => $gateway->allowedCountries,
'allowedCurrencies' => $gateway->allowedCurrencies,
'publicKey' => $gateway->get_public_key(),
'locale' => get_locale(),
'nonce' => Util::getNonce(),
'storeName' => get_bloginfo( 'name' ),
'storeCountry' => strtoupper( substr( get_option( 'woocommerce_default_country' ), 0, 2 ) ),
'paymentComponentId' => str_replace( '_', '-', static::GATEWAY_ID ) . '-payment-component',
'paymentComponentId' => str_replace( '_', '-', static::GATEWAY_ID ) . '-payment-component',
'checkoutComponentId' => str_replace( '_', '-', static::GATEWAY_ID ) . '-checkout-component',
'getCustomerDataUrl' => WC()->api_request_url( CheckoutController::GET_UNZER_CUSTOMER_SLUG ),
'getCustomerDataUrl' => WC()->api_request_url( CheckoutController::GET_UNZER_CUSTOMER_SLUG ),
);
}

Expand Down Expand Up @@ -92,47 +92,47 @@ protected function get_identifier() {
}

public function get_payment_method_script_handles() {
if (is_admin()) {
$identifier = $this->get_identifier();
$asset_handle = $identifier . '-block-checkout';
$script_dependencies_path = UNZER_PLUGIN_PATH . 'assets/build/' . $identifier . '.asset.php';
$script_url = UNZER_PLUGIN_URL . '/assets/build/' . $identifier . '.js';

if ( file_exists( $script_dependencies_path ) ) {
$script_dependencies = require $script_dependencies_path;

if ( ! wp_script_is( 'unzer_global-block-checkout', 'registered' ) ) {
wp_register_script(
'unzer_global-block-checkout',
UNZER_PLUGIN_URL . '/assets/build/unzer_global.js',
array( 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n' ),
UNZER_VERSION,
true
);
}

$dependencies = array_unique(
array_merge(
array( 'unzer_global-block-checkout' ),
$script_dependencies['dependencies']
)
);

if ( ! wp_script_is( $asset_handle, 'registered' ) ) {
wp_register_script(
$asset_handle,
$script_url,
$dependencies,
$script_dependencies['version'],
true
);

wp_set_script_translations( $asset_handle, 'unzer-payments' );
}
}

return array( $asset_handle );
}
if ( is_admin() ) {
$identifier = $this->get_identifier();
$asset_handle = $identifier . '-block-checkout';
$script_dependencies_path = UNZER_PLUGIN_PATH . 'assets/build/' . $identifier . '.asset.php';
$script_url = UNZER_PLUGIN_URL . '/assets/build/' . $identifier . '.js';

if ( file_exists( $script_dependencies_path ) ) {
$script_dependencies = require $script_dependencies_path;

if ( ! wp_script_is( 'unzer_global-block-checkout', 'registered' ) ) {
wp_register_script(
'unzer_global-block-checkout',
UNZER_PLUGIN_URL . '/assets/build/unzer_global.js',
array( 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n' ),
UNZER_VERSION,
true
);
}

$dependencies = array_unique(
array_merge(
array( 'unzer_global-block-checkout' ),
$script_dependencies['dependencies']
)
);

if ( ! wp_script_is( $asset_handle, 'registered' ) ) {
wp_register_script(
$asset_handle,
$script_url,
$dependencies,
$script_dependencies['version'],
true
);

wp_set_script_translations( $asset_handle, 'unzer-payments' );
}
}

return array( $asset_handle );
}
return array( $this->get_identifier() . '-block-checkout' );
}

Expand Down
Loading
Loading