Example #1
0
        public void testGetAuthUrl()
        {
            Dictionary <String, String> configurationMap = new Dictionary <string, string>();
            string clientId     = "AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd";
            string clientSecret = "EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX";

            configurationMap.Add("mode", "sandbox");

            APIContext apiContext = new APIContext();

            apiContext.Config = configurationMap;
            List <string> scopelist = new List <string>();

            scopelist.Add("openid");
            scopelist.Add("email");
            string redirectURI = "https://devtools-paypal.com";
            string redirectURL = Session.GetRedirectURL(clientId, redirectURI, scopelist, apiContext);

            Console.WriteLine(redirectURL);
            CreateFromAuthorizationCodeParameters param = new CreateFromAuthorizationCodeParameters();

            param.setClientId(clientId);
            param.setClientSecret(clientSecret);
            // code you will get back as part of the url after redirection
            param.setCode("VxirnJHENB8k5slnoqZOdmjQcCMJRvbI-ispixwWHke-gsOh6XJaWQNJuTCTp3n3o6ttQs3VoNX1De3HOVpmH2PLN53PPedZcTujzLqYrlTS-CqKHYb5wb0NT2joumArOdEy51D4HgoCa46dxuPMm79nX40RQXRP8J0OQsgrEbhf_Kna");
            Tokeninfo          info           = Tokeninfo.CreateFromAuthorizationCode(apiContext, param);
            UserinfoParameters userinfoParams = new UserinfoParameters();

            userinfoParams.setAccessToken(info.access_token);
            Userinfo userinfo = Userinfo.GetUserinfo(apiContext, userinfoParams);

            Console.WriteLine("Email" + userinfo.email);
        }
Example #2
0
        public void TestCreateFromAuthorizationCodeDynamic()
        {
            Dictionary<string, string> configurationMap = new Dictionary<string, string>();
            configurationMap.Add("clientId", "");
            configurationMap.Add("clientSecret", "");
            configurationMap.Add("mode", "live");
            APIContext apiContext = new APIContext();
            apiContext.Config = configurationMap;
            CreateFromAuthorizationCodeParameters param = new CreateFromAuthorizationCodeParameters();

            // code you will get back as part of the url after redirection
            param.SetCode("xxxx");
            info = Tokeninfo.CreateFromAuthorizationCode(apiContext, param);
            Assert.AreEqual(true, info.access_token != null);
        }
        public void TestCreateFromAuthorizationCodeDynamic()
        {
            Dictionary <string, string> configurationMap = new Dictionary <string, string>();

            configurationMap.Add("clientId", "");
            configurationMap.Add("clientSecret", "");
            configurationMap.Add("mode", "live");
            APIContext apiContext = new APIContext();

            apiContext.Config = configurationMap;
            CreateFromAuthorizationCodeParameters param = new CreateFromAuthorizationCodeParameters();

            // code you will get back as part of the url after redirection
            param.SetCode("xxxx");
            info = Tokeninfo.CreateFromAuthorizationCode(apiContext, param);
            Assert.AreEqual(true, info.access_token != null);
        }
        public BasePaymentResponse LinkAccount(Guid accountId, string authCode)
        {
            try
            {
                var account = _accountDao.FindById(accountId);
                if (account == null)
                {
                    throw new Exception("Account not found.");
                }

                var authorizationCodeParameters = new CreateFromAuthorizationCodeParameters();
                authorizationCodeParameters.setClientId(GetClientId());
                authorizationCodeParameters.setClientSecret(GetSecret());
                authorizationCodeParameters.SetCode(authCode);

                // Get refresh and access tokens
                var tokenInfo = Tokeninfo.CreateFromAuthorizationCodeForFuturePayments(GetAPIContext(), authorizationCodeParameters);

                // Store access token securely
                _commandBus.Send(new LinkPayPalAccount
                {
                    AccountId             = accountId,
                    EncryptedRefreshToken = CryptoService.Encrypt(tokenInfo.refresh_token)
                });

                return(new BasePaymentResponse
                {
                    IsSuccessful = true,
                    Message = "Success"
                });
            }
            catch (Exception e)
            {
                _logger.LogMessage("PayPal: LinkAccount error");
                _logger.LogError(e);
                return(new BasePaymentResponse
                {
                    IsSuccessful = false,
                    Message = e.Message
                });
            }
        }
Example #5
0
 /// <summary>
 /// Creates an Access Token from an Authorization Code.
 /// <param name="apiContext">APIContext to be used for the call.</param>
 /// <param name="createFromAuthorizationCodeParameters">Query parameters used for API call</param>
 /// </summary>
 public static Tokeninfo CreateFromAuthorizationCode(APIContext apiContext, CreateFromAuthorizationCodeParameters createFromAuthorizationCodeParameters)
 {
     string pattern = "v1/identity/openidconnect/tokenservice?grant_type={0}&code={1}&redirect_uri={2}";
     object[] parameters = new object[] { createFromAuthorizationCodeParameters };
     string resourcePath = SDKUtil.FormatURIPath(pattern, parameters);
     string payLoad = resourcePath.Substring(resourcePath.IndexOf('?') + 1);
     resourcePath = resourcePath.Substring(0, resourcePath.IndexOf("?"));
     Dictionary<string, string> headersMap = new Dictionary<string, string>();
     headersMap.Add(BaseConstants.ContentTypeHeader, "application/x-www-form-urlencoded");
     if (apiContext == null)
     {
         apiContext = new APIContext();
     }
     apiContext.HTTPHeaders = headersMap;
     apiContext.MaskRequestId = true;
     return PayPalResource.ConfigureAndExecute<Tokeninfo>(apiContext, HttpMethod.POST, resourcePath, payLoad);
 }
Example #6
0
 /// <summary>
 /// Creates an Access Token from an Authorization Code.
 /// <param name="createFromAuthorizationCodeParameters">Query parameters used for API call</param>
 /// </summary>
 public static Tokeninfo CreateFromAuthorizationCode(CreateFromAuthorizationCodeParameters createFromAuthorizationCodeParameters)
 {
     return CreateFromAuthorizationCode(null, createFromAuthorizationCodeParameters);
 }
        /// <summary>
        /// Code example for creating a future payment object.
        /// </summary>
        /// <param name="correlationId"></param>
        /// <param name="authorizationCode"></param>
        private Payment CreateFuturePayment(string correlationId, string authorizationCode, string redirectUrl)
        {
            // ###Payer
            // A resource representing a Payer that funds a payment
            // Payment Method
            // as `paypal`
            Payer payer = new Payer()
            {
                payment_method = "paypal"
            };

            // ###Details
            // Let's you specify details of a payment amount.
            Details details = new Details()
            {
                tax      = "15",
                shipping = "10",
                subtotal = "75"
            };

            // ###Amount
            // Let's you specify a payment amount.
            var amount = new Amount()
            {
                currency = "USD",
                total    = "100", // Total must be equal to sum of shipping, tax and subtotal.
                details  = details
            };

            // # Redirect URLS
            var redirUrls = new RedirectUrls()
            {
                cancel_url = redirectUrl,
                return_url = redirectUrl
            };

            // ###Items
            // Items within a transaction.
            var itemList = new ItemList()
            {
                items = new List <Item>()
            };

            itemList.items.Add(new Item()
            {
                name     = "Item Name",
                currency = "USD",
                price    = "15",
                quantity = "5",
                sku      = "sku"
            });

            // ###Transaction
            // A transaction defines the contract of a
            // payment - what is the payment for and who
            // is fulfilling it.
            var transactionList = new List <Transaction>();

            // The Payment creation API requires a list of
            // Transaction; add the created `Transaction`
            // to a List
            transactionList.Add(new Transaction()
            {
                description = "Transaction description.",
                amount      = amount,
                item_list   = itemList
            });

            var authorizationCodeParameters = new CreateFromAuthorizationCodeParameters();

            authorizationCodeParameters.setClientId(Configuration.ClientId);
            authorizationCodeParameters.setClientSecret(Configuration.ClientSecret);
            authorizationCodeParameters.SetCode(authorizationCode);

            var apiContext = new APIContext();

            apiContext.Config = Configuration.GetConfig();

            var tokenInfo   = Tokeninfo.CreateFromAuthorizationCodeForFuturePayments(apiContext, authorizationCodeParameters);
            var accessToken = string.Format("{0} {1}", tokenInfo.token_type, tokenInfo.access_token);

            // ###Payment
            // A FuturePayment Resource
            this.futurePayment = new FuturePayment()
            {
                intent        = "authorize",
                payer         = payer,
                transactions  = transactionList,
                redirect_urls = redirUrls
            };
            return(this.futurePayment.Create(accessToken, correlationId));
        }
        /// <summary>
        /// Code example for creating a future payment object.
        /// </summary>
        /// <param name="correlationId"></param>
        /// <param name="authorizationCode"></param>
        private Payment CreateFuturePayment(string correlationId, string authorizationCode, string redirectUrl)
        {
            // ###Payer
            // A resource representing a Payer that funds a payment
            // Payment Method
            // as `paypal`
            Payer payer = new Payer()
            {
                payment_method = "paypal"
            };

            // ###Details
            // Let's you specify details of a payment amount.
            Details details = new Details()
            {
                tax = "15",
                shipping = "10",
                subtotal = "75"
            };

            // ###Amount
            // Let's you specify a payment amount.
            var amount = new Amount()
            {
                currency = "USD",
                total = "100", // Total must be equal to sum of shipping, tax and subtotal.
                details = details
            };

            // # Redirect URLS
            var redirUrls = new RedirectUrls()
            {
                cancel_url = redirectUrl,
                return_url = redirectUrl
            };

            // ###Items
            // Items within a transaction.
            var itemList = new ItemList() { items = new List<Item>() };
            itemList.items.Add(new Item()
            {
                name = "Item Name",
                currency = "USD",
                price = "15",
                quantity = "5",
                sku = "sku"
            });

            // ###Transaction
            // A transaction defines the contract of a
            // payment - what is the payment for and who
            // is fulfilling it. 
            var transactionList = new List<Transaction>();

            // The Payment creation API requires a list of
            // Transaction; add the created `Transaction`
            // to a List
            transactionList.Add(new Transaction()
            {
                description = "Transaction description.",
                amount = amount,
                item_list = itemList
            });

            var authorizationCodeParameters = new CreateFromAuthorizationCodeParameters();
		    authorizationCodeParameters.setClientId(Configuration.ClientId);
		    authorizationCodeParameters.setClientSecret(Configuration.ClientSecret);
		    authorizationCodeParameters.SetCode(authorizationCode);

		    var apiContext = new APIContext();
            apiContext.Config = Configuration.GetConfig();

            var tokenInfo = Tokeninfo.CreateFromAuthorizationCodeForFuturePayments(apiContext, authorizationCodeParameters);
            var accessToken = string.Format("{0} {1}", tokenInfo.token_type, tokenInfo.access_token);

            // ###Payment
            // A FuturePayment Resource
            this.futurePayment = new FuturePayment()
            {
                intent = "authorize",
                payer = payer,
                transactions = transactionList,
                redirect_urls = redirUrls
            };
            return this.futurePayment.Create(accessToken, correlationId);
        }