From 53f4fad589a9b55519093db2897ad2397d6ad0c3 Mon Sep 17 00:00:00 2001 From: Guillaume AGNIERAY Date: Tue, 18 Aug 2026 12:15:25 +0200 Subject: [PATCH] Store relevant JSON values in the history table --- lib/GaletteStripe/StripeHistory.php | 22 ++++---- scripts/mysql.sql | 15 +++--- scripts/pgsql.sql | 27 +++++----- scripts/upgrade-to-1.0-mysql.sql | 11 ---- scripts/upgrade-to-1.0-pgsql.sql | 10 ---- scripts/upgrade-to-1.0.0-mysql.sql | 44 ++++++++++++++++ scripts/upgrade-to-1.0.0-pgsql.sql | 31 +++++++++++ templates/default/stripe_history.html.twig | 60 ++++++++-------------- 8 files changed, 130 insertions(+), 90 deletions(-) delete mode 100644 scripts/upgrade-to-1.0-mysql.sql delete mode 100644 scripts/upgrade-to-1.0-pgsql.sql create mode 100644 scripts/upgrade-to-1.0.0-mysql.sql create mode 100644 scripts/upgrade-to-1.0.0-pgsql.sql diff --git a/lib/GaletteStripe/StripeHistory.php b/lib/GaletteStripe/StripeHistory.php index eedc8c9..d79ee6b 100644 --- a/lib/GaletteStripe/StripeHistory.php +++ b/lib/GaletteStripe/StripeHistory.php @@ -69,20 +69,20 @@ public function add(array|string $action, string $argument = '', string $query = $stripe = new Stripe($this->zdb, $this->preferences); $request = $action; $payment_method = $this->getStripePaymentMethod($request['data']['object']['payment_method']); - - // Retrieve receipt URL and add it to the request $charge = $this->getStripeCharge($request['data']['object']['latest_charge']); - $request['receipt_url'] = $charge['receipt_url']; try { $values = [ 'history_date' => date('Y-m-d H:i:s'), 'intent_id' => $request['data']['object']['id'], - 'amount' => $stripe->isZeroDecimal($stripe->getCurrency()) ? $request['data']['object']['amount'] : $request['data']['object']['amount'] / 100, 'payer_name' => $payment_method['billing_details']['name'], + 'member_id' => $request['data']['object']['metadata']['member_id'] ?? 0, 'comments' => $request['data']['object']['metadata']['item_name'], - 'request' => Galette::jsonEncode($request), - 'state' => self::STATE_NONE + 'amount' => $stripe->isZeroDecimal($stripe->getCurrency()) ? $request['data']['object']['amount'] : $request['data']['object']['amount'] / 100, + 'method' => $payment_method['type'], + 'state' => self::STATE_NONE, + 'receipt_url' => $charge['receipt_url'], + 'request' => Galette::jsonEncode($request) ]; $insert = $this->zdb->insert($this->getTableName()); @@ -145,9 +145,7 @@ public function getStripeHistory(): array $oa = Galette::jsonDecode($o['request']); } - $member_id = $oa['data']['object']['metadata']['member_id'] ?? '0'; - - $o['member_fullname'] = $this->getMemberFullName($member_id); + $o['member_fullname'] = $this->getMemberFullName($o['member_id']); $o['raw_request'] = print_r($oa, true); $o['request'] = $oa; @@ -167,9 +165,9 @@ public function getStripeHistory(): array /** * Gets Member full name * - * @param string $id ID of the member to retrieve + * @param int $id ID of the member to retrieve */ - protected function getMemberFullName(string $id): string + protected function getMemberFullName(int $id): string { $fullname = _T('None', 'stripe'); @@ -180,7 +178,7 @@ protected function getMemberFullName(string $id): string $row = $result->current(); if ($row) { - $fullname = mb_strtoupper($row['nom_adh']) . ' ' . $row['prenom_adh']; + $fullname = mb_strtoupper($row['nom_adh'], 'UTF-8') . ' ' . $row['prenom_adh']; } return $fullname; diff --git a/scripts/mysql.sql b/scripts/mysql.sql index 0c24045..12ce84e 100644 --- a/scripts/mysql.sql +++ b/scripts/mysql.sql @@ -8,14 +8,17 @@ DROP TABLE IF EXISTS galette_stripe_history; CREATE TABLE galette_stripe_history ( id_stripe int(11) NOT NULL auto_increment, history_date datetime NOT NULL, - intent_id varchar(255) COLLATE utf8_unicode_ci, + intent_id varchar(255), amount double NOT NULL, - payer_name varchar(255) COLLATE utf8_unicode_ci, - comments varchar(255) COLLATE utf8_unicode_ci, - request text COLLATE utf8_unicode_ci, + comments varchar(255), + request text, state tinyint(4) NOT NULL DEFAULT 0, + payer_name varchar(255), + member_id int(10) NOT NULL, + method varchar(20) NOT NULL, + receipt_url varchar(255), PRIMARY KEY (`id_stripe`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; -- -- Table structure for table `galette_stripe_preferences` @@ -27,7 +30,7 @@ CREATE TABLE galette_stripe_preferences ( val_pref varchar(200) NOT NULL default '', PRIMARY KEY (id_pref), UNIQUE KEY (nom_pref) -) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; +) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci; INSERT INTO galette_stripe_preferences (nom_pref, val_pref) VALUES ('stripe_pubkey', ''); INSERT INTO galette_stripe_preferences (nom_pref, val_pref) VALUES ('stripe_privkey', ''); diff --git a/scripts/pgsql.sql b/scripts/pgsql.sql index df35488..5aac1a9 100644 --- a/scripts/pgsql.sql +++ b/scripts/pgsql.sql @@ -6,11 +6,11 @@ DROP SEQUENCE IF EXISTS galette_stripe_history_id_seq; CREATE SEQUENCE galette_stripe_history_id_seq - START 1 - INCREMENT 1 - MAXVALUE 2147483647 - MINVALUE 1 - CACHE 1; + START 1 + INCREMENT 1 + MAXVALUE 2147483647 + MINVALUE 1 + CACHE 1; DROP TABLE IF EXISTS galette_stripe_history; CREATE TABLE galette_stripe_history ( @@ -18,10 +18,13 @@ CREATE TABLE galette_stripe_history ( history_date date NOT NULL, intent_id character varying(255), amount real NOT NULL, - payer_name character varying(255), comments character varying(255), request text, state smallint DEFAULT 0 NOT NULL, + payer_name character varying(255), + member_id integer NOT NULL, + method character varying(20) NOT NULL, + receipt_url character varying(255), PRIMARY KEY (id_stripe) ); @@ -30,18 +33,18 @@ CREATE TABLE galette_stripe_history ( -- DROP SEQUENCE IF EXISTS galette_stripe_preferences_id_seq; CREATE SEQUENCE galette_stripe_preferences_id_seq - START 1 - INCREMENT 1 - MAXVALUE 2147483647 - MINVALUE 1 - CACHE 1; + START 1 + INCREMENT 1 + MAXVALUE 2147483647 + MINVALUE 1 + CACHE 1; DROP TABLE IF EXISTS galette_stripe_preferences; CREATE TABLE galette_stripe_preferences ( id_pref integer DEFAULT nextval('galette_stripe_preferences_id_seq'::text) NOT NULL, nom_pref character varying(100) NOT NULL default '', val_pref character varying(200) NOT NULL default '', - PRIMARY KEY (id_pref) + PRIMARY KEY (id_pref) ); CREATE UNIQUE INDEX galette_stripe_preferences_unique_idx ON galette_stripe_preferences (nom_pref); diff --git a/scripts/upgrade-to-1.0-mysql.sql b/scripts/upgrade-to-1.0-mysql.sql deleted file mode 100644 index fa38a71..0000000 --- a/scripts/upgrade-to-1.0-mysql.sql +++ /dev/null @@ -1,11 +0,0 @@ --- --- This file is part of Galette Stripe plugin (https://galette-community.github.io/plugin-stripe). --- SPDX-FileCopyrightText: Copyright © 2021-2026 The Galette Team --- SPDX-License-Identifier: GPL-3.0-or-later --- - -DROP TABLE galette_stripe_types_cotisation_prices; -ALTER TABLE galette_stripe_history CHANGE COLUMN comment comments varchar(255); -ALTER TABLE galette_stripe_history CHANGE COLUMN metadata request text; -ALTER TABLE galette_stripe_history ADD COLUMN payer_name varchar(255); -UPDATE galette_stripe_history SET state = 3 WHERE state = 0; diff --git a/scripts/upgrade-to-1.0-pgsql.sql b/scripts/upgrade-to-1.0-pgsql.sql deleted file mode 100644 index 01484ac..0000000 --- a/scripts/upgrade-to-1.0-pgsql.sql +++ /dev/null @@ -1,10 +0,0 @@ --- --- This file is part of Galette Stripe plugin (https://galette-community.github.io/plugin-stripe). --- SPDX-FileCopyrightText: Copyright © 2021-2026 The Galette Team --- SPDX-License-Identifier: GPL-3.0-or-later --- - -DROP TABLE galette_stripe_types_cotisation_prices; -ALTER TABLE galette_stripe_history RENAME COLUMN metadata TO request; -ALTER TABLE galette_stripe_history ADD COLUMN payer_name varchar(255); -UPDATE galette_stripe_history SET state = 3 WHERE state = 0; diff --git a/scripts/upgrade-to-1.0.0-mysql.sql b/scripts/upgrade-to-1.0.0-mysql.sql new file mode 100644 index 0000000..a2bb413 --- /dev/null +++ b/scripts/upgrade-to-1.0.0-mysql.sql @@ -0,0 +1,44 @@ +-- +-- This file is part of Galette Stripe plugin (https://galette-community.github.io/plugin-stripe). +-- SPDX-FileCopyrightText: Copyright © 2021-2026 The Galette Team +-- SPDX-License-Identifier: GPL-3.0-or-later +-- + +DROP TABLE galette_stripe_types_cotisation_prices; + +ALTER TABLE galette_stripe_history + CHANGE COLUMN comment comments varchar(255), + CHANGE COLUMN metadata request text; + +ALTER TABLE galette_stripe_history + MODIFY intent_id VARCHAR(255) COLLATE utf8mb4_unicode_520_ci, + MODIFY comments VARCHAR(255) COLLATE utf8mb4_unicode_520_ci, + MODIFY request TEXT COLLATE utf8mb4_unicode_520_ci, + CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci; + +ALTER TABLE galette_stripe_history + ADD COLUMN payer_name VARCHAR(255), + ADD COLUMN member_id INT(10) NOT NULL, + ADD COLUMN method VARCHAR(20) NOT NULL, + ADD COLUMN receipt_url VARCHAR(255); + +UPDATE galette_stripe_history +SET + state = CASE + WHEN state = 0 THEN 3 + ELSE state + END, + member_id = COALESCE( + CAST( + JSON_UNQUOTE(JSON_EXTRACT(request, '$.data.object.metadata.member_id')) + AS UNSIGNED + ), + 0 + ), + method = JSON_UNQUOTE(JSON_EXTRACT(request, '$.data.object.payment_method_types[0]')), + receipt_url = JSON_UNQUOTE(JSON_EXTRACT(request, '$.receipt_url')); + +ALTER TABLE galette_stripe_preferences + MODIFY nom_pref VARCHAR(100) NOT NULL DEFAULT '', + MODIFY val_pref VARCHAR(200) NOT NULL DEFAULT '', + CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci; diff --git a/scripts/upgrade-to-1.0.0-pgsql.sql b/scripts/upgrade-to-1.0.0-pgsql.sql new file mode 100644 index 0000000..8ba94e6 --- /dev/null +++ b/scripts/upgrade-to-1.0.0-pgsql.sql @@ -0,0 +1,31 @@ +-- +-- This file is part of Galette Stripe plugin (https://galette-community.github.io/plugin-stripe). +-- SPDX-FileCopyrightText: Copyright © 2021-2026 The Galette Team +-- SPDX-License-Identifier: GPL-3.0-or-later +-- + +DROP TABLE galette_stripe_types_cotisation_prices; + +ALTER TABLE galette_stripe_history + RENAME COLUMN metadata TO request, + ADD COLUMN payer_name character varying(255), + ADD COLUMN member_id integer, + ADD COLUMN method character varying(20), + ADD COLUMN receipt_url character varying(255); + +UPDATE galette_stripe_history +SET + state = CASE + WHEN state = 0 THEN 3 + ELSE state + END, + member_id = COALESCE( + (request #>> '{data,object,metadata,member_id}')::int, + 0 + ), + method = request #>> '{data,object,payment_method_types,0}', + receipt_url = request #>> '{receipt_url}'; + +ALTER TABLE galette_stripe_history + ALTER COLUMN member_id SET NOT NULL, + ALTER COLUMN method SET NOT NULL; diff --git a/templates/default/stripe_history.html.twig b/templates/default/stripe_history.html.twig index f9ee56c..597d704 100644 --- a/templates/default/stripe_history.html.twig +++ b/templates/default/stripe_history.html.twig @@ -87,45 +87,29 @@ {{ log.intent_id }} - {% if log.payer_name is not empty %} - {{ log.payer_name }} - {% else %} - {{ _T("None given", "stripe") }} - {% endif %} + {% if log.payer_name is not empty %} + {{ log.payer_name }} + {% else %} + {{ _T("None given", "stripe") }} + {% endif %} - {% if log.request is iterable %} - {% if log.request.data.object.metadata.member_id is defined %} - - {% endif %} - {{ log.member_fullname }} - {% if log.request.data.object.metadata.member_id is defined %} - - {% endif %} - {% else %} - {{ _T("No request or unable to read request.", "stripe") }} - {% endif %} + {% if log.member_id != 0 %} + + {% endif %} + {{ log.member_fullname }} + {% if log.member_id != 0 %} + + {% endif %} - {% if log.request is iterable %} - {% if log.request.data.object.metadata.item_name is defined %} - {{ log.request.data.object.metadata.item_name }} - {% else %} - {{ _T("None", "stripe") }} - {% endif %} - {% endif %} + {{ log.comments }} {{ log.amount }} - {% if log.request is iterable %} - {% if log.request.data.object.payment_method_types.0 is defined %} - {{ log.request.data.object.payment_method_types.0 }} - {% endif %} - {% else %} - {{ _T("No request or unable to read request.", "stripe") }} - {% endif %} + {{ log.method }} {% if log.state == constant('GaletteStripe\\StripeHistory::STATE_PUBLIC') or log.state == constant('GaletteStripe\\StripeHistory::STATE_PROCESSED') %} @@ -152,15 +136,13 @@ {% endif %} - {% if log.request is iterable %} - {% if log.request.receipt_url is defined %} - - - - {{ _T("View Stripe receipt of payment nb. %1$s sent by email to the payer", "stripe")|replace({"%1$s": log.intent_id}) }} - - - {% endif %} + {% if log.receipt_url is not empty %} + + + + {{ _T("View Stripe receipt of payment nb. %1$s sent by email to the payer", "stripe")|replace({"%1$s": log.intent_id}) }} + + {% endif %}