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.../public_h.../wp-conte.../plugins/wpforms-.../src/Integrat.../Stripe/Api/Webhooks
File: InvoiceCreated.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 Exception;
[5] Fix | Delete
use WPForms\Vendor\Stripe\Invoice;
[6] Fix | Delete
use WPForms\Integrations\Stripe\Helpers;
[7] Fix | Delete
use WPForms\Db\Payments\Queries;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Webhook invoice.created class.
[11] Fix | Delete
*
[12] Fix | Delete
* @since 1.8.4
[13] Fix | Delete
*/
[14] Fix | Delete
class InvoiceCreated extends Base {
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Handle invoice.created webhook.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 1.8.4
[20] Fix | Delete
*
[21] Fix | Delete
* @throws RuntimeException If original subscription not found or not updated.
[22] Fix | Delete
*
[23] Fix | Delete
* @return bool
[24] Fix | Delete
*/
[25] Fix | Delete
public function handle() {
[26] Fix | Delete
[27] Fix | Delete
if ( ! isset( $this->data->billing_reason ) || $this->data->billing_reason !== 'subscription_cycle' ) {
[28] Fix | Delete
return false; // Webhook handler for Invoice.Create supports only billing_reason = subscription_cycle.
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
$original_subscription = ( new Queries() )->get_subscription( $this->data->subscription );
[32] Fix | Delete
[33] Fix | Delete
if ( is_null( $original_subscription ) ) {
[34] Fix | Delete
return false; // Original subscription not found.
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
$renewal = ( new Queries() )->get_renewal_by_invoice_id( $this->data->id );
[38] Fix | Delete
[39] Fix | Delete
if ( ! is_null( $renewal ) ) {
[40] Fix | Delete
return false; // Renewal already exists.
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
$renewal_id = $this->insert_renewal( $original_subscription );
[44] Fix | Delete
[45] Fix | Delete
if ( ! $renewal_id ) {
[46] Fix | Delete
throw new RuntimeException( 'Subscription renewal not saved in database' );
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
$this->insert_renewal_meta( $renewal_id, $original_subscription );
[50] Fix | Delete
[51] Fix | Delete
wpforms()->get( 'payment_meta' )->add_log(
[52] Fix | Delete
$renewal_id,
[53] Fix | Delete
sprintf(
[54] Fix | Delete
'Stripe renewal was created (Invoice ID: %1$s).',
[55] Fix | Delete
$this->data->id
[56] Fix | Delete
)
[57] Fix | Delete
);
[58] Fix | Delete
[59] Fix | Delete
$this->finalize_invoice();
[60] Fix | Delete
[61] Fix | Delete
return true;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Insert renewal.
[66] Fix | Delete
*
[67] Fix | Delete
* @since 1.8.4
[68] Fix | Delete
*
[69] Fix | Delete
* @param object $original_subscription Original subscription.
[70] Fix | Delete
*
[71] Fix | Delete
* @return int|false
[72] Fix | Delete
*/
[73] Fix | Delete
private function insert_renewal( $original_subscription ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
[74] Fix | Delete
[75] Fix | Delete
$currency = strtoupper( $this->data->currency );
[76] Fix | Delete
$amount = $this->data->amount_due / Helpers::get_decimals_amount( $currency );
[77] Fix | Delete
[78] Fix | Delete
return wpforms()->get( 'payment' )->add(
[79] Fix | Delete
[
[80] Fix | Delete
'mode' => $original_subscription->mode,
[81] Fix | Delete
'form_id' => isset( $original_subscription->form_id ) ? $original_subscription->form_id : 0,
[82] Fix | Delete
'entry_id' => isset( $original_subscription->entry_id ) ? $original_subscription->entry_id : 0,
[83] Fix | Delete
'status' => 'pending',
[84] Fix | Delete
'type' => 'renewal',
[85] Fix | Delete
'gateway' => 'stripe',
[86] Fix | Delete
'title' => $original_subscription->title,
[87] Fix | Delete
'subtotal_amount' => $amount,
[88] Fix | Delete
'total_amount' => $amount,
[89] Fix | Delete
'currency' => $currency,
[90] Fix | Delete
'transaction_id' => '',
[91] Fix | Delete
'subscription_id' => $original_subscription->subscription_id,
[92] Fix | Delete
'customer_id' => $original_subscription->customer_id,
[93] Fix | Delete
'date_created_gmt' => gmdate( 'Y-m-d H:i:s', $this->data->lines->data[0]->period->start ),
[94] Fix | Delete
'date_updated_gmt' => gmdate( 'Y-m-d H:i:s' ),
[95] Fix | Delete
]
[96] Fix | Delete
);
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Insert renewal meta.
[101] Fix | Delete
*
[102] Fix | Delete
* @since 1.8.4
[103] Fix | Delete
*
[104] Fix | Delete
* @param int $renewal_id Renewal ID.
[105] Fix | Delete
* @param object $original_subscription Original subscription.
[106] Fix | Delete
*/
[107] Fix | Delete
private function insert_renewal_meta( $renewal_id, $original_subscription ) {
[108] Fix | Delete
[109] Fix | Delete
$meta = $this->copy_meta_from_db( $original_subscription->id );
[110] Fix | Delete
[111] Fix | Delete
$meta['invoice_id'] = $this->data->id;
[112] Fix | Delete
$meta['customer_email'] = isset( $this->data->customer_email ) ? $this->data->customer_email : '';
[113] Fix | Delete
[114] Fix | Delete
wpforms()->get( 'payment_meta' )->bulk_add( $renewal_id, $meta );
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
/**
[118] Fix | Delete
* Copy meta from original subscription.
[119] Fix | Delete
*
[120] Fix | Delete
* @since 1.8.4
[121] Fix | Delete
*
[122] Fix | Delete
* @param int $original_subscription_id Original subscription ID.
[123] Fix | Delete
*
[124] Fix | Delete
* @return array
[125] Fix | Delete
*/
[126] Fix | Delete
private function copy_meta_from_db( $original_subscription_id ) {
[127] Fix | Delete
[128] Fix | Delete
$all_meta = wpforms()->get( 'payment_meta' )->get_all( $original_subscription_id );
[129] Fix | Delete
$db_meta_keys = [
[130] Fix | Delete
'fields',
[131] Fix | Delete
'subscription_period',
[132] Fix | Delete
'coupon_value',
[133] Fix | Delete
'coupon_info',
[134] Fix | Delete
'coupon_id',
[135] Fix | Delete
];
[136] Fix | Delete
$meta = [];
[137] Fix | Delete
[138] Fix | Delete
foreach ( $db_meta_keys as $key ) {
[139] Fix | Delete
if ( isset( $all_meta[ $key ]->value ) ) {
[140] Fix | Delete
$meta[ $key ] = $all_meta[ $key ]->value;
[141] Fix | Delete
}
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
return $meta;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Finalize invoice.
[149] Fix | Delete
*
[150] Fix | Delete
* @since 1.8.4
[151] Fix | Delete
*
[152] Fix | Delete
* @throws RuntimeException If invoice not finalized.
[153] Fix | Delete
*/
[154] Fix | Delete
private function finalize_invoice() {
[155] Fix | Delete
[156] Fix | Delete
try {
[157] Fix | Delete
$invoice = new Invoice();
[158] Fix | Delete
$invoice = $invoice->retrieve( $this->data->id, Helpers::get_auth_opts() );
[159] Fix | Delete
[160] Fix | Delete
if ( empty( $invoice->finalized_at ) ) {
[161] Fix | Delete
$invoice->finalizeInvoice();
[162] Fix | Delete
}
[163] Fix | Delete
} catch ( Exception $e ) {
[164] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[165] Fix | Delete
throw new RuntimeException( $e->getMessage() );
[166] Fix | Delete
}
[167] Fix | Delete
}
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function