From a15081ac671cf4f2600c1df0e9142431cdff1879 Mon Sep 17 00:00:00 2001 From: wrongecho Date: Sun, 12 Jan 2025 20:56:38 +0000 Subject: [PATCH 1/7] Initial add Stripe Auto-payment with saved card --- database_updates.php | 12 +- database_version.php | 2 +- db.sql | 11 ++ js/autopay_setup_stripe.js | 23 ++++ portal/autopay.php | 126 +++++++++++++++++++ portal/portal_header.php | 41 +++--- portal/portal_post.php | 249 ++++++++++++++++++++++++++++++++++++- 7 files changed, 440 insertions(+), 24 deletions(-) create mode 100644 js/autopay_setup_stripe.js create mode 100644 portal/autopay.php diff --git a/database_updates.php b/database_updates.php index c29c68ed..6ad35610 100644 --- a/database_updates.php +++ b/database_updates.php @@ -2404,10 +2404,16 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) { mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.7.5'"); } - // if (CURRENT_DATABASE_VERSION == '1.7.5') { - // // Insert queries here required to update to DB version 1.7.6 + if (CURRENT_DATABASE_VERSION == '1.7.5') { + mysqli_query($mysqli, "CREATE TABLE `client_stripe` (`client_id` INT(11) NOT NULL, `stripe_id` VARCHAR(255) NOT NULL, `stripe_pm` varchar(255) NULL) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci; "); + + mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.7.6'"); + } + + // if (CURRENT_DATABASE_VERSION == '1.7.6') { + // // Insert queries here required to update to DB version 1.7.7 // // Then, update the database to the next sequential version - // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.7.6'"); + // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.7.7'"); // } } else { diff --git a/database_version.php b/database_version.php index 589a6c56..61ca815b 100644 --- a/database_version.php +++ b/database_version.php @@ -5,4 +5,4 @@ * It is used in conjunction with database_updates.php */ -DEFINE("LATEST_DATABASE_VERSION", "1.7.5"); +DEFINE("LATEST_DATABASE_VERSION", "1.7.6"); diff --git a/db.sql b/db.sql index 664915d8..6001fb86 100644 --- a/db.sql +++ b/db.sql @@ -342,6 +342,17 @@ CREATE TABLE `client_notes` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `client_stripe` +-- + +DROP TABLE IF EXISTS `client_stripe`; +CREATE TABLE IF NOT EXISTS `client_stripe` ( + `client_id` int(11) NOT NULL, + `stripe_id` varchar(255) NOT NULL, + `stripe_pm` varchar(255) NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + -- -- Table structure for table `client_tags` -- diff --git a/js/autopay_setup_stripe.js b/js/autopay_setup_stripe.js new file mode 100644 index 00000000..d0787f1b --- /dev/null +++ b/js/autopay_setup_stripe.js @@ -0,0 +1,23 @@ +// Initialize Stripe.js +const stripe = Stripe('pk_test_51OTpmkHRGkC845Mqz0zM2A1pjnnXwOyD5tyPzWnRwVthuizNjuBIjoYgMHBMLQBuegrUXQpIyX4yr1fNMo7QzCs500bBnFJgEr'); + +initialize(); + +// Fetch Checkout Session and retrieve the client secret +async function initialize() { + const fetchClientSecret = async () => { + const response = await fetch("/portal/portal_post.php?create_stripe_checkout", { + method: "POST", + }); + const { clientSecret } = await response.json(); + return clientSecret; + }; + + // Initialize Checkout + const checkout = await stripe.initEmbeddedCheckout({ + fetchClientSecret, + }); + + // Mount Checkout + checkout.mount('#checkout'); +} diff --git a/portal/autopay.php b/portal/autopay.php new file mode 100644 index 00000000..a4b92821 --- /dev/null +++ b/portal/autopay.php @@ -0,0 +1,126 @@ + + +

AutoPay

+
+ +
+ + + + + Save card details
+ In order to set up automatic payments, you must create a customer record in Stripe.
+ First, you must authorize Stripe to store your card details for the purpose of automatic payment. +

+ +
+
+ +
+
+ + +
+
+ +
+ +
+
+
+ + + elseif (empty($stripe_pm)) { ?> + + Save card details
+ Please add the payment details you would like to save.
+ By adding payment details here, you grant consent for future automatic payments of invoices.

+ + + + +
+ +
+ + + + Manage saved card details + + customers->retrievePaymentMethod( + $stripe_id, + $stripe_pm, + [] + ); + + $card_name = nullable_htmlentities($payment_method->billing_details->name); + $card_brand = nullable_htmlentities($payment_method->card->display_brand); + $card_last4 = nullable_htmlentities($payment_method->card->last4); + $card_expires = nullable_htmlentities($payment_method->card->exp_month) . "/" . nullable_htmlentities($payment_method->card->exp_year); + + ?> + +
+ +
+ Actions
+ - Remove saved card + + + + +
+ +
+ + + - - + - - - - - + +