Exemplo n.º 1
0
        /// <summary>
        /// Refunds the payment.
        /// </summary>
        /// <param name="paymentId">The payment identifier.</param>
        public void RefundPayment(long paymentId)
        {
            PaymentBR biz     = (PaymentBR)this.BusinessLogicObject;
            vPayment  payment = (vPayment)GetByID(paymentId, new GetByIDParameters(GetSourceTypeEnum.View));

            IUserPaymentInfoService paymentInfoService =
                (IUserPaymentInfoService)EntityFactory.GetEntityServiceByName(vUserPaymentInfo.EntityName, "");

            UserPaymentInfo receiverPaymentInfo = (UserPaymentInfo)paymentInfoService.GetByID(payment.ReceiverUserID, new GetByIDParameters());

            biz.RefundPayment(payment, receiverPaymentInfo);

            PayPalService paypalService   = new PayPalService();
            var           executionStatus = paypalService.GetPaymentExecutionStatus(payment.PayKey);

            if (executionStatus == PaypalApi.PaymentExecStatusSEnum.COMPLETED)
            {
                // logging the details
                long?userId = null;
                if (FWUtils.SecurityUtils.IsUserAuthenticated())
                {
                    userId = FWUtils.SecurityUtils.GetCurrentUserIDLong();
                }
                FWUtils.ExpLogUtils.Logger.WriteLog(new AppLog()
                {
                    AppLogTypeID = (short)EntityEnums.AppLogType.PayPal_Refund, UserID = userId, ExtraBigInt = paymentId
                });

                RefundParameters p = new RefundParameters();
                p.payKey          = payment.PayKey;
                p.receiver1amount = payment.Amount - payment.ServiceChargeAmount;
                p.receiver2amount = payment.ServiceChargeAmount;
                p.receiver1email  = receiverPaymentInfo.UserPaymentInfoPayPalEmail;
                p.receiver2email  = FWUtils.ConfigUtils.GetAppSettings().Paypal.MainAccount;

                paypalService.RefundPayment(p);
                UpdateStatusPaymentInDatabase(paymentId, EntityEnums.PaymentStatusEnum.Refunded, true);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Refunds the payment.
        /// </summary>
        /// <param name="p">The parameters.</param>
        /// <exception cref="UserException">If refund was not suceessful, it throws an exception for the user</exception>
        public void RefundPayment(RefundParameters p)
        {
            // error language : (Required) RFC 3066 language in which error messages are returned; by default it is en_US, which is the only language currently supported.
            RefundRequest request = new RefundRequest(new RequestEnvelope("en_US"));

            request.payKey       = p.payKey;
            request.currencyCode = p.currencyCode;

            List <Receiver> receivers = new List <Receiver>();

            request.receiverList = new ReceiverList(receivers);
            if (p.receiver1amount != 0)
            {
                AddReceiver(p.receiver1amount, p.receiver1email, request.receiverList);
            }
            if (p.receiver2amount != 0)
            {
                AddReceiver(p.receiver2amount, p.receiver2email, request.receiverList);
            }

            AdaptivePaymentsService service  = null;
            RefundResponse          response = null;

            // Configuration map containing signature credentials and other required configuration.
            // For a full list of configuration parameters refer in wiki page
            // (https://github.com/paypal/sdk-core-dotnet/wiki/SDK-Configuration-Parameters)
            Dictionary <string, string> configurationMap = FWUtils.ConfigUtils.GetAppSettings().Paypal.GetAcctAndConfig();

            // Creating service wrapper object to make an API call and loading
            // configuration map for your credentials and endpoint
            service  = new AdaptivePaymentsService(configurationMap);
            response = service.Refund(request);

            string ack = response.responseEnvelope.ack.ToString().Trim().ToUpper();

            // if no error happened
            if (!ack.Equals(AckCode.FAILURE.ToString()) &&
                !ack.Equals(AckCode.FAILUREWITHWARNING.ToString()))
            {
                StringBuilder sbErrors = new StringBuilder();
                bool          hasError = false;
                foreach (RefundInfo refund in response.refundInfoList.refundInfo)
                {
                    RefundStatusSEnum refundStatus = new RefundStatusSEnum(refund.refundStatus);
                    if (refundStatus != RefundStatusSEnum.REFUNDED &&
                        refundStatus != RefundStatusSEnum.REFUNDED_PENDING)
                    {
                        sbErrors.Append(refundStatus.GetUserFriendlyMessage());
                        hasError = true;

                        if (refundStatus == RefundStatusSEnum.NO_API_ACCESS_TO_RECEIVER)
                        {
                            EmailPersonRefundError(refund.receiver.email, refundStatus);
                        }
                    }
                }
                if (hasError)
                {
                    throw new UserException(sbErrors.ToString());
                }
            }
            else
            {
                throw new UserException(GetPayPalErrorString(response.error));
            }
        }