コード例 #1
0
        /// <summary>
        /// Get Auth0 Token
        /// </summary>
        /// <param name="clientTokenRequest">Client Token Request</param>
        /// <param name="cancellationToken">Cancellation Token</param>
        /// <returns>Client Token Response</returns>
        private async Task <ClientTokenResponse> GetClientTokenAsync(ClientTokenRequest clientTokenRequest, CancellationToken cancellationToken)
        {
            var requestJson = JsonConvert.SerializeObject(clientTokenRequest);

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders
                .Accept
                .Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(CONTENT_TYPE));

                using (var request = new HttpRequestMessage(HttpMethod.Post, _gcpTokenServiceUrl))
                {
                    request.Content = new StringContent(requestJson, Encoding.UTF8, CONTENT_TYPE);

                    using (var response = await client.SendAsync(request, cancellationToken))
                    {
                        var content = await response.Content.ReadAsStringAsync();

                        if (response.IsSuccessStatusCode == false)
                        {
                            throw new Exception(content + "Status Code :" + response.StatusCode.ToString());
                        }

                        return(JsonConvert.DeserializeObject <ClientTokenResponse>(content));
                    }
                }
            }
        }
コード例 #2
0
        public PurchaseClientTokenResponse Get(PurchaseClientTokenRequest request)
        {
            var gateway = config.GetGateway();
            var user    = ValidateAndGetCurrentUser();

            try
            {
                if (user.BraintreeCustomerId != null)
                {
                    Customer customer = gateway.Customer.Find(user.BraintreeCustomerId);
                }
                else
                {
                    throw new ArgumentException("Missing BraintreeCustomerId");
                }
            }
            catch (Exception ex)
            {
                CreateCustomer(user);
            }

            var customerId = user.BraintreeCustomerId;

            var clientTokenRequest = new ClientTokenRequest {
                CustomerId = customerId
            };
            var clientToken = gateway.ClientToken.generate(clientTokenRequest);

            return(new PurchaseClientTokenResponse {
                Token = clientToken
            });
        }
コード例 #3
0
        public async Task <IActionResult> GetAccessTokenAsync(string clientId, string clientSecret)
        {
            string token = string.Empty;

            try
            {
                var clientTokenRequest = new ClientTokenRequest
                {
                    ClientId     = clientId,
                    ClientSecret = clientSecret,
                    Audience     = _taaDocumentAPIAudience,
                    GrantType    = _grantType
                };

                var response = await GetClientTokenAsync(clientTokenRequest, CancellationToken.None);

                return(Ok(response));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to fetch Access Token -> Error Message:{ex.Message}");
            }

            return(BadRequest("Client Data Not Recognised!"));
        }
コード例 #4
0
        public ActionResult GetClientToken(string device)
        {
            try
            {
                string authorization = _httpContext.Request.Headers["Authorization"];
                var    customer      = GetCustomerFromToken(authorization, device);
                if (customer != null)
                {
                    var    payPalStandardPaymentSettings = _settingService.LoadSetting <PayPalStandardPaymentSettings>(_storeContext.CurrentStore.Id);
                    string environment       = payPalStandardPaymentSettings.UseSandbox ? "sandbox" : "product";
                    string merchantId        = payPalStandardPaymentSettings.MerchantId;
                    string publicKey         = payPalStandardPaymentSettings.PublicKey;
                    string privateKey        = payPalStandardPaymentSettings.PrivateKey;
                    string merchantAccountId = payPalStandardPaymentSettings.MerchantAccountId;

                    var braintreeGateway   = new BraintreeGateway(environment, merchantId, publicKey, privateKey);
                    var clientTokenRequest = new ClientTokenRequest();
                    clientTokenRequest.MerchantAccountId = merchantAccountId;
                    string token = braintreeGateway.ClientToken.Generate(clientTokenRequest);
                    //string token = braintreeGateway.ClientToken.Generate();
                    return(Json(new { Data = token }));
                }
            }
            catch (Exception e) { }
            return(Json(new { Errors = "E_1000" }));
        }
コード例 #5
0
        /// <summary>
        /// Generates the client token
        /// </summary>
        /// <param name="customerId">The id of customer</param>
        /// <returns></returns>
        public GenerateClientTokenResponse GenerateClientToken(String customerId = null)
        {
            var request = new ClientTokenRequest
            {
                CustomerId        = customerId,
                MerchantAccountId = _configurationProvider["BraintreeMerchantAccountId"],
            };

            if (!String.IsNullOrEmpty(customerId))
            {
                request.Options = new ClientTokenOptionsRequest
                {
                    VerifyCard = true,
                };
            }

            var result = _gateway.ClientToken.generate(request);

            var response = new GenerateClientTokenResponse
            {
                IsSuccess = !String.IsNullOrWhiteSpace(result),
            };

            if (response.IsSuccess)
            {
                response.ClientToken = result;
            }
            else
            {
                response.Errors = CollectResponseErrors(new ValidationErrors(), "Unable to generate client token");
            }

            return(response);
        }
コード例 #6
0
ファイル: TestHelper.cs プロジェクト: ronin1/braintree_dotnet
 public static string GenerateDecodedClientToken(BraintreeGateway gateway, ClientTokenRequest request = null)
 {
   var encodedClientToken = gateway.ClientToken.Generate(request);
   var decodedClientToken = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(encodedClientToken));
   var unescapedClientToken = System.Text.RegularExpressions.Regex.Unescape(decodedClientToken);
   return unescapedClientToken;
 }
コード例 #7
0
        public static string GenerateDecodedClientToken(BraintreeGateway gateway, ClientTokenRequest request = null)
        {
            var encodedClientToken   = gateway.ClientToken.generate(request);
            var decodedClientToken   = Encoding.UTF8.GetString(Convert.FromBase64String(encodedClientToken));
            var unescapedClientToken = Regex.Unescape(decodedClientToken);

            return(unescapedClientToken);
        }
コード例 #8
0
        public string GetToken([FromBody] string email)
        {
            ClientTokenRequest request = null;
            var id = this.GetCustomerId(email);

            if (id != null)
            {
                request = new ClientTokenRequest
                {
                    CustomerId = id
                };
            }

            return(this.gateway.Value.ClientToken.generate(request));
        }
コード例 #9
0
        /// <summary>
        /// Creates a <see cref="ClientTokenRequest"/>.
        /// </summary>
        /// <param name="customerKey">
        /// The customer key.
        /// </param>
        /// <param name="merchantAccountId">
        /// The merchant account Id
        /// </param>
        /// <returns>
        /// The <see cref="ClientTokenRequest"/>.
        /// </returns>
        public ClientTokenRequest CreateClientTokenRequest(Guid customerKey, string merchantAccountId)
        {
            var ctr = new ClientTokenRequest();

            if (customerKey != Guid.Empty)
            {
                ctr.CustomerId = customerKey.ToString();
            }

            if (!string.IsNullOrWhiteSpace(merchantAccountId))
            {
                ctr.MerchantAccountId = merchantAccountId;
            }

            return(ctr);
        }