Exemplo n.º 1
0
 public string GenerateStripeAccessToken(string authorization_code,
                                         Contracts.Enums.Channels channel,
                                         long eventId)
 {
     try
     {
         var eventStripeAccount = _eventStripeAccountMappingRepository.GetByEventId(eventId);
         StripeConfiguration.ApiKey = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Stripe.Feel.SecretKey);
         if (channel == Contracts.Enums.Channels.Feel && eventStripeAccount != null && eventStripeAccount.StripeAccountId == Contracts.Enums.StripeAccount.StripeAustralia) // If event belongs to StripeAustralia
         {
             StripeConfiguration.ApiKey = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Stripe.FeelAustralia.SecretKey);
         }
         else if (channel == Contracts.Enums.Channels.Feel && eventStripeAccount != null && eventStripeAccount.StripeAccountId == Contracts.Enums.StripeAccount.StripeIndia) // If event belongs to StripeIndia
         {
             StripeConfiguration.ApiKey = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Stripe.FeelIndia.SecretKey);
         }
         if (channel == Contracts.Enums.Channels.Website)
         {
             StripeConfiguration.ApiKey = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Stripe.SecretKey);
         }
         var options = new OAuthTokenCreateOptions
         {
             GrantType = "authorization_code",
             Code      = authorization_code,
         };
         var service  = new OAuthTokenService();
         var response = service.Create(options);
         return(response.StripeUserId);
     }
     catch (Exception e)
     {
         _logger.Log(FIL.Logging.Enums.LogCategory.Error, e);
         return("");
     }
 }
Exemplo n.º 2
0
        public virtual async Task <OAuthToken> Create(OAuthTokenCreateOptions createOptions)
        {
            var url = this.ApplyAllParameters(createOptions, Urls.OAuthToken, false);

            var response = await Requestor.Post(url, true);

            return(Mapper <OAuthToken> .MapFromJson(response));
        }
        public async Task <IActionResult> CreateAccountAsync([FromBody] CreateAccountDto createAccountDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage) }));
            }

            StripeConfiguration.ApiKey = "sk_test_dEYerF4aiezK453envsRBmWZ";

            try
            {
                if ((await _accountsRepository.GetByUserId(User.FindFirstValue(ClaimTypes.Name))) != null)
                {
                    throw new InvalidStripeUser("Account already exists");
                }

                var options = new OAuthTokenCreateOptions
                {
                    GrantType = "authorization_code",
                    Code      = createAccountDto.AuthorizationCode
                };

                var service  = new OAuthTokenService();
                var response = await service.CreateAsync(options);

                if (response != null)
                {
                    var account = new Account
                    {
                        UserId              = createAccountDto.UserId,
                        AccessToken         = response.AccessToken,
                        LiveMode            = response.Livemode,
                        RefreshToken        = response.RefreshToken,
                        StripePublisableKey = response.StripePublishableKey,
                        StripeUserId        = response.StripeUserId,
                        Scope = response.Scope
                    };

                    var createdAccount = await _accountsRepository.Create(account);

                    var createdAccountDto = _mapper.Map <CreatedAccountDto>(createdAccount);

                    return(Ok(response.StripeUserId));
                }

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest(new MessageObj(ex.Message)));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> HandleOAuthRedirect(
            [FromQuery] string state,
            [FromQuery] string code
            )
        {
            var service = new OAuthTokenService(_client);

            // Assert the state matches the state you provided in the OAuth link (optional).
            if (!StateMatches(state).Result)
            {
                return(StatusCode(
                           StatusCodes.Status403Forbidden,
                           Json(new { Error = string.Format("Incorrect state parameter: {0}", state) })
                           ));
            }

            // Send the authorization code to Stripe's API.
            var options = new OAuthTokenCreateOptions
            {
                GrantType = "authorization_code",
                Code      = code
            };

            OAuthToken response = null;

            try
            {
                response = service.Create(options);
            }
            catch (StripeException e)
            {
                Log.Error("stripe error: {0}", e);
                if (e.StripeError != null && e.StripeError.Error == "invalid_grant")
                {
                    return(StatusCode(
                               StatusCodes.Status400BadRequest,
                               Json(new { Error = string.Format("Invalid authorization code: {0}", code) })
                               ));
                }
                return(StatusCode(
                           StatusCodes.Status500InternalServerError,
                           Json(new { Error = "An unknown error occurred." })
                           ));
            }

            var connectedAccountId = response.StripeUserId;

            await SaveAccountId(connectedAccountId);

            // Render some HTML or redirect to a different page.
            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 5
0
        public async Task <OAuthToken> CreateOAuthTokenAsync(string code)
        {
            var options = new OAuthTokenCreateOptions
            {
                ClientSecret = StripeConfiguration.ApiKey,
                Code         = code,
                GrantType    = "authorization_code"
            };

            var service = new Stripe.OAuthTokenService();

            return(await service.CreateAsync(options));
        }
Exemplo n.º 6
0
        private OAuthToken CreateStripeOAuthToken(string code)
        {
            var options = new OAuthTokenCreateOptions
            {
                GrantType = "authorization_code",
                Code      = code,
            };
            OAuthToken oAuthToken = null;
            var        service    = new OAuthTokenService(_client);

            oAuthToken = service.Create(options);
            return(oAuthToken);
        }
Exemplo n.º 7
0
        public IActionResult HandleOAuthRedirect([FromQuery] string state, [FromQuery] string code)
        {
            var service = new OAuthTokenService(client);

            // Assert the state matches the state you provided in the OAuth link (optional).
            //if (!StateMatches(state))
            //{
            //     return StatusCode(
            //        StatusCodes.Status403Forbidden,
            //       Json(new { Error = String.Format("Incorrect state parameter: {0}", state) })
            //    );
            //}

            // Send the authorization code to Stripe's API.
            var options = new OAuthTokenCreateOptions
            {
                GrantType = "authorization_code",
                Code      = code,
            };

            OAuthToken response = null;

            try
            {
                response = service.Create(options);
            }
            catch (StripeException e)
            {
                if (e.StripeError != null && e.StripeError.Error == "invalid_grant")
                {
                    return(StatusCode(
                               StatusCodes.Status400BadRequest,
                               Json(new { Error = String.Format("Invalid authorization code: {0}", code) })
                               ));
                }
                else
                {
                    return(StatusCode(
                               StatusCodes.Status500InternalServerError,
                               Json(new { Error = "An unknown error occurred." })
                               ));
                }
            }

            var connectedAccountId = response.StripeUserId;

            SaveAccountId(connectedAccountId);

            // Render some HTML or redirect to a different page.
            return(new OkObjectResult(Json(new { Success = true })));
        }
Exemplo n.º 8
0
        public OAuthTokenServiceTest(
            StripeMockFixture stripeMockFixture,
            MockHttpClientFixture mockHttpClientFixture)
            : base(stripeMockFixture, mockHttpClientFixture)
        {
            this.service = new OAuthTokenService(this.StripeClient);

            this.createOptions = new OAuthTokenCreateOptions
            {
                Code      = AuthorizationCode,
                GrantType = "authorization_code",
            };

            this.deauthorizeOptions = new OAuthDeauthorizeOptions
            {
                ClientId     = ClientId,
                StripeUserId = AccountId,
            };
        }
        public async Task <IActionResult> StripeCallback(string code)
        {
            var userId = this.userManager.GetUserId(this.User);
            var user   = await this.userManager.FindByIdAsync(userId);

            var options = new OAuthTokenCreateOptions()
            {
                GrantType = "authorization_code",
                Code      = code,
            };

            var service  = new OAuthTokenService();
            var response = service.Create(options);

            user.StripeConnectedAccountId = response.StripeUserId;
            user.StripePublishableKey     = response.StripePublishableKey;
            user.StripeRefreshToken       = response.RefreshToken;
            // user.StripeAccessToken = response.AccessToken;

            await this.userManager.UpdateAsync(user);

            if (this.User.IsInRole(ManagerRoleName))
            {
                return(this.RedirectToAction("Index", "Dashboard", new { area = string.Empty })
                       .WithSuccess(string.Empty, SuccessfullyRegistered));
            }

            if (this.User.IsInRole(ViewerRoleName))
            {
                return(this.RedirectToAction("Create", "Listings", new { area = string.Empty })
                       .WithSuccess(string.Empty, SuccessfullyRegistered));
            }

            return(this.RedirectToAction("Create", "Listings", new { area = ManagementArea })
                   .WithSuccess(string.Empty, SuccessfullyRegistered));
        }