Exemple #1
0
        public static TransactionResult ProcessExecution(Order order, PaymentReturnModel returnModel, PaypalWithRedirectSettings settings)
        {
            var transAmount = order.OrderTotal - order.StoreCreditAmount;
            var payment     = new PaymentExecution()
            {
                PayerId      = returnModel.PayerId,
                Transactions = new List <CartBase>()
                {
                    new CartBase()
                    {
                        Amount = new Amount()
                        {
                            Currency = order.CurrencyCode,
                            Total    = transAmount.ToString("N")
                        }
                    }
                }
            };
            var pcRequest = new PaymentExecuteRequest(returnModel.PaymentId);

            pcRequest.RequestBody(payment);

            var environment = GetEnvironment(settings);

            var client            = new PayPalHttpClient(environment);
            var transactionResult = new TransactionResult();

            try
            {
                var response = client.Execute(pcRequest).Result;
                var result   = response.Result <Payment>();
                transactionResult.Success                 = true;
                transactionResult.NewStatus               = result.State == "approved" ? PaymentStatus.Complete : PaymentStatus.OnHold;
                transactionResult.OrderGuid               = order.Guid;
                transactionResult.TransactionAmount       = result.Transactions[0].Amount.Total.GetDecimal();
                transactionResult.TransactionGuid         = returnModel.PaymentId;
                transactionResult.TransactionCurrencyCode = result.Transactions[0].Amount.Currency;
                transactionResult.ResponseParameters      = new Dictionary <string, object>()
                {
                    { "id", result.Id },
                    { "payerId", returnModel.PayerId },
                    { "paymentId", returnModel.PaymentId },
                    { "createTime", result.CreateTime },
                    { "failureReason", result.FailureReason },
                    { "experienceProfileId", result.ExperienceProfileId },
                    { "noteToPayer", result.NoteToPayer },
                    { "intent", result.Intent },
                    { "state", result.State },
                    { "updateTime", result.UpdateTime },
                    { "saleId", result.Transactions[0].RelatedResources[0].Sale.Id }
                };
            }
            catch (BraintreeHttp.HttpException ex)
            {
                transactionResult.Success   = false;
                transactionResult.Exception = ex;
            }

            return(transactionResult);
        }
Exemple #2
0
        private static Coupon GetCoupon(Order order)
        {
            Coupon coupon = null;

            if (order.DiscountId.HasValue && order.Discount > 0)
            {
                var couponCode = order.DiscountCoupon;
                if (couponCode.IsNullEmptyOrWhiteSpace())
                {
                    couponCode = "COUPON_" + order.DiscountId;
                }
                //create a coupon
                var options = new CouponCreateOptions
                {
                    Duration  = "once",
                    Id        = couponCode,
                    AmountOff = (long)order.Discount
                };
                var service = new CouponService();
                coupon = service.Create(options);
            }

            return(coupon);
        }