コード例 #1
0
ファイル: Models.cs プロジェクト: berlstone/GStore
        public PayPalTransactionData(decimal total, string description, PayPalItemData[] items)
        {
            this.description = description;
            this.amount.currency = "USD";
            this.amount.total = total.ToString("N2");
            this.amount.details = null;
            this.related_resources = null;
            this.soft_descriptor = null;
            this.invoice_number = null;
            this.custom = null;
            if (items == null)
            {
                this.item_list = null;
            }
            else
            {

                this.item_list = new PayPalItemListData(items);
            }
        }
コード例 #2
0
        protected PayPalPaymentData CreatePayPalPayment(PayPalOAuthTokenData token, decimal total, string description, PayPalItemData[] items, string return_url, string cancel_url, bool useSandbox)
        {
            if (string.IsNullOrEmpty(token.access_token))
            {
                throw new ArgumentNullException("token.access_token");
            }
            if (string.IsNullOrEmpty(return_url))
            {
                throw new ArgumentNullException("return_url");
            }
            if (string.IsNullOrEmpty(cancel_url))
            {
                throw new ArgumentNullException("cancel_url");
            }
            if (total < 0.01M)
            {
                throw new ArgumentOutOfRangeException("total", "total must be 1 cent or more; value invalid: " + total.ToString());
            }

            PayPalPaymentData requestData = new PayPalPaymentData();
            requestData.intent = "sale";
            requestData.redirect_urls.return_url = return_url;
            requestData.redirect_urls.cancel_url = cancel_url;
            requestData.payer.payment_method = "paypal";

            List<PayPalTransactionData> transactions = new List<PayPalTransactionData>();
            transactions.Add(new PayPalTransactionData(total, description, items));
            requestData.transactions = transactions.ToArray();

            string requestJson = JsonConvert.SerializeObject(requestData, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
            StringContent postContent = new StringContent(requestJson, Encoding.UTF8, "application/json");

            HttpClient httpClient = new HttpClient();
            httpClient.Timeout = new TimeSpan(0, 0, 30);
            Uri requestUri = null;
            if (useSandbox)
            {
                httpClient.BaseAddress = new Uri("https://api.sandbox.paypal.com/");
            }
            else
            {
                httpClient.BaseAddress = new Uri("https://api.paypal.com/");
            }
            requestUri = new Uri("v1/payments/payment", UriKind.Relative);

            HttpResponseMessage response;
            httpClient.DefaultRequestHeaders.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
            try
            {
                response = httpClient.PostAsync(requestUri, postContent).Result;
            }
            catch (Exception ex)
            {
                string message = "HTTP Client returned an error during PayPal Create Payment API post: " + ex.Message + ". See inner exception for details.";
                throw new Exceptions.PayPalExceptionCreatePaymentFailed(useSandbox, message, null, ex);
            }

            PayPalPaymentData paymentResponseData;
            if (response.IsSuccessStatusCode)
            {
                //get PayPal data
                string json = response.Content.ReadAsStringAsync().Result;
                try
                {
                    paymentResponseData = JsonConvert.DeserializeObject<PayPalPaymentData>(json);
                }
                catch (Exception ex)
                {
                    string message = "Error reading PayPal Create Payment API result! \nError code: " + response.StatusCode + " " + response.ReasonPhrase + "\n" + response.ToString() + " see inner exception for details.\nJSON Response Data: " + json;
                    throw new Exceptions.PayPalExceptionCreatePaymentFailed(useSandbox, message, response, ex);
                }
                paymentResponseData.Json = json;
            }
            else
            {
                string message = "PayPal Create Payment API call failed! \nError code: " + response.StatusCode + " " + response.ReasonPhrase + "\n" + response.ToString();
                throw new Exceptions.PayPalExceptionCreatePaymentFailed(useSandbox, message, response, null);
            }

            return paymentResponseData;
        }
コード例 #3
0
ファイル: Models.cs プロジェクト: berlstone/GStore
 public PayPalItemListData(PayPalItemData[] items)
 {
     this.items = items;
     this.shipping_address = null;
 }