Skip to content

Commit 605d745

Browse files
committed
to fix refund of application fees
1 parent e11cfee commit 605d745

File tree

2 files changed

+49
-68
lines changed

2 files changed

+49
-68
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ThisBuild / organization := "app.softnetwork"
22

33
name := "payment"
44

5-
ThisBuild / version := "0.7.3.1"
5+
ThisBuild / version := "0.7.3.2"
66

77
ThisBuild / scalaVersion := "2.12.18"
88

stripe/src/main/scala/app/softnetwork/payment/spi/StripeRefundApi.scala

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.stripe.model.{
1313
Transfer,
1414
TransferReversal
1515
}
16+
import com.stripe.net.RequestOptions
1617
import com.stripe.param.{
1718
FeeRefundCollectionCreateParams,
1819
PayoutCancelParams,
@@ -42,6 +43,36 @@ trait StripeRefundApi extends RefundApi { _: StripeContext =>
4243
s"Processing refund transaction for order: ${refundTransaction.orderUuid} -> ${asJson(refundTransaction)}"
4344
)
4445

46+
def refundApplicationFees(
47+
charge: Charge,
48+
feesRefundAmount: Int,
49+
requestOptions: RequestOptions
50+
): Unit = {
51+
// retrieve the optional application fees associated with the charge
52+
Option(charge.getApplicationFee) match {
53+
54+
case Some(applicationFeeId) =>
55+
val applicationFee = ApplicationFee.retrieve(applicationFeeId, requestOptions)
56+
57+
val params =
58+
FeeRefundCollectionCreateParams
59+
.builder()
60+
.setAmount(Math.min(charge.getApplicationFeeAmount.intValue(), feesRefundAmount))
61+
.putMetadata("order_uuid", refundTransaction.orderUuid)
62+
.putMetadata("author_id", refundTransaction.authorId)
63+
.putMetadata("reason_message", refundTransaction.reasonMessage)
64+
65+
mlog.info(
66+
s"Processing refund fees for order: ${refundTransaction.orderUuid} -> ${new Gson()
67+
.toJson(params)}"
68+
)
69+
70+
applicationFee.getRefunds.create(params.build(), requestOptions)
71+
72+
case _ =>
73+
}
74+
}
75+
4576
Try {
4677
val requestOptions = StripeApi().requestOptions()
4778

@@ -57,14 +88,21 @@ trait StripeRefundApi extends RefundApi { _: StripeContext =>
5788
if sourceTransaction
5889
.startsWith("pi_") || sourceTransaction.startsWith("ch_") =>
5990
false
60-
case _ => true
91+
case _ =>
92+
refundTransaction.feesRefundAmount.getOrElse(
93+
0
94+
) > 0
95+
//FIXME when application fees to refund are less than
96+
// the application fees charged
6197
}
6298

6399
val params =
64100
TransferReversalCollectionCreateParams
65101
.builder()
66102
.setAmount(Math.min(transfer.getAmount.intValue(), refundTransaction.refundAmount))
67-
.setRefundApplicationFee(refundApplicationFee)
103+
.setRefundApplicationFee(
104+
refundApplicationFee
105+
) // whether to refund the application fees or not
68106
.putMetadata("order_uuid", refundTransaction.orderUuid)
69107
.putMetadata("author_id", refundTransaction.authorId)
70108
.putMetadata("reason_message", refundTransaction.reasonMessage)
@@ -107,44 +145,13 @@ trait StripeRefundApi extends RefundApi { _: StripeContext =>
107145
val transferReversal =
108146
Option(payment.getTransferData).flatMap(td => Option(td.getDestination)).isDefined
109147

110-
val refundFees = transferReversal && payment.getApplicationFeeAmount > 0
111-
112-
val refundApplicationFee =
113-
refundFees && refundTransaction.feesRefundAmount.getOrElse(0) == 0
114-
115148
refundTransaction.feesRefundAmount match {
116149
case Some(feesRefundAmount)
117-
if refundFees && feesRefundAmount > 0 => // refund application fees
150+
if transferReversal && payment.getApplicationFeeAmount > 0 && feesRefundAmount > 0 =>
118151
// first retrieve the latest charge associated with the payment
119-
val latestCharge = Charge.retrieve(payment.getLatestCharge, requestOptions)
120-
121-
// then retrieve the optional application fees associated with the charge
122-
Option(latestCharge.getApplicationFee) match {
123-
124-
case Some(applicationFeeId) =>
125-
val applicationFee = ApplicationFee.retrieve(applicationFeeId, requestOptions)
126-
127-
val params =
128-
FeeRefundCollectionCreateParams
129-
.builder()
130-
.setAmount(
131-
Math.min(payment.getApplicationFeeAmount.intValue(), feesRefundAmount)
132-
)
133-
.putMetadata("order_uuid", refundTransaction.orderUuid)
134-
.putMetadata("author_id", refundTransaction.authorId)
135-
.putMetadata("reason_message", refundTransaction.reasonMessage)
136-
137-
mlog.info(
138-
s"Processing refund fees for order: ${refundTransaction.orderUuid} -> ${new Gson()
139-
.toJson(params)}"
140-
)
141-
142-
applicationFee.getRefunds.create(params.build(), requestOptions)
143-
144-
case _ =>
145-
146-
}
147-
152+
val charge = Charge.retrieve(payment.getLatestCharge, requestOptions)
153+
// refund the application fees
154+
refundApplicationFees(charge, feesRefundAmount, requestOptions)
148155
case _ =>
149156
}
150157

@@ -156,7 +163,7 @@ trait StripeRefundApi extends RefundApi { _: StripeContext =>
156163
)
157164
.setCharge(payment.getLatestCharge)
158165
.setReverseTransfer(transferReversal)
159-
.setRefundApplicationFee(refundApplicationFee)
166+
.setRefundApplicationFee(false)
160167
.putMetadata("order_uuid", refundTransaction.orderUuid)
161168
.putMetadata("author_id", refundTransaction.authorId)
162169
.putMetadata("reason_message", refundTransaction.reasonMessage)
@@ -175,35 +182,9 @@ trait StripeRefundApi extends RefundApi { _: StripeContext =>
175182
case charge: Charge =>
176183
refundTransaction.feesRefundAmount match {
177184
case Some(feesRefundAmount)
178-
if charge.getApplicationFeeAmount > 0 && feesRefundAmount > 0 => // refund application fees
179-
180-
// retrieve the optional application fees associated with the charge
181-
Option(charge.getApplicationFee) match {
182-
183-
case Some(applicationFeeId) =>
184-
val applicationFee = ApplicationFee.retrieve(applicationFeeId, requestOptions)
185-
186-
val params =
187-
FeeRefundCollectionCreateParams
188-
.builder()
189-
.setAmount(
190-
Math.min(charge.getApplicationFeeAmount.intValue(), feesRefundAmount)
191-
)
192-
.putMetadata("order_uuid", refundTransaction.orderUuid)
193-
.putMetadata("author_id", refundTransaction.authorId)
194-
.putMetadata("reason_message", refundTransaction.reasonMessage)
195-
196-
mlog.info(
197-
s"Processing refund fees for order: ${refundTransaction.orderUuid} -> ${new Gson()
198-
.toJson(params)}"
199-
)
200-
201-
applicationFee.getRefunds.create(params.build(), requestOptions)
202-
203-
case _ =>
204-
205-
}
206-
185+
if charge.getApplicationFeeAmount > 0 && feesRefundAmount > 0 =>
186+
// refund the application fees
187+
refundApplicationFees(charge, feesRefundAmount, requestOptions)
207188
case _ =>
208189
}
209190

0 commit comments

Comments
 (0)