diff --git a/.gitignore b/.gitignore index 2f2c52c..161f68f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ sbt.json target .metals -*.log \ No newline at end of file +*.log +tmp \ No newline at end of file diff --git a/build.sbt b/build.sbt index f970912..cf2b14e 100644 --- a/build.sbt +++ b/build.sbt @@ -2,7 +2,7 @@ ThisBuild / organization := "app.softnetwork" name := "payment" -ThisBuild / version := "0.8.2" +ThisBuild / version := "0.8.3" ThisBuild / scalaVersion := "2.12.18" diff --git a/common/src/main/protobuf/message/payment/transaction.proto b/common/src/main/protobuf/message/payment/transaction.proto index 9fd1402..2a61b0d 100644 --- a/common/src/main/protobuf/message/payment/transaction.proto +++ b/common/src/main/protobuf/message/payment/transaction.proto @@ -139,6 +139,7 @@ message TransferFailedEvent { required string resultMessage = 2; optional app.softnetwork.payment.model.Transaction transaction = 3; optional string externalReference = 4; + optional string orderUuid = 5; } message DirectDebitedEvent { diff --git a/core/src/main/scala/app/softnetwork/payment/persistence/typed/PaymentBehavior.scala b/core/src/main/scala/app/softnetwork/payment/persistence/typed/PaymentBehavior.scala index 534a302..8575a40 100644 --- a/core/src/main/scala/app/softnetwork/payment/persistence/typed/PaymentBehavior.scala +++ b/core/src/main/scala/app/softnetwork/payment/persistence/typed/PaymentBehavior.scala @@ -471,37 +471,56 @@ trait PaymentBehavior case Some(authorId) => paymentAccount.walletId match { case Some(debitedWalletId) => - // load credited payment account - paymentDao.loadPaymentAccount(creditedAccount, clientId) complete () match { - case Success(s) => - maybeCreditedPaymentAccount = s - maybeCreditedPaymentAccount match { - case Some(creditedPaymentAccount) => // credited account - creditedPaymentAccount.userId match { - case Some(creditedUserId) => - creditedPaymentAccount.walletId match { - case Some(creditedWalletId) => - Some( - TransferTransaction.defaultInstance - .withDebitedAmount(debitedAmount) - .withFeesAmount(feesAmount) - .withCurrency(currency) - .withAuthorId(authorId) - .withDebitedWalletId(debitedWalletId) - .withCreditedUserId(creditedUserId) - .withCreditedWalletId(creditedWalletId) - .copy( - orderUuid = orderUuid, - externalReference = externalReference - ) - ) - case _ => None - } - case _ => None - } - case _ => None - } - case Failure(_) => None + if (debitedAccount == creditedAccount) { // direct transfer from the platform account to authorId + val creditedUserId = authorId + val creditedWalletId = debitedWalletId + Some( + TransferTransaction.defaultInstance + .withDebitedAmount(debitedAmount) + .withFeesAmount(feesAmount) + .withCurrency(currency) + .withAuthorId(authorId) + .withDebitedWalletId(debitedWalletId) + .withCreditedUserId(creditedUserId) + .withCreditedWalletId(creditedWalletId) + .copy( + orderUuid = orderUuid, + externalReference = externalReference + ) + ) + } else { + // load credited payment account + paymentDao.loadPaymentAccount(creditedAccount, clientId) complete () match { + case Success(s) => + maybeCreditedPaymentAccount = s + maybeCreditedPaymentAccount match { + case Some(creditedPaymentAccount) => // credited account + creditedPaymentAccount.userId match { + case Some(creditedUserId) => + creditedPaymentAccount.walletId match { + case Some(creditedWalletId) => + Some( + TransferTransaction.defaultInstance + .withDebitedAmount(debitedAmount) + .withFeesAmount(feesAmount) + .withCurrency(currency) + .withAuthorId(authorId) + .withDebitedWalletId(debitedWalletId) + .withCreditedUserId(creditedUserId) + .withCreditedWalletId(creditedWalletId) + .copy( + orderUuid = orderUuid, + externalReference = externalReference + ) + ) + case _ => None + } + case _ => None + } + case _ => None + } + case Failure(_) => None + } } case _ => None } @@ -579,7 +598,10 @@ trait PaymentBehavior .withDebitedAccount(paymentAccount.externalUuid) .withResultMessage(transaction.resultMessage) .withTransaction(transaction) - .copy(externalReference = externalReference) + .copy( + externalReference = externalReference, + orderUuid = orderUuid + ) ) :+ transactionUpdatedEvent ) .thenRun(_ => diff --git a/stripe/src/main/scala/app/softnetwork/payment/spi/StripeTransferApi.scala b/stripe/src/main/scala/app/softnetwork/payment/spi/StripeTransferApi.scala index a5a28c2..47a905c 100644 --- a/stripe/src/main/scala/app/softnetwork/payment/spi/StripeTransferApi.scala +++ b/stripe/src/main/scala/app/softnetwork/payment/spi/StripeTransferApi.scala @@ -150,6 +150,7 @@ trait StripeTransferApi extends TransferApi { _: StripeContext => .setAmount(Math.min(amountToTransfer, availableAmount)) .setDestination(transferTransaction.creditedUserId) .setCurrency(transferTransaction.currency) + // .setSourceType(TransferCreateParams.SourceType.CARD) .putMetadata("available_amount", availableAmount.toString) .putMetadata("debited_amount", transferTransaction.debitedAmount.toString) .putMetadata("fees_amount", transferTransaction.feesAmount.toString)