Skip to content

Commit

Permalink
Fixed importing donations from Stripe export files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcage committed Jan 10, 2024
1 parent 7c1bacc commit e0b24dc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions app/Http/Controllers/Fundraising/API/DonationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]),
]);
}
}
24 changes: 22 additions & 2 deletions app/Imports/Fundraising/DonationsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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')) {
Expand All @@ -57,6 +76,7 @@ public function collection(Collection $rows): void
$donation->purpose = $row['description'];

$donor->addDonation($donation);
$this->importedDonations++;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion resources/js/vue-i18n-locales.generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e0b24dc

Please sign in to comment.