Exemple #1
0
        public async Task ExecuteAsync(
            UserId enactingUserId,
            string stripeChargeId,
            PositiveInt totalRefundAmount,
            RefundCreditReason reason,
            UserType userType)
        {
            enactingUserId.AssertNotNull("enactingUserId");
            stripeChargeId.AssertNotNull("stripeChargeId");
            totalRefundAmount.AssertNotNull("totalRefundAmount");

            var apiKey = this.apiKeyRepository.GetApiKey(userType);

            var options = new StripeRefundCreateOptions
            {
                Amount   = totalRefundAmount.Value,
                Reason   = this.GetReason(reason),
                Metadata = new Dictionary <string, string>
                {
                    { EnactingUserIdMetadataKey, enactingUserId.ToString() },
                }
            };

            await this.stripeService.RefundChargeAsync(stripeChargeId, options, apiKey);
        }
Exemple #2
0
        internal string GetReason(RefundCreditReason reason)
        {
            switch (reason)
            {
            case RefundCreditReason.Duplicate:
                return(DuplicateString);

            case RefundCreditReason.Fraudulent:
                return(FraudulentString);

            case RefundCreditReason.RequestedByCustomer:
                return(RequestedByCustomerString);
            }

            throw new InvalidOperationException("Unknown reason: " + reason);
        }
        public async Task <UserId> ExecuteAsync(
            UserId enactingUserId,
            TransactionReference transactionReference,
            DateTime timestamp,
            PositiveInt refundCreditAmount,
            RefundCreditReason reason,
            string comment)
        {
            enactingUserId.AssertNotNull("enactingUserId");
            transactionReference.AssertNotNull("transactionReference");
            refundCreditAmount.AssertNotNull("refundCreditAmount");

            var transaction = await this.retryOnTransientFailure.HandleAsync(
                () => this.getCreditTransaction.ExecuteAsync(transactionReference));

            if (transaction == null)
            {
                throw new BadRequestException(
                          "Transaction reference " + transactionReference + " was not a valid transaction, or the user is not a standard user.");
            }

            if (refundCreditAmount.Value > transaction.CreditAmountAvailableForRefund)
            {
                throw new BadRequestException(
                          "Requested refund amount was greater than amount available for refund.");
            }

            var userId = transaction.UserId;

            // Refund on taxamo, get tax refund amount.
            var taxamoResult = await this.retryOnTransientFailure.HandleAsync(
                () => this.createTaxamoRefund.ExecuteAsync(transaction.TaxamoTransactionKey, refundCreditAmount, UserType.StandardUser));

            try
            {
                // Call CreateCreditRefundDbStatement
                await this.retryOnTransientFailure.HandleAsync(
                    () => this.persistCreditRefund.ExecuteAsync(
                        enactingUserId,
                        userId,
                        timestamp,
                        taxamoResult.TotalRefundAmount,
                        refundCreditAmount,
                        transactionReference,
                        transaction.StripeChargeId,
                        transaction.TaxamoTransactionKey,
                        comment));

                // refund on stripe.
                await this.retryOnTransientFailure.HandleAsync(
                    () => this.createStripeRefund.ExecuteAsync(enactingUserId, transaction.StripeChargeId, taxamoResult.TotalRefundAmount, reason, UserType.StandardUser));
            }
            catch (Exception t)
            {
                var json = JsonConvert.SerializeObject(
                    new
                {
                    TransactionReference = transactionReference,
                    Transaction          = transaction,
                    TaxamoResult         = taxamoResult,
                });

                throw new FailedToRefundCreditException(json, t);
            }

            return(userId);
        }