Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/wpforms-.../src/Integrat.../Square/Api/Webhooks
File: RefundUpdated.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Integrations\Square\Api\Webhooks;
[2] Fix | Delete
[3] Fix | Delete
use RuntimeException;
[4] Fix | Delete
use WPForms\Db\Payments\UpdateHelpers;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Webhook refund.updated class.
[8] Fix | Delete
*
[9] Fix | Delete
* @since 1.9.5
[10] Fix | Delete
*/
[11] Fix | Delete
class RefundUpdated extends Base {
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Handle the Webhook's data.
[15] Fix | Delete
*
[16] Fix | Delete
* Save refunded amount in the payment meta with key refunded_amount.
[17] Fix | Delete
* Update payment status to 'partrefund' or 'refunded' if refunded amount is equal to the total amount.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 1.9.5
[20] Fix | Delete
*
[21] Fix | Delete
* @throws RuntimeException If payment isn't updated.
[22] Fix | Delete
*
[23] Fix | Delete
* @return bool
[24] Fix | Delete
*/
[25] Fix | Delete
public function handle(): bool {
[26] Fix | Delete
[27] Fix | Delete
$this->set_payment();
[28] Fix | Delete
[29] Fix | Delete
if ( $this->db_payment === null ) {
[30] Fix | Delete
throw new RuntimeException( 'Refund Update Event: Payment has not been found and set.' );
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
// Perform refund only if it's allowed.
[34] Fix | Delete
if ( ! $this->is_refund_allowed() ) {
[35] Fix | Delete
return false;
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
$currency = strtoupper( $this->data->object->refund->amount_money->currency );
[39] Fix | Delete
$decimals_amount = wpforms_get_currency_multiplier( $currency );
[40] Fix | Delete
[41] Fix | Delete
// We need to format the amount since it doesn't contain decimals, e.g., 525 instead of 5.25.
[42] Fix | Delete
$refunded_amount = ( $decimals_amount !== 0 ) ? ( $this->data->object->refund->amount_money->amount / $decimals_amount ) : 0;
[43] Fix | Delete
$refunded_amount_formatted = wpforms_format_amount( $refunded_amount, true, $currency );
[44] Fix | Delete
$log = sprintf( 'Square payment refunded from the Square dashboard. Refunded amount: %1$s.', $refunded_amount_formatted );
[45] Fix | Delete
$total_amount_refund = wpforms()->obj( 'payment_meta' )->get_single( $this->db_payment->id, 'total_refunded_amount' );
[46] Fix | Delete
[47] Fix | Delete
if ( empty( $total_amount_refund ) ) {
[48] Fix | Delete
$total_amount_refund = $refunded_amount;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
if ( ! UpdateHelpers::refund_payment( $this->db_payment, $total_amount_refund, $log ) ) {
[52] Fix | Delete
/* translators: %s - transaction id. */
[53] Fix | Delete
$log = sprintf( __( 'Payment for transaction %s was not updated.', 'wpforms-lite' ), $this->db_payment->transaction_id );
[54] Fix | Delete
[55] Fix | Delete
throw new RuntimeException( esc_html( $log ) );
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
return true;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Check if refund is allowed.
[63] Fix | Delete
*
[64] Fix | Delete
* @since 1.9.5
[65] Fix | Delete
*
[66] Fix | Delete
* @return bool
[67] Fix | Delete
*/
[68] Fix | Delete
private function is_refund_allowed(): bool {
[69] Fix | Delete
[70] Fix | Delete
if ( ! $this->db_payment ) {
[71] Fix | Delete
return false;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
// Do not track uncompleted refunds.
[75] Fix | Delete
if ( $this->data->object->refund->status !== 'COMPLETED' ) {
[76] Fix | Delete
return false;
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
// Do not track refunds that were not requested by the customer.
[80] Fix | Delete
if ( ! empty( $this->data->object->refund->reason ) && $this->data->object->refund->reason !== 'Requested by customer' ) {
[81] Fix | Delete
return false;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
// Square sends two webhooks for a refund with the same COMPLETED statuses,
[85] Fix | Delete
// but the final is the one with the fee included.
[86] Fix | Delete
if ( ! isset( $this->data->object->refund->processing_fee ) ) {
[87] Fix | Delete
return false;
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
return true;
[91] Fix | Delete
}
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function