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
 public void GetAccessTokenTest()
 {
     OAuthTokenCredential target = new OAuthTokenCredential(ClientID, ClientSecret); // TODO: Initialize to an appropriate value
     string expected = target.GetAccessToken();
     string actual = target.GetAccessToken();
     Assert.IsTrue(actual.ToLower().Contains("bearer"));
 }
Beispiel #2
0
        public static APIContext Create()
        {
            var config = ConfigManager.Instance.GetProperties();

            var credentials = new OAuthTokenCredential(
                config[BaseConstants.ClientId],
                config[BaseConstants.ClientSecret]
                
                );

            // ### Api Context
            // Pass in a `APIContext` object to authenticate 
            // the call and to send a unique request id 
            // (that ensures idempotency). The SDK generates
            // a request id if you do not pass one explicitly. 
            var apiContext = new APIContext(credentials.GetAccessToken())
            {
                Config = config
            };

            // Use this variant if you want to pass in a request id  
            // that is meaningful in your application, ideally 
            // a order id.
            // String requestId = Long.toString(System.nanoTime();
            // APIContext apiContext = new APIContext(GetAccessToken(), requestId ));

            return apiContext;
        }
 /// <summary>
 /// Helper method for getting an access token for test purposes.
 /// </summary>
 /// <param name="endpoint"></param>
 /// <param name="clientId"></param>
 /// <param name="clientSecret"></param>
 /// <returns></returns>
 private string GetAccessToken(string endpoint, string clientId, string clientSecret)
 {
     Dictionary<string, string> config = new Dictionary<string, string>();
     config.Add("endpoint", endpoint);
     OAuthTokenCredential target = new OAuthTokenCredential(clientId, clientSecret, config);
     return target.GetAccessToken();
 }
Beispiel #4
0
        //Retriving the Token
        public static string GetPayPalToken()
        {
            OAuthTokenCredential tokenCredential = new OAuthTokenCredential(clientID, clientSecret);
            string accessToken = tokenCredential.GetAccessToken();

            return accessToken;
        }
 public void GetAccessTokenTimeoutTest()
 {
     Dictionary<string, string> config = new Dictionary<string, string>();
     config[BaseConstants.ApplicationModeConfig] = BaseConstants.SandboxMode;
     config[BaseConstants.HttpConnectionTimeoutConfig] = "10";
     OAuthTokenCredential target = new OAuthTokenCredential(clientId, clientSecret, config);
     string accessToken = target.GetAccessToken();
 }
        public string EfetuarCompra(PessoaFisica pessoaFisica, CreditCard creditCard, int quantidadeFits)
        {
            string valor = (quantidadeFits * valorFits).ToString();

            Dictionary<string, string> payPalConfig = new Dictionary<string, string>();
            payPalConfig.Add("mode", this.mode);

            OAuthTokenCredential tokenCredential = new OAuthTokenCredential(this.clientId, this.clientSecret, payPalConfig);

            string accessToken = tokenCredential.GetAccessToken();

            Address billingAddress = new Address();
            billingAddress.line1 = string.Format("{0} Num {1}",pessoaFisica.Endereco.Rua, pessoaFisica.Endereco.Numero) ;
            billingAddress.city = pessoaFisica.Endereco.Cidade;
            billingAddress.country_code = "BR";
            billingAddress.postal_code = pessoaFisica.Endereco.CEP;
            billingAddress.state = pessoaFisica.Endereco.Estado;

            creditCard.billing_address = billingAddress;

            Details amountDetails = new Details();
            amountDetails.subtotal = valor;
            amountDetails.tax = "0.00";
            amountDetails.shipping = "0.00";

            Amount amount = new Amount();
            amount.total = (quantidadeFits * valorFits).ToString();
            amount.currency = "USD";
            amount.details = amountDetails;

            Transaction transaction = new Transaction();
            transaction.amount = amount;
            transaction.description = string.Format("Este pagamento foi efetuado por {0}, na quantia de {1} fits", pessoaFisica.Nome, quantidadeFits);

            List<Transaction> transactions = new List<Transaction>();
            transactions.Add(transaction);

            FundingInstrument fundingInstrument = new FundingInstrument();
            fundingInstrument.credit_card = creditCard;

            List<FundingInstrument> fundingInstruments = new List<FundingInstrument>();
            fundingInstruments.Add(fundingInstrument);

            Payer payer = new Payer();
            payer.funding_instruments = fundingInstruments;
            payer.payment_method = "credit_card";

            Payment payment = new Payment();
            payment.intent = "sale";
            payment.payer = payer;
            payment.transactions = transactions;

            Payment createdPayment = payment.Create(accessToken);

            return valor;
        }
 public void GetAccessTokenTest()
 {
     Dictionary<string, string> config = new Dictionary<string, string>();
     config.Add("endpoint", "https://api.sandbox.paypal.com");
     string clientId = "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM";
     string clientSecret = "EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM";
     OAuthTokenCredential target = new OAuthTokenCredential(clientId, clientSecret, config);
     string expected = string.Empty;
     string actual;
     actual = target.GetAccessToken();
     Assert.AreEqual(true, actual.StartsWith("Bearer "));
 }
Beispiel #8
0
		/// <summary>
		/// Helper: Holt von PayPal ein neues Accesstoken.
		/// </summary>
		/// <returns></returns>
		public APIContext GetPayPalToken()
		{
            var config = new Dictionary<string, string>();
            config.Add("clientId", ConfigurationManager.AppSettings["PayPal:Username"]);
            config.Add("clientSecret", ConfigurationManager.AppSettings["PayPal:Password"]);
            if (ConfigurationManager.AppSettings["PayPal:Sandbox"] != "False") {
                config.Add("mode", "sandbox");
            }

            var tokenCredential = new OAuthTokenCredential(
                config["clientId"],
                config["clientSecret"],
                config);

            var apiContext = new APIContext(tokenCredential.GetAccessToken());
            apiContext.Config = config;

			return apiContext;
		}
Beispiel #9
0
 public static string AccessToken()
 {
     OAuthTokenCredential token = new OAuthTokenCredential(APIKey, APISecret);
     return token.GetAccessToken();
 }
Beispiel #10
0
        private APIContext GetApiContext()
        {
            Dictionary<string, string> payPalConfig = new Dictionary<string, string>();

            if (this._isSandbox)
            {
                payPalConfig.Add("mode", "sandbox");
            }
            else
            {
                payPalConfig.Add("endpoint", this._payPalUrl);
            }

            OAuthTokenCredential tokenCredential = new OAuthTokenCredential(this._payPalClientId, this._payPalSecret, payPalConfig);
            string accessToken = tokenCredential.GetAccessToken();

            APIContext context = new APIContext(accessToken);
            context.Config = payPalConfig;
            return context;
        }
        private PayPalRest.Payment CreatePayPalPayment(ProcessPaymentRequest request, PayPalConfig settings)
        {
            var config = new Dictionary<string, string>();
            config.Add("mode", settings.SandboxMode ? "sandbox" : "live");

            var credentials = new OAuthTokenCredential(settings.ClientId, settings.ClientSecret, config);
            var accessToken = credentials.GetAccessToken();
            var payment = new PayPalRest.Payment
            {
                intent = "sale",
                payer = new Payer
                {
                    payment_method = "credit_card",
                    funding_instruments = new List<PayPalRest.FundingInstrument>
                    {
                        new PayPalRest.FundingInstrument
                        {
                            credit_card = CreateCreditCard(request)
                        }
                    }
                },
                transactions = new List<Transaction> { CreateTransaction(request) }
            };

            return payment.Create(new APIContext(accessToken)
            {
                Config = config
            });
        }