Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/wpforms-.../src/Integrat.../Square/Api/Webhooks
File: PaymentUpdated.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\Queries;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Webhook payment.updated class.
[8] Fix | Delete
* Set the status to 'completed' if payment is paid.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 1.9.5
[11] Fix | Delete
*/
[12] Fix | Delete
class PaymentUpdated extends Base {
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Invoice object.
[16] Fix | Delete
*
[17] Fix | Delete
* @since 1.9.5
[18] Fix | Delete
*
[19] Fix | Delete
* @var Invoice|null
[20] Fix | Delete
*/
[21] Fix | Delete
private $invoice;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Handle the Webhook's data.
[25] Fix | Delete
*
[26] Fix | Delete
* @since 1.9.5
[27] Fix | Delete
*
[28] Fix | Delete
* @throws RuntimeException If payment isn't found or not updated.
[29] Fix | Delete
*
[30] Fix | Delete
* @return bool
[31] Fix | Delete
*/
[32] Fix | Delete
public function handle(): bool {
[33] Fix | Delete
[34] Fix | Delete
$order_id = $this->data->object->payment->order_id ?? '';
[35] Fix | Delete
$this->invoice = $this->api->get_invoice_by_order_id( $order_id );
[36] Fix | Delete
$subscription_id = $this->invoice ? $this->invoice->getSubscriptionId() : '';
[37] Fix | Delete
[38] Fix | Delete
// If a subscription ID exists, process the subscription-specific logic.
[39] Fix | Delete
if ( $subscription_id ) {
[40] Fix | Delete
$this->update_subscription_payment( $subscription_id );
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
$this->set_payment();
[44] Fix | Delete
[45] Fix | Delete
if ( $this->db_payment === null ) {
[46] Fix | Delete
throw new RuntimeException( 'Payment Update Event: Payment has not been found and set.' );
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
// Update payment method details to keep them up to date.
[50] Fix | Delete
if ( isset( $this->data->object->payment ) && $this->data->object->payment !== null ) {
[51] Fix | Delete
$this->update_payment_method_details( $this->db_payment->id, $this->data->object->payment );
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
// Update total refunded amount if set.
[55] Fix | Delete
if ( ! empty( $this->data->object->payment->refunded_money ) ) {
[56] Fix | Delete
$this->update_total_refund( $this->db_payment->id, $this->data->object->payment->refunded_money );
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
if ( $this->db_payment->status !== 'processed' || $this->data->object->payment->status !== 'COMPLETED' ) {
[60] Fix | Delete
return false;
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
$currency = strtoupper( $this->data->object->payment->total_money->currency );
[64] Fix | Delete
$db_amount = wpforms_format_amount( $this->db_payment->total_amount );
[65] Fix | Delete
$amount = wpforms_format_amount( $this->data->object->payment->total_money->amount / wpforms_get_currency_multiplier( $currency ) );
[66] Fix | Delete
[67] Fix | Delete
if ( $amount !== $db_amount ) {
[68] Fix | Delete
return false;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
$updated_payment = wpforms()->obj( 'payment' )->update(
[72] Fix | Delete
$this->db_payment->id,
[73] Fix | Delete
[
[74] Fix | Delete
'status' => 'completed',
[75] Fix | Delete
'date_updated_gmt' => gmdate( 'Y-m-d H:i:s' ),
[76] Fix | Delete
]
[77] Fix | Delete
);
[78] Fix | Delete
[79] Fix | Delete
if ( ! $updated_payment ) {
[80] Fix | Delete
throw new RuntimeException( 'Payment not updated' );
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
wpforms()->obj( 'payment_meta' )->add_log(
[84] Fix | Delete
$this->db_payment->id,
[85] Fix | Delete
'Square payment was completed.'
[86] Fix | Delete
);
[87] Fix | Delete
[88] Fix | Delete
return true;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Update subscription payment.
[93] Fix | Delete
*
[94] Fix | Delete
* @since 1.9.5
[95] Fix | Delete
*
[96] Fix | Delete
* @param string $subscription_id Subscription ID.
[97] Fix | Delete
*
[98] Fix | Delete
* @return bool
[99] Fix | Delete
*/
[100] Fix | Delete
private function update_subscription_payment( string $subscription_id ): bool {
[101] Fix | Delete
[102] Fix | Delete
// If this is the first invoice in the subscription, do not create a renewal.
[103] Fix | Delete
if ( $this->is_initial_invoice_for_subscription( $subscription_id ) ) {
[104] Fix | Delete
return false;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
// Retrieve the renewal record from the database.
[108] Fix | Delete
$db_renewal = ( new Queries() )->get_renewal_by_invoice_id( $this->invoice->getId() );
[109] Fix | Delete
[110] Fix | Delete
if ( is_null( $db_renewal ) ) {
[111] Fix | Delete
return false; // The newest renewal not found.
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
// Check if the renewal payment is already completed.
[115] Fix | Delete
if ( $db_renewal->status === 'completed' ) {
[116] Fix | Delete
return true;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
// Retrieve the payment requests from the invoice.
[120] Fix | Delete
$payment_requests = $this->invoice->getPaymentRequests();
[121] Fix | Delete
[122] Fix | Delete
if ( empty( $payment_requests ) ) {
[123] Fix | Delete
return false;
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
// Use the first payment request to get the final paid amount.
[127] Fix | Delete
$total_completed = $payment_requests[0]->getTotalCompletedAmountMoney();
[128] Fix | Delete
$currency = strtoupper( $total_completed->getCurrency() );
[129] Fix | Delete
$amount = $total_completed->getAmount() / wpforms_get_currency_multiplier( $currency );
[130] Fix | Delete
[131] Fix | Delete
// Retrieve the transaction ID using the subscription ID.
[132] Fix | Delete
$transaction_id = $this->get_latest_subscription_transaction_id( $subscription_id );
[133] Fix | Delete
[134] Fix | Delete
if ( empty( $transaction_id ) ) {
[135] Fix | Delete
$transaction_id = '';
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
// Update the renewal payment with the final amount and transaction ID.
[139] Fix | Delete
wpforms()->obj( 'payment' )->update(
[140] Fix | Delete
$db_renewal->id,
[141] Fix | Delete
[
[142] Fix | Delete
'total_amount' => $amount,
[143] Fix | Delete
'subtotal_amount' => $amount,
[144] Fix | Delete
'status' => 'completed',
[145] Fix | Delete
'transaction_id' => $transaction_id,
[146] Fix | Delete
]
[147] Fix | Delete
);
[148] Fix | Delete
[149] Fix | Delete
// Copy additional meta data from the transaction details.
[150] Fix | Delete
$this->copy_meta_from_transaction_details( (int) $db_renewal->id, $transaction_id );
[151] Fix | Delete
[152] Fix | Delete
wpforms()->obj( 'payment_meta' )->add_log(
[153] Fix | Delete
$db_renewal->id,
[154] Fix | Delete
sprintf(
[155] Fix | Delete
'Square renewal was successfully paid. (Payment ID: %1$s)',
[156] Fix | Delete
$transaction_id
[157] Fix | Delete
)
[158] Fix | Delete
);
[159] Fix | Delete
[160] Fix | Delete
return true;
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Copy meta from transaction.
[165] Fix | Delete
*
[166] Fix | Delete
* @since 1.9.5
[167] Fix | Delete
*
[168] Fix | Delete
* @param int $renewal_id Renewal ID.
[169] Fix | Delete
* @param string $transaction_id Transaction ID.
[170] Fix | Delete
*/
[171] Fix | Delete
private function copy_meta_from_transaction_details( int $renewal_id, string $transaction_id ) {
[172] Fix | Delete
[173] Fix | Delete
$card_details = $this->api->get_card_details_from_transaction_id( $transaction_id );
[174] Fix | Delete
[175] Fix | Delete
if ( ! $card_details ) {
[176] Fix | Delete
return;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
$this->update_payment_method_details( $renewal_id, $card_details );
[180] Fix | Delete
}
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function