From e0b24dcf6b88be309ad9a8cfc24f6847f3e9de75 Mon Sep 17 00:00:00 2001 From: Nicolas Perrenoud Date: Wed, 10 Jan 2024 23:31:34 +0100 Subject: [PATCH] Fixed importing donations from Stripe export files --- Changelog.md | 1 + .../Fundraising/API/DonationController.php | 8 +++++-- app/Imports/Fundraising/DonationsImport.php | 24 +++++++++++++++++-- lang/de.json | 2 +- resources/js/vue-i18n-locales.generated.js | 2 +- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Changelog.md b/Changelog.md index 15c5c05c8..ee017f855 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,7 @@ ## 5.8.0 * Consolidated donor/donation export dialogs into separate page, with more options, and improved performance of Excel export +* Fixed importing donations from Stripe export files ## 5.7.0 diff --git a/app/Http/Controllers/Fundraising/API/DonationController.php b/app/Http/Controllers/Fundraising/API/DonationController.php index 26df7aa82..a816e5bcd 100644 --- a/app/Http/Controllers/Fundraising/API/DonationController.php +++ b/app/Http/Controllers/Fundraising/API/DonationController.php @@ -218,11 +218,15 @@ public function import(Request $request): JsonResponse ], ]); - (new DonationsImport())->import($request->file('file')); + $importer = new DonationsImport(); + $importer->import($request->file('file')); return response() ->json([ - 'message' => __('Import successful.'), + 'message' => __('Import successful (:donors new donors, :donations new donations).', [ + 'donors' => $importer->importedDonors, + 'donations' => $importer->importedDonations, + ]), ]); } } diff --git a/app/Imports/Fundraising/DonationsImport.php b/app/Imports/Fundraising/DonationsImport.php index 8137ce3cd..46ff8379f 100644 --- a/app/Imports/Fundraising/DonationsImport.php +++ b/app/Imports/Fundraising/DonationsImport.php @@ -16,8 +16,15 @@ class DonationsImport implements ToCollection, WithHeadingRow { use Importable; + public int $importedDonors = 0; + + public int $importedDonations = 0; + public function collection(Collection $rows): void { + $this->importedDonors = 0; + $this->importedDonations = 0; + foreach ($rows as $row) { Validator::make($rows->toArray(), [ '*.status' => 'required', @@ -28,12 +35,24 @@ public function collection(Collection $rows): void ->first(); if ($donor == null) { $donor = new Donor(); - $donor->first_name = preg_replace('/@.*$/', '', $row['customer_email']); + $nameArr = explode(' ', $row['customer_name_metadata']); + if (count($nameArr) > 1) { + $donor->last_name = array_pop($nameArr); + $donor->first_name = implode(' ', $nameArr); + } else { + $donor->first_name = $row['customer_name_metadata']; + } $donor->email = $row['customer_email']; + $donor->street = $row['shipping_address_line1'].($row['shipping_address_line2'] !== null ? ', '.$row['shipping_address_line2'] : ''); + $donor->city = $row['shipping_address_city']; + $donor->zip = $row['shipping_address_postal_code']; + $donor->country_code = $row['shipping_address_country']; + $donor->phone = $row['customer_phone']; $donor->save(); + $this->importedDonors++; } - $date = new Carbon($row['created_utc']); + $date = new Carbon($row['created_date_utc'] ?? $row['created_utc']); $amount = $row['amount']; $currency = strtoupper($row['currency']); if ($currency != config('fundraising.base_currency')) { @@ -57,6 +76,7 @@ public function collection(Collection $rows): void $donation->purpose = $row['description']; $donor->addDonation($donation); + $this->importedDonations++; } } } diff --git a/lang/de.json b/lang/de.json index 3fdf53f63..8eec407ef 100644 --- a/lang/de.json +++ b/lang/de.json @@ -221,7 +221,7 @@ "Choose file...": "Datei wählen...", "Remove file": "Datei entfernen", "View file": "Datei anzeigen", - "Import successful.": "Import erfolgreich.", + "Import successful (:donors new donors, :donations new donations).": "Import erfolgreich (:donors neue Spender, :donations neue Spenden).", "Imported :num records.": ":num Datensätze importiert.", "Select status...": "Status wählen...", "Unspecified": "Nicht spezifiziert", diff --git a/resources/js/vue-i18n-locales.generated.js b/resources/js/vue-i18n-locales.generated.js index 75342a1a3..0e3017c1c 100644 --- a/resources/js/vue-i18n-locales.generated.js +++ b/resources/js/vue-i18n-locales.generated.js @@ -222,7 +222,7 @@ export default { "Choose file...": "Datei wählen...", "Remove file": "Datei entfernen", "View file": "Datei anzeigen", - "Import successful.": "Import erfolgreich.", + "Import successful ({donors} new donors, {donations} new donations).": "Import erfolgreich ({donors} neue Spender, {donations} neue Spenden).", "Imported {num} records.": "{num} Datensätze importiert.", "Select status...": "Status wählen...", "Unspecified": "Nicht spezifiziert",