GetAccessToken() public method

Returns the currently cached access token. If no access token was previously cached, or if the current access token is expired, then a new one is generated and returned.
Thrown if clientId or clientSecret are null or empty. Thrown if there is an issue converting the credentials to a formatted authorization string. Thrown if authorization fails as a result of providing invalid credentials. Thrown if authorization fails and an HTTP error response is received. Thrown if there is an issue attempting to connect to PayPal's services. Thrown if there is an error with any informaiton provided by the . Thrown for any other general exception. See inner exception for further details.
public GetAccessToken ( ) : string
return string
コード例 #1
0
        public void OAuthTokenCredentialGetAccessTokenTest()
        {
            try
            {
                var oauthTokenCredential = new OAuthTokenCredential();
                var accessToken = oauthTokenCredential.GetAccessToken();
                this.RecordConnectionDetails();

                Assert.IsTrue(accessToken.StartsWith("Bearer "));
            }
            catch(ConnectionException)
            {
                this.RecordConnectionDetails(false);
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// ตัดบัตรเครดิต
        /// </summary>
        /// <param name="payment">ข้อมูลบัตรเครดิตที่ต้องการดำเนินการ</param>
        public PaymentResult ChargeCreditCard(PaymentInformation paymentInfo)
        {
            var tokenCredential = new OAuthTokenCredential(_appConfig.PaypalClientId, _appConfig.PaypalClientSecret, new Dictionary<string, string>());
            var accessToken = tokenCredential.GetAccessToken();
            var config = new Dictionary<string, string>();
            config.Add("mode", "sandbox"); // HACK: Paypal mode ('live' or 'sandbox')
            var apiContext = new APIContext
            {
                Config = config,
                AccessToken = accessToken
            };

            // A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. 
            var transaction = new Transaction()
            {
                amount = new Amount()
                {
                    currency = "USD",
                    total = paymentInfo.TotalPrice.ToString(),
                    details = new Details()
                    {
                        shipping = "0",
                        subtotal = paymentInfo.TotalPrice.ToString(),
                        tax = "0"
                    }
                },
                description = $"User { paymentInfo.UserProfileId } pay { paymentInfo.TotalPrice.ToString("C2") } for course { paymentInfo.PurchaseForCourseId }",
            };

            // A resource representing a Payer that funds a payment.
            var payer = new Payer()
            {
                payment_method = "credit_card",
                funding_instruments = new List<FundingInstrument>()
                {
                    new FundingInstrument()
                    {
                        credit_card = new CreditCard()
                        {
                            billing_address = new Address()
                            {
                                city = paymentInfo.City,
                                country_code = paymentInfo.Country,
                                line1 = paymentInfo.Address,
                                postal_code = paymentInfo.PostalCode,
                                state = paymentInfo.State
                            },
                            cvv2 = paymentInfo.CVV,
                            expire_month = paymentInfo.ExpiredMonth,
                            expire_year = paymentInfo.ExpiredYear,
                            first_name = paymentInfo.FirstName,
                            last_name = paymentInfo.LastName,
                            number = paymentInfo.CreditCardNumber,
                            type = paymentInfo.CardType.ToLower()
                        }
                     }
                },
                payer_info = new PayerInfo { email = paymentInfo.UserProfileId }
            };

            // A Payment resource; create one using the above types and intent as `sale` or `authorize`
            var payment = new PayPal.Api.Payment()
            {
                intent = "sale",
                payer = payer,
                transactions = new List<Transaction>() { transaction }
            };

            // Create a payment using a valid APIContext
            var createdPayment = payment.Create(apiContext);
            var result = PaymentResult.Unknow;
            return Enum.TryParse<PaymentResult>(createdPayment.state, out result) ? result : PaymentResult.Unknow;
        }
コード例 #3
0
 public void OAuthTokenCredentialInvalidClientIdTest()
 {
     try
     {
         var config = ConfigManager.Instance.GetProperties();
         config[BaseConstants.ClientId] = "abc";
         var oauthTokenCredential = new OAuthTokenCredential(config);
         TestingUtil.AssertThrownException<IdentityException>(() => oauthTokenCredential.GetAccessToken());
         this.RecordConnectionDetails();
     }
     catch (ConnectionException)
     {
         this.RecordConnectionDetails(false);
         throw;
     }
 }
コード例 #4
0
 public void OAuthTokenCredentialMissingClientSecretTest()
 {
     var config = ConfigManager.Instance.GetProperties();
     config[BaseConstants.ClientSecret] = "";
     var oauthTokenCredential = new OAuthTokenCredential(config);
     TestingUtil.AssertThrownException<MissingCredentialException>(() => oauthTokenCredential.GetAccessToken());
 }
コード例 #5
0
 private   APIContext getPaypalContext()
 {
     Dictionary<string, string> configurationPayment = new Dictionary<string, string>();
     configurationPayment.Add("mode", IsPayPalSandBox ? "sandbox" : "live");
     OAuthTokenCredential tokenCredential = new OAuthTokenCredential(clientId, Secret, configurationPayment);
     var api = new APIContext(tokenCredential.GetAccessToken());
     api.Config = configurationPayment;
     return api;
 }