114 lines
4.5 KiB
PHP
114 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace humhub\modules\donations\services;
|
|
|
|
use humhub\modules\donations\models\DonationGoal;
|
|
use humhub\modules\donations\models\DonationTransaction;
|
|
use Yii;
|
|
|
|
class DonationAnimalIntegrationService
|
|
{
|
|
public function applySucceededIntegration(DonationTransaction $transaction, ?DonationGoal $goal, array $payload): array
|
|
{
|
|
if (!empty($payload['animal_progress_succeeded_created'])) {
|
|
return $payload;
|
|
}
|
|
|
|
return $this->publishAnimalProgressEntry($transaction, $goal, $payload, 'succeeded');
|
|
}
|
|
|
|
public function applyRefundedIntegration(DonationTransaction $transaction, ?DonationGoal $goal, array $payload): array
|
|
{
|
|
if (!empty($payload['animal_progress_refunded_created'])) {
|
|
return $payload;
|
|
}
|
|
|
|
return $this->publishAnimalProgressEntry($transaction, $goal, $payload, 'refunded');
|
|
}
|
|
|
|
private function publishAnimalProgressEntry(DonationTransaction $transaction, ?DonationGoal $goal, array $payload, string $mode): array
|
|
{
|
|
$goal = $goal instanceof DonationGoal ? $goal : DonationGoal::findOne([
|
|
'id' => (int)$transaction->goal_id,
|
|
'contentcontainer_id' => (int)$transaction->contentcontainer_id,
|
|
]);
|
|
|
|
if (!$goal instanceof DonationGoal || (string)$goal->goal_type !== DonationGoal::TYPE_ANIMAL || (int)$goal->target_animal_id <= 0) {
|
|
return $payload;
|
|
}
|
|
|
|
$animalClass = 'humhub\\modules\\animal_management\\models\\Animal';
|
|
$updateClass = 'humhub\\modules\\animal_management\\models\\AnimalProgressUpdate';
|
|
$publisherClass = 'humhub\\modules\\animal_management\\services\\AnimalStreamPublisherService';
|
|
|
|
if (!class_exists($animalClass) || !class_exists($updateClass)) {
|
|
return $payload;
|
|
}
|
|
|
|
if (Yii::$app->db->schema->getTableSchema($animalClass::tableName(), true) === null
|
|
|| Yii::$app->db->schema->getTableSchema($updateClass::tableName(), true) === null) {
|
|
return $payload;
|
|
}
|
|
|
|
$animal = $animalClass::findOne([
|
|
'id' => (int)$goal->target_animal_id,
|
|
'contentcontainer_id' => (int)$transaction->contentcontainer_id,
|
|
]);
|
|
|
|
if ($animal === null) {
|
|
$payload['animal_progress_' . $mode . '_error'] = 'Target animal not found.';
|
|
return $payload;
|
|
}
|
|
|
|
$label = number_format((float)$transaction->amount, 2) . ' ' . strtoupper((string)$transaction->currency);
|
|
$note = $mode === 'refunded'
|
|
? Yii::t('DonationsModule.base', 'Donation refunded: {amount} for goal "{goalTitle}".', [
|
|
'amount' => $label,
|
|
'goalTitle' => (string)$goal->title,
|
|
])
|
|
: Yii::t('DonationsModule.base', 'Donation received: {amount} for goal "{goalTitle}".', [
|
|
'amount' => $label,
|
|
'goalTitle' => (string)$goal->title,
|
|
]);
|
|
|
|
$update = new $updateClass();
|
|
$update->animal_id = (int)$animal->id;
|
|
$update->created_by = (int)$transaction->donor_user_id > 0 ? (int)$transaction->donor_user_id : null;
|
|
$update->update_at = date('Y-m-d H:i:s');
|
|
$update->behavior_notes = $note;
|
|
$update->post_to_space_feed = 1;
|
|
$update->post_to_animal_feed = 1;
|
|
|
|
if (!$update->save()) {
|
|
Yii::warning([
|
|
'message' => 'Could not save animal progress update for donation integration.',
|
|
'transaction_id' => (int)$transaction->id,
|
|
'mode' => $mode,
|
|
'errors' => $update->getErrors(),
|
|
], 'donations.animal_integration');
|
|
|
|
$payload['animal_progress_' . $mode . '_error'] = 'Could not save animal progress update.';
|
|
return $payload;
|
|
}
|
|
|
|
if (class_exists($publisherClass) && method_exists($publisherClass, 'publishProgressUpdate')) {
|
|
try {
|
|
$publisherClass::publishProgressUpdate($animal, $update);
|
|
} catch (\Throwable $e) {
|
|
Yii::warning([
|
|
'message' => 'Could not publish animal stream entry for donation integration.',
|
|
'transaction_id' => (int)$transaction->id,
|
|
'mode' => $mode,
|
|
'exception' => $e->getMessage(),
|
|
], 'donations.animal_integration');
|
|
}
|
|
}
|
|
|
|
$payload['animal_progress_' . $mode . '_created'] = 1;
|
|
$payload['animal_progress_' . $mode . '_id'] = (int)$update->id;
|
|
$payload['animal_progress_' . $mode . '_at'] = date('c');
|
|
|
|
return $payload;
|
|
}
|
|
}
|