Skip to content

Commit

Permalink
Merge pull request #153 from Dintero/develop
Browse files Browse the repository at this point in the history
Version 1.8.0
  • Loading branch information
mntzrr authored Oct 31, 2023
2 parents 0f50e28 + 68691cc commit 3403e06
Show file tree
Hide file tree
Showing 16 changed files with 616 additions and 736 deletions.
2 changes: 1 addition & 1 deletion assets/js/dintero-checkout-web-sdk.umd.min.js

Large diffs are not rendered by default.

29 changes: 2 additions & 27 deletions classes/class-dintero-checkout-callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ public function handle_callback( $transaction_id, $merchant_reference, $error )
* Get the order from the reference.
*
* @param string $merchant_reference The merchant reference from Dintero.
* @return WC_Order
* @return WC_Order|null
*/
public function get_order_from_reference( $merchant_reference ) {
$order_id = $this->get_order_id_from_reference( $merchant_reference );
$order_id = dintero_get_order_id_by_merchant_reference( $merchant_reference );

// Check that we get a order id.
if ( empty( $order_id ) ) {
Expand Down Expand Up @@ -273,30 +273,5 @@ public static function callback_url() {
public static function is_localhost() {
return ( isset( $_SERVER['REMOTE_ADDR'] ) && in_array( $_SERVER['REMOTE_ADDR'], array( '127.0.0.1', '::1' ), true ) || isset( $_SERVER['HTTP_HOST'] ) && 'localhost' === substr( wp_unslash( $_SERVER['HTTP_HOST'] ), 0, 9 ) ); // phpcs:ignore
}


/**
* Get a order id from the merchant reference.
*
* @param string $merchant_reference The merchant reference from dintero.
* @return int
*/
public function get_order_id_from_reference( $merchant_reference ) {
$query_args = array(
'fields' => 'ids',
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
'meta_key' => '_dintero_merchant_reference', // phpcs:ignore WordPress.DB.SlowDBQuery -- Slow DB Query is ok here, we need to limit to our meta key.
'meta_value' => $merchant_reference, // phpcs:ignore WordPress.DB.SlowDBQuery -- Slow DB Query is ok here, we need to limit to our meta key.
);

$order_ids = get_posts( $query_args );

if ( empty( $order_ids ) ) {
return null;
}

return $order_ids[0];
}
}
new Dintero_Checkout_Callback();
28 changes: 2 additions & 26 deletions classes/class-dintero-checkout-confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function handle_error( $error, $order ) {
wc_add_notice( $note, 'error' );
}

Dintero_Checkout_Logger::log( "REDIRECT ERROR [$error]: $note WC order id: $order_id / %s: %s", $note, $order_id );
Dintero_Checkout_Logger::log( "REDIRECT ERROR [$error]: $note WC order id: $order_id" );
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
Expand All @@ -125,7 +125,7 @@ public function handle_error( $error, $order ) {
* @return WC_Order|null On error, null is returned. Otherwise, WC_Order.
*/
public function get_order_from_reference( $merchant_reference ) {
$order_id = $this->get_order_id_from_reference( $merchant_reference );
$order_id = dintero_get_order_id_by_merchant_reference( $merchant_reference );

// Check that we get a order id.
if ( empty( $order_id ) ) {
Expand All @@ -143,29 +143,5 @@ public function get_order_from_reference( $merchant_reference ) {

return $order;
}

/**
* Get a order id from the merchant reference.
*
* @param string $merchant_reference The merchant reference from dintero.
* @return int
*/
public function get_order_id_from_reference( $merchant_reference ) {
$query_args = array(
'fields' => 'ids',
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
'meta_key' => '_dintero_merchant_reference', // phpcs:ignore WordPress.DB.SlowDBQuery -- Slow DB Query is ok here, we need to limit to our meta key.
'meta_value' => $merchant_reference, // phpcs:ignore WordPress.DB.SlowDBQuery -- Slow DB Query is ok here, we need to limit to our meta key.
);

$order_ids = get_posts( $query_args );

if ( empty( $order_ids ) ) {
return null;
}

return $order_ids[0];
}
}
new Dintero_Checkout_Redirect();
67 changes: 49 additions & 18 deletions classes/class-dintero-checkout-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,37 @@ public function __construct() {
/**
* Adds cart_item_key to the order item's meta data to be used as a unique line id. This applies to both embedded and redirect flow.
*/
add_action(
'woocommerce_checkout_create_order_line_item',
function( $item, $cart_item_key ) {
$item->update_meta_data( '_dintero_checkout_line_id', $cart_item_key );
},
10,
2
);
add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'create_order_line_item' ), 10, 2 );

/**
* By default, a custom meta data will be displayed on the order page. Since the meta data _dintero_checkout_line_id is an implementation detail,
* we should hide it on the order page. The meta key has to be prefixed with an underscore (_) to also hide the meta data beyond the order page (e.g., in emails, PDF documents).
*/
add_filter(
'woocommerce_hidden_order_itemmeta',
function( $hidden_meta ) {
$hidden_meta[] = '_dintero_checkout_line_id';
return $hidden_meta;
}
);
add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) );
}

/**
* Add line ID to uniquely identify order item.
*
* @param WC_Order_Item_Product $item
* @param string $cart_item_key
*
* @return void
*/
public function create_order_line_item( $item, $cart_item_key ) {
$item->update_meta_data( '_dintero_checkout_line_id', $cart_item_key );
$item->save();
}

/**
* Hide order line ID on the order page.
*
* @param array $hidden_meta The itemmeta.
* @return array
*/
public function hidden_order_itemmeta( $hidden_meta ) {
$hidden_meta[] = '_dintero_checkout_line_id';
return $hidden_meta;
}

/**
Expand Down Expand Up @@ -101,7 +112,25 @@ public function get_icon() {
* @return boolean
*/
public function is_available() {
return ( 'yes' === $this->enabled );
if ( 'yes' !== $this->enabled ) {
return false;
}

if ( is_wc_endpoint_url( 'order-pay' ) ) {
$order_id = absint( get_query_var( 'order-pay', 0 ) );
$order = wc_get_order( $order_id );
if ( empty( $order ) || 0.0 === floatval( $order->get_total() ) ) {
return false;
}

return true;
}

if ( ! isset( WC()->cart ) ) {
return false;
}

return 0.0 < floatval( WC()->cart->total );
}

/**
Expand Down Expand Up @@ -130,8 +159,9 @@ public function process_payment( $order_id ) {
public function process_embedded_payment( $order_id ) {
$order = wc_get_order( $order_id );
$reference = WC()->session->get( 'dintero_merchant_reference' );
update_post_meta( $order_id, '_dintero_merchant_reference', $reference );
$order->update_meta_data( '_dintero_merchant_reference', $reference );
$order->add_order_note( __( 'Dintero order created with reference ', 'dintero-checkout-for-woocommerce' ) . $reference );
$order->save();

return array(
'result' => 'success',
Expand All @@ -148,7 +178,8 @@ public function process_redirect_payment( $order_id ) {
$order = wc_get_order( $order_id );
$session = Dintero()->api->create_session( $order_id );
$reference = WC()->session->get( 'dintero_merchant_reference' );
update_post_meta( $order_id, '_dintero_merchant_reference', $reference );
$order->update_meta_data( '_dintero_merchant_reference', $reference );
$order->save();

if ( is_wp_error( $session ) ) {
return array(
Expand Down
6 changes: 3 additions & 3 deletions classes/class-dintero-checkout-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Dintero_Checkout_Logger {
* Logs a single event.
*
* @static
* @param array $data The data to log.
* @param array|string $data The data to log.
* @return void
*/
public static function log( $data ) {
Expand All @@ -46,8 +46,8 @@ public static function log( $data ) {
/**
* Formats the log data to prevent json error.
*
* @param string $data Json string of data.
* @return array
* @param array|string $data An array with request/response data or a string.
* @return array|string
*/
public static function format_data( $data ) {
if ( isset( $data['request']['headers']['authorization'] ) ) {
Expand Down
21 changes: 12 additions & 9 deletions classes/class-dintero-checkout-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
exit;
}


/**
* Dintero_Meta_Box
*/
class Dintero_Checkout_Meta_Box {

/**
* Class constructor.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'dintero_checkout_meta_box' ) );
}
Expand All @@ -27,11 +31,11 @@ public function __construct() {
* @return void
*/
public function dintero_checkout_meta_box( $post_type ) {
if ( 'shop_order' === $post_type ) {
$order_id = get_the_ID();
if ( in_array( $post_type, array( 'woocommerce_page_wc-orders', 'shop_order' ), true ) ) {
$order_id = dintero_get_the_ID();
$order = wc_get_order( $order_id );
if ( 'dintero_checkout' === $order->get_payment_method() ) {
add_meta_box( 'dintero_checkout_meta_box', __( 'Dintero Checkout', 'dintero-checkout-for-woocommerce' ), array( $this, 'dintero_meta_box_content' ), 'shop_order', 'side', 'core' );
add_meta_box( 'dintero_checkout_meta_box', __( 'Dintero Checkout', 'dintero-checkout-for-woocommerce' ), array( $this, 'dintero_meta_box_content' ), $post_type, 'side', 'core' );
}
}
}
Expand All @@ -44,10 +48,9 @@ public function dintero_checkout_meta_box( $post_type ) {
* @return void
*/
public function dintero_meta_box_content() {
$order_id = get_the_ID();
$order = wc_get_order( $order_id );
$order = wc_get_order( dintero_get_the_ID() );

if ( ! empty( get_post_meta( $order_id, '_transaction_id', true ) ) ) {
if ( ! empty( $order->get_transaction_id() ) ) {

$dintero_order = Dintero()->api->get_order( $order->get_transaction_id() );

Expand All @@ -72,11 +75,11 @@ public function dintero_meta_box_content() {
* @return void
*/
public function print_content( $dintero_order ) {
$order_id = get_the_ID();
$environment = ! empty( get_post_meta( $order_id, '_wc_dintero_checkout_environment', true ) ) ? get_post_meta( $order_id, '_wc_dintero_checkout_environment', true ) : '';
$order = wc_get_order( dintero_get_the_ID() );
$environment = ! empty( $order->get_meta( '_wc_dintero_checkout_environment' ) ) ? $order->get_meta( '_wc_dintero_checkout_environment' ) : '';

$account_id = trim( get_option( 'woocommerce_dintero_checkout_settings', array( 'account_id' => '' ) )['account_id'] );
$transaction_id = get_post_meta( $order_id, '_dintero_transaction_id', true );
$transaction_id = $order->get_meta( '_dintero_transaction_id' );

/* Remove duplicate words from the payment method type (e.g., swish.swish → Swish). Otherwise, prints as is (e.g., collector.invoice → Collector Invoice). */
$payment_method = implode( ' ', array_unique( explode( ' ', ucwords( str_replace( '.', ' ', $dintero_order['payment_product_type'] ) ) ) ) );
Expand Down
Loading

0 comments on commit 3403e06

Please sign in to comment.