Пример #1
0
        public static PayPalPaymentConfirmation ProcessResponse(PayPalPaymentConfirmation payment)
        {
            Contract.Requires <ArgumentNullException>(null != payment);

            if (string.IsNullOrWhiteSpace(payment.Response))
            {
                log.Log("Empty response returned from PayPal.");
            }
            else if (payment.Response.StartsWith("SUCCESS", StringComparison.InvariantCultureIgnoreCase))
            {
                payment.Successful = true;

                payment = PaymentCore.ProcessSuccessfulResponse(payment);

                var preferenceTable = new AzureTable <UserPreferenceRow>(ServerConfiguration.Default);
                var preference      = preferenceTable.QueryBy(payment.Application.Identifier.ToString(), payment.User.Identifier.ToString());

                preference = preference ?? new UserPreferenceRow(payment.Application.Identifier, payment.User.Identifier);

                preference.MaxiumAllowedApplications = preference.MaxiumAllowedApplications.HasValue ? preference.MaxiumAllowedApplications + 1 : 1;
                preferenceTable.AddOrUpdateEntity(preference);
            }
            else
            {
                payment.Successful = false;

                log.Log("Error Accepting payment response.");
            }

            var table = new AzureTable <PayPalPaymentConfirmationRow>(ServerConfiguration.Default);

            table.AddEntity(payment.Convert());

            return(payment);
        }
Пример #2
0
        public bool ValidatePurchase(PayPalPaymentConfirmation payment)
        {
            Contract.Requires <ArgumentNullException>(null != payment);

            Contract.Requires <ArgumentException>(!string.IsNullOrWhiteSpace(payment.TransactionId));

            payment.Successful = false;

            var url   = ServerConfiguration.PayPalPaymentDataTransferUrl;
            var query = "cmd=_notify-synch&tx={0}&at={1}".FormatWithCulture(payment.TransactionId, ServerConfiguration.PayPalPaymentDataTransfer);
            var req   = (HttpWebRequest)WebRequest.Create(url);

            // Set values for the request back
            req.Method        = "POST";
            req.ContentType   = "application/x-www-form-urlencoded";
            req.ContentLength = query.Length;

            // Write the request back IPN strings
            using (var output = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
            {
                output.Write(query);

                // Do the request to PayPal and get the response
                var input = new StreamReader(req.GetResponse().GetResponseStream());

                payment.Response = input.ReadToEnd();

                payment = PaymentCore.ProcessResponse(payment);
            }

            return(payment.Successful);
        }