Exemplo n.º 1
0
        // GET: /account/paypal-payment
        public ActionResult PaypalPayment(string tx = null)
        {
            PayPalPaymentResult result = PayPalPaymentResult.Canceled;

            var helper = new PayPalHelper();

            var txnId = !String.IsNullOrEmpty(tx)
                        // tx in PDT from Sandbox is upper-case on automatic return and lower-case on manual return. A PDT request returns error 4002 if given a low-case tx.
                ? tx.ToUpper()
                : "PDT " + KeyUtils.GetCurrentTimeKey();

            helper.WriteLog(PayPalLogEntity.NotificationKind.PDT, txnId, Request.Url.Query);

            if (!String.IsNullOrEmpty(tx))
            {
                // Query PayPal.
                var response = helper.RequestPaymentDetails(txnId);

                var logRowKey = helper.WriteLog(PayPalLogEntity.NotificationKind.DetailsRequest, txnId, response);

                // Parse the response
                var lines   = helper.SplitPdtMessage(response);
                var payment = helper.ParsePaymentLines(lines);

                // Write the payment to the database.
                if (helper.PostIncomingPayPalPayment(payment, logRowKey))
                {
                    result = PayPalPaymentResult.Success;
                }
            }

            return(RedirectToAction("Transactions", new { PayPalPaymentResult = result }));
        }
Exemplo n.º 2
0
        public async Task <PayPalPaymentResult> CreatePaymentAsync(PayPalPayment payment)
        {
            var payPalPaymentCreateRequest = new PayPalPaymentCreateRequest
            {
                Intent = "sale",
                Payer  = new PayPalPayer()
                {
                    PaymentMethod = "paypal"
                },
                Transactions = new[]
                {
                    new PayPalTransaction
                    {
                        Amount = new PayPalAmount
                        {
                            Total    = payment.Total,
                            Currency = payment.Currency
                        }
                    },
                },
                RedirectUrls = new PayPalRedirectUrls()
                {
                    CancelUrl = payment.CancelUrl, ReturnUrl = payment.ReturnUrl
                }
            };
            var requestContent = new StringContent(JsonConvert.SerializeObject(payPalPaymentCreateRequest),
                                                   Encoding.UTF8, "application/json");

            var request = new HttpRequestMessage(HttpMethod.Post, CreatePaymentUrl);

            request.Content = requestContent;

            var response = await _http.SendAsync(request);

            var content = await response.Content.ReadAsStringAsync();

            var payPalResponse = JsonConvert.DeserializeObject <PayPalPaymentCreatedResponse>(content);

            if (!String.IsNullOrWhiteSpace(payPalResponse.ErrorMessage))
            {
                throw new PaypalException(payPalResponse.ErrorMessage, payPalResponse.ErrorDetailes.ToModels(), payPalResponse.InformationLink);
            }

            var result = new PayPalPaymentResult
            {
                RedirectUrl = payPalResponse.Links.SingleOrDefault(x => x.Rel == "approval_url")?.Href,
                Id          = payPalResponse.Id,
            };

            return(result);
        }
Exemplo n.º 3
0
 public ActionResult Transactions(PayPalPaymentResult payPalPaymentResult = PayPalPaymentResult.None)
 {
     return(View(payPalPaymentResult));
 }
Exemplo n.º 4
0
 public ActionResult Balance(PayPalPaymentResult payPalPaymentResult = PayPalPaymentResult.None)
 {
     return(View(payPalPaymentResult));
 }