Skip to content

Commit

Permalink
Merge pull request #145 from dimaspolohov/master
Browse files Browse the repository at this point in the history
Settle price and all lines settle dynamically
  • Loading branch information
markusbrunke authored Jul 6, 2022
2 parents 6aa0f51 + e746a4f commit 66235d0
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 43 deletions.
1 change: 1 addition & 0 deletions Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The Reepay plugin extends WooCommerce allowing you to take payments on your stor
See installation guide right here: https://intercom.help/reepay/reepay-plugins/woocommerce-plugin

== Changelog ==
v 1.4.22 - Settle price and all lines settle dynamically
v 1.4.21 - Fix shipping settle
v 1.4.20 - Change status message, add card token in order custom fields, fix display price refund
v 1.4.19 - Bugfixing
Expand Down
7 changes: 6 additions & 1 deletion includes/abstracts/abstract-wc-gateway-reepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,12 @@ public function validate_fields() {
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
$token_id = isset( $_POST['wc-' . $this->id . '-payment-token'] ) ? wc_clean( $_POST['wc-' . $this->id . '-payment-token'] ) : 'new';
$maybe_save_card = isset( $_POST['wc-' . $this->id . '-new-payment-method'] ) && (bool) $_POST['wc-' . $this->id . '-new-payment-method'];

if ( isset( $_POST[ 'wc-' . $this->id . '-new-payment-method' ] ) ) {
$maybe_save_card = (bool) $_POST[ 'wc-' . $this->id . '-new-payment-method' ];
} else {
$maybe_save_card = wcs_cart_have_subscription();
}

if ( 'yes' !== $this->save_cc ) {
$token_id = 'new';
Expand Down
129 changes: 106 additions & 23 deletions includes/class-wc-gateway-reepay-capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function __construct() {
add_action('woocommerce_after_order_fee_item_name', array($this, 'add_item_capture_button'), 10, 3);
add_action('woocommerce_order_status_changed', array($this, 'capture_full_order'), 10, 4);
add_action('admin_init', array($this, 'process_item_capture'));
add_action('woocommerce_order_item_add_action_buttons', array($this, 'capture_full_order_button'), 10, 1);
}

public function capture_full_order( $order_id, $this_status_transition_from, $this_status_transition_to, $instance){
Expand Down Expand Up @@ -51,29 +52,81 @@ public function settle_item($item, $order){
}
}

public function add_item_capture_button($item_id, $item, $product){
$order_id = wc_get_order_id_by_order_item_id( $item_id );
$order = wc_get_order( $order_id );
public function check_allow_capture($order){
$payment_method = $order->get_payment_method();

if(strpos($payment_method, 'reepay') === false){
return;
return false;
}

$gateway = rp_get_payment_method( $order );

$invoice_data = $gateway->api->get_invoice_data($order);

if ( is_wp_error( $invoice_data ) ) {
echo __( 'Invoice not found', 'reepay-checkout-gateway' );
return;
return false;
}

if($invoice_data['authorized_amount'] > $invoice_data['settled_amount']){
return true;
}

return true;
}

public function get_no_settled_amount($order){
$amount = 0;
foreach ( $order->get_items() as $item_key => $item ) {
$settled = $item->get_meta('settled');
if(empty($settled)) {
$price = $this->get_item_price($item, $order);
if(!empty($price)){
$amount += $price['with_tax'];
}
}
}

foreach( $order->get_items( 'shipping' ) as $item_id => $item ){
$settled = $item->get_meta('settled');
if(empty($settled)) {
$price = $this->get_item_price($item, $order);
if(!empty($price)){
$amount += $price['with_tax'];
}
}
}

return $amount;
}

public function capture_full_order_button($order){

if($this->check_allow_capture($order)){
$amount = $this->get_no_settled_amount($order);
if($amount > 0){
$amount = number_format(round($amount, 2), 2, '.', '');
echo '<button type="submit" class="button save_order button-primary capture-item-button" name="all_items_capture" value="'.$order->get_id().'">
'.__( 'Capture '. $order->get_currency() . $amount, 'reepay-checkout-gateway' ).'
</button>';
}
}
}

public function add_item_capture_button($item_id, $item, $product){
$order_id = wc_get_order_id_by_order_item_id( $item_id );
$order = wc_get_order( $order_id );

$settled = $item->get_meta('settled');
$data = $item->get_data();

if(empty($settled) && floatval($data['total']) > 0 && $invoice_data['authorized_amount'] > $invoice_data['settled_amount']){
if(empty($settled) && floatval($data['total']) > 0 && $this->check_allow_capture($order)){
$order_item = WC_Order_Factory::get_order_item( $item_id );
$price = $this->get_item_price($order_item, $order);
$unitPrice = number_format(round($price['with_tax'], 2), 2, '.', '');

echo '<button type="submit" class="button save_order button-primary capture-item-button" name="line_item_capture" value="'.$item_id.'">
'.__( 'Capture', 'reepay-checkout-gateway' ).'
'.__( 'Capture '. $order->get_currency() . $unitPrice, 'reepay-checkout-gateway' ).'
</button>';
}
}
Expand All @@ -88,16 +141,58 @@ public function process_item_capture(){
$this->settle_item($item, $order);
}
}

if(isset($_POST['all_items_capture']) && isset($_POST['post_type']) && isset($_POST['post_ID'])){
if($_POST['post_type'] == 'shop_order'){
$order = wc_get_order( $_POST['post_ID'] );

$payment_method = $order->get_payment_method();

if(strpos($payment_method, 'reepay') === false){
return;
}

foreach ( $order->get_items() as $item_key => $item ) {
$this->settle_item($item, $order);
}

foreach( $order->get_items( 'shipping' ) as $item_id => $item ){
$this->settle_item($item, $order);
}
}
}
}

public function get_item_data($order_item, $order){
$prices_incl_tax = wc_prices_include_tax();

$price = $this->get_item_price($order_item, $order);
var_dump($price);
$tax = $price['with_tax'] - $price['original'];
$taxPercent = ($tax > 0) ? round(100 / ($price['original'] / $tax)) : 0;
$unitPrice = round(($prices_incl_tax ? $price['with_tax'] : $price['original']) / $order_item->get_quantity(), 2);

$item_data = [
'ordertext' => $order_item->get_name(),
'quantity' => $order_item->get_quantity(),
'amount' => rp_prepare_amount($unitPrice, $order->get_currency()),
'vat' => round($taxPercent / 100, 2),
'amount_incl_vat' => $prices_incl_tax
];

return $item_data;
}


public function get_item_price($order_item, $order){
if(is_object($order_item) && get_class($order_item) == 'WC_Order_Item_Product'){
$price = $order->get_line_subtotal( $order_item, false, false );
$price['original'] = floatval($order->get_line_subtotal( $order_item, false, false ));
if($order_item->get_subtotal() !== $order_item->get_total()){
$discount = $order_item->get_subtotal() - $order_item->get_total();
$price['original'] = $price['original'] - $discount;
}
}else{
$price = $order->get_line_total( $order_item, false, false );
$price['original'] = floatval($order->get_line_total( $order_item, false, false ));
}


Expand All @@ -115,21 +210,9 @@ public function get_item_data($order_item, $order){
}
}

$priceWithTax = floatval($price) + $res_tax;

$tax = $priceWithTax - $price;
$taxPercent = ( $tax > 0 ) ? round( 100 / ( $price / $tax ) ) : 0;
$unitPrice = round( ( $prices_incl_tax ? $priceWithTax : $price ) / $order_item->get_quantity(), 2 );
$price['with_tax'] = $price['original'] + $res_tax;

$item_data = array(
'ordertext' => $order_item->get_name(),
'quantity' => $order_item->get_quantity(),
'amount' => rp_prepare_amount( $unitPrice, $order->get_currency() ),
'vat' => round($taxPercent / 100, 2),
'amount_incl_vat' => $prices_incl_tax
);

return $item_data;
return $price;
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-reepay-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function request( $method, $url, $params = array() ) {
}

$data = json_decode( $body, true );
if ( JSON_ERROR_NONE === json_last_error() && isset( $data['code'] ) ) {
if ( JSON_ERROR_NONE === json_last_error() && isset( $data['code'] ) && !empty($data['message']) ) {
return new WP_Error( $data['code'], sprintf( __( 'API Error: %s - %s.', 'reepay-checkout-gateway' ), $data['error'], $data['message'] ) );
}

Expand Down
18 changes: 8 additions & 10 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,17 @@ function wcs_is_payment_change() {
* @return bool
*/
function wcs_cart_have_subscription() {
if ( ! class_exists( 'WC_Subscriptions_Product' ) ) {
return false;
}

// Check is Recurring Payment
$cart = WC()->cart->get_cart();
foreach ( $cart as $key => $item ) {
if ( is_object( $item['data'] ) && WC_Subscriptions_Product::is_subscription( $item['data'] ) ) {
return true;
if ( class_exists( 'WC_Subscriptions_Product' ) ) {
// Check is Recurring Payment
$cart = WC()->cart->get_cart();
foreach ( $cart as $key => $item ) {
if ( is_object( $item['data'] ) && WC_Subscriptions_Product::is_subscription( $item['data'] ) ) {
return true;
}
}
}

return false;
return apply_filters( 'wcs_cart_have_subscription', false );
}
}

Expand Down
2 changes: 1 addition & 1 deletion reepay-woocommerce-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: Provides a Payment Gateway through Reepay for WooCommerce.
* Author: reepay
* Author URI: http://reepay.com
* Version: 1.4.21
* Version: 1.4.22
* Text Domain: reepay-checkout-gateway
* Domain Path: /languages
* WC requires at least: 3.0.0
Expand Down
7 changes: 0 additions & 7 deletions templates/admin/metabox-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@
</span>
</li>
<li style='font-size: xx-small'>&nbsp;</li>
<?php if ( $order_data['settled_amount'] == 0 && ! in_array( $order_data['state'], array( 'cancelled', 'created') ) && !$order_is_cancelled ): ?>
<li class="reepay-full-width">
<a class="button button-primary" data-action="reepay_capture" id="reepay_capture" data-nonce="<?php echo wp_create_nonce( 'reepay' ); ?>" data-order-id="<?php echo $order_id; ?>" data-confirm="<?php echo __( 'You are about to CAPTURE this payment', 'reepay-checkout-gateway' ); ?>">
<?php echo sprintf( __( 'Capture Full Amount (%s)', 'reepay-checkout-gateway' ), rp_make_initial_amount( $order_data['authorized_amount'], $order_data['currency'] ) .' '.get_woocommerce_currency_symbol() ); ?>
</a>
</li>
<?php endif; ?>

<?php if ( $order_data['settled_amount'] == 0 && ! in_array( $order_data['state'], array( 'cancelled', 'created') ) && ! $order_is_cancelled ): ?>
<li class="reepay-full-width">
Expand Down

0 comments on commit 66235d0

Please sign in to comment.