Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wpforms-.../src/Integrat.../Stripe/Api/Webhooks
File: ChargeRefunded.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Integrations\Stripe\Api\Webhooks;
[2] Fix | Delete
[3] Fix | Delete
use RuntimeException;
[4] Fix | Delete
use WPForms\Integrations\Stripe\Helpers;
[5] Fix | Delete
use WPForms\Db\Payments\UpdateHelpers;
[6] Fix | Delete
use WPForms\Integrations\Stripe\Api\PaymentIntents;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Webhook charge.refunded class.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 1.8.4
[12] Fix | Delete
*/
[13] Fix | Delete
class ChargeRefunded extends Base {
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Decimals amount.
[17] Fix | Delete
*
[18] Fix | Delete
* @since 1.8.4
[19] Fix | Delete
*
[20] Fix | Delete
* @var int
[21] Fix | Delete
*/
[22] Fix | Delete
private $decimals_amount;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Handle the Webhook's data.
[26] Fix | Delete
*
[27] Fix | Delete
* Save refunded amount in the payment meta with key refunded_amount.
[28] Fix | Delete
* Update payment status to 'partrefund' or 'refunded' if refunded amount is equal to total amount.
[29] Fix | Delete
*
[30] Fix | Delete
* @since 1.8.4
[31] Fix | Delete
*
[32] Fix | Delete
* @throws RuntimeException If payment not found or not updated.
[33] Fix | Delete
*
[34] Fix | Delete
* @return bool
[35] Fix | Delete
*/
[36] Fix | Delete
public function handle() {
[37] Fix | Delete
[38] Fix | Delete
$this->set_payment();
[39] Fix | Delete
[40] Fix | Delete
if ( ! $this->db_payment ) {
[41] Fix | Delete
return false;
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
$currency = strtoupper( $this->data->currency );
[45] Fix | Delete
$this->decimals_amount = Helpers::get_decimals_amount( $currency );
[46] Fix | Delete
[47] Fix | Delete
$charge = ( new PaymentIntents() )->get_charge( $this->data->id );
[48] Fix | Delete
[49] Fix | Delete
if ( isset( $charge->refunds->data[0]->metadata->refunded_by ) && $charge->refunds->data[0]->metadata->refunded_by === 'wpforms_dashboard' ) {
[50] Fix | Delete
return false;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
if ( ! $this->is_previous_statuses_matched() ) {
[54] Fix | Delete
return false;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
// We need to format amount since it doesn't contain decimals, e.g. 525 instead of 5.25.
[58] Fix | Delete
$refunded_amount = $this->data->amount_refunded / $this->decimals_amount;
[59] Fix | Delete
$last_refund_formatted = wpforms_format_amount( $this->get_last_refund_amount() / $this->decimals_amount, true, $currency );
[60] Fix | Delete
$log = sprintf( 'Stripe payment refunded from the Stripe dashboard. Refunded amount: %1$s.', $last_refund_formatted );
[61] Fix | Delete
[62] Fix | Delete
if ( ! UpdateHelpers::refund_payment( $this->db_payment, $refunded_amount, $log ) ) {
[63] Fix | Delete
throw new RuntimeException( 'Payment not updated' );
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
return true;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Get last refund amount.
[71] Fix | Delete
*
[72] Fix | Delete
* @since 1.8.4
[73] Fix | Delete
*
[74] Fix | Delete
* @return int Last refund amount in cents.
[75] Fix | Delete
*/
[76] Fix | Delete
private function get_last_refund_amount() {
[77] Fix | Delete
[78] Fix | Delete
if ( isset( $this->data->refunds->data[0]->amount ) ) {
[79] Fix | Delete
return $this->data->refunds->data[0]->amount;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
if ( isset( $this->data->previous_attributes->amount_refunded ) ) {
[83] Fix | Delete
return $this->data->amount_refunded - $this->data->previous_attributes->amount_refunded;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
// get the last refunded from DB.
[87] Fix | Delete
$previous_refund_in_db = wpforms()->get( 'payment_meta' )->get_last_by(
[88] Fix | Delete
'refunded_amount',
[89] Fix | Delete
$this->db_payment->id
[90] Fix | Delete
);
[91] Fix | Delete
$previous_refund = $previous_refund_in_db ? $previous_refund_in_db->meta_value : 0;
[92] Fix | Delete
[93] Fix | Delete
return $this->data->amount_refunded - ( $previous_refund * $this->decimals_amount );
[94] Fix | Delete
}
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function