feat: refund

This commit is contained in:
efrilm
2025-08-04 02:04:03 +07:00
parent c1a739f7bd
commit cbfa1689ba
7 changed files with 709 additions and 206 deletions
@@ -395,4 +395,56 @@ class OrderRemoteDatasource {
return const Left('Terjadi kesalahan tak terduga');
}
}
Future<Either<String, bool>> refund({
required String orderId,
required String reason,
required List<OrderItem> orderItems,
}) async {
final authData = await AuthLocalDataSource().getAuthData();
final url = '${Variables.baseUrl}/api/v1/orders/$orderId/refund';
final int refundAmount = orderItems.fold(
0,
(sum, item) => sum + ((item.unitPrice ?? 0) * (item.quantity ?? 0)),
);
try {
final response = await dio.post(
url,
data: {
'refund_amount': refundAmount,
"order_items": orderItems
.map((item) => {
'order_item_id': item.id,
"refund_quantity": item.quantity,
"refund_amount": item.totalPrice,
"reason": "Item was cold"
})
.toList(),
'reason': reason,
},
options: Options(
headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
'Content-Type': 'application/json',
},
),
);
if (response.statusCode == 200) {
return Right(true);
} else {
return const Left('Gagal refund');
}
} on DioException catch (e) {
final errorMessage = e.response?.data['message'] ?? 'Kesalahan jaringan';
log("💥 Dio error: ${e.message}");
log("💥 Dio response: ${e.response?.data}");
return Left(errorMessage);
} catch (e) {
log("💥 Unexpected error: $e");
return const Left('Terjadi kesalahan tak terduga');
}
}
}