Skip to content

Commit

Permalink
Add Vendor Payment Refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunetsiz committed Feb 15, 2025
1 parent 350b08e commit eab51f5
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ MONERO_RPC_PORT=18082
MONERO_RPC_SSL=false
MONERO_VENDOR_PAYMENT_REQUIRED_AMOUNT=0.4
MONERO_VENDOR_PAYMENT_MINIMUM_AMOUNT=0.04
MONERO_VENDOR_PAYMENT_REFUND_PERCENTAGE=80
MONERO_ADDRESS_EXPIRATION_TIME=1440

# Marketplace Settings
Expand Down
66 changes: 63 additions & 3 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -978,20 +978,80 @@ public function denyVendorApplication(VendorPayment $application)
}

try {
\DB::beginTransaction();

// Get refund percentage from config
$refundPercentage = config('monero.vendor_payment_refund_percentage');
if ($refundPercentage <= 0) {
throw new \Exception('Refund percentage must be greater than zero');
}

// Calculate refund amount
$refundAmount = $application->total_received * ($refundPercentage / 100);

// Get random verified return address
$returnAddress = $application->user->returnAddresses()
->where('is_verified', true)
->inRandomOrder()
->first();

if (!$returnAddress) {
throw new \Exception('No verified return address found for user');
}

// First update application status
$application->update([
'application_status' => 'denied',
'admin_response_at' => now()
]);

Log::info("Vendor application denied for user {$application->user_id}");
try {
// Initialize WalletRPC with increased timeout
$config = config('monero');
$walletRPC = new \MoneroIntegrations\MoneroPhp\walletRPC(
$config['host'],
$config['port'],
$config['ssl'],
30000 // 30 second timeout
);

// Process refund
$result = $walletRPC->transfer([
'address' => $returnAddress->monero_address,
'amount' => $refundAmount,
'priority' => 1
]);

// Update refund details only if transfer successful
$application->update([
'refund_amount' => $refundAmount,
'refund_address' => $returnAddress->monero_address
]);

$refundMessage = "Refund of {$refundAmount} XMR has been processed.";
} catch (\Exception $e) {
Log::error('Error processing refund: ' . $e->getMessage());
$refundMessage = "Application denied but refund failed. Please process refund manually.";

// Still update refund details for manual processing
$application->update([
'refund_amount' => $refundAmount,
'refund_address' => $returnAddress->monero_address
]);
}

\DB::commit();

Log::info("Vendor application denied for user {$application->user_id}. Refund processed: {$refundAmount} XMR to address {$returnAddress->monero_address}");

return redirect()->route('admin.vendor-applications.show', $application)
->with('success', 'Application denied successfully.');
->with('success', "Application denied successfully. " . $refundMessage);

} catch (\Exception $e) {
\DB::rollBack();
Log::error('Error denying vendor application: ' . $e->getMessage());
return redirect()->back()
->with('error', 'An error occurred while processing the application.');
->with('error', 'An error occurred while processing the application and refund.');
}
}

Expand Down
5 changes: 4 additions & 1 deletion app/Models/VendorPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ class VendorPayment extends Model
'application_status',
'application_images',
'application_submitted_at',
'admin_response_at'
'admin_response_at',
'refund_amount',
'refund_address'
];

protected $casts = [
'total_received' => 'decimal:12',
'refund_amount' => 'decimal:12',
'expires_at' => 'datetime',
'payment_completed' => 'boolean',
'application_images' => 'json',
Expand Down
1 change: 1 addition & 0 deletions config/monero.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
'ssl' => env('MONERO_RPC_SSL', false),
'vendor_payment_required_amount' => env('MONERO_VENDOR_PAYMENT_REQUIRED_AMOUNT', 0.4),
'vendor_payment_minimum_amount' => env('MONERO_VENDOR_PAYMENT_MINIMUM_AMOUNT', 0.04),
'vendor_payment_refund_percentage' => env('MONERO_VENDOR_PAYMENT_REFUND_PERCENTAGE', 80), /*This value must never be set to zero.*/
'address_expiration_time' => env('MONERO_ADDRESS_EXPIRATION_TIME', 1440),
];
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function up()
$table->json('application_images')->nullable(); // Store up to 4 image paths
$table->timestamp('application_submitted_at')->nullable();
$table->timestamp('admin_response_at')->nullable();
$table->decimal('refund_amount', 20, 12)->nullable();
$table->string('refund_address')->nullable();
$table->boolean('payment_completed')->default(false);
});
}
Expand Down
14 changes: 14 additions & 0 deletions resources/views/admin/vendor-applications/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@
</p>
</div>
</div>

@if($application->application_status === 'denied' && $application->refund_amount)
<div class="vendor-applications-show-section">
<h2 class="vendor-applications-show-section-title">Refund Details</h2>
<div class="vendor-applications-show-status">
<p class="vendor-applications-show-detail-row">
<strong>Refund Amount:</strong> {{ $application->refund_amount }} XMR
</p>
<p class="vendor-applications-show-detail-row">
<strong>Refund Address:</strong> {{ $application->refund_address }}
</p>
</div>
</div>
@endif
@endif

<div class="vendor-applications-show-back">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/become-vendor/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<p class="become-vendor-index-status-message">Congratulations! Your application has been accepted. You can now access the vendor features.</p>
@else
<p class="become-vendor-index-status-message">Unfortunately, your application has been denied. You cannot submit a new application at this time.</p>
@if($vendorPayment->refund_amount)
<div class="become-vendor-index-refund-info">
<p class="become-vendor-index-status-message">A refund of {{ $vendorPayment->refund_amount }} XMR has been sent to your return address.</p>
</div>
@endif
@endif
</div>
@endif
Expand Down

0 comments on commit eab51f5

Please # to comment.