Skip to content

Commit

Permalink
Merge pull request #283 from amihaiemil/280
Browse files Browse the repository at this point in the history
#280 Stripe BillingInfo front-end form and validation (fe + be)
  • Loading branch information
amihaiemil authored Dec 21, 2020
2 parents 9a7be6e + 492bb31 commit a23ce53
Show file tree
Hide file tree
Showing 12 changed files with 1,399 additions and 277 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

<properties>
<java.version>11</java.version>
<self.core.version>0.0.43</self.core.version>
<self.storage.version>0.0.20</self.storage.version>
<self.core.version>0.0.44</self.core.version>
<self.storage.version>0.0.21</self.storage.version>
</properties>

<dependencies>
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/selfxdsd/selfweb/api/WalletsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import com.selfxdsd.api.*;
import com.selfxdsd.api.exceptions.WalletAlreadyExistsException;
import com.selfxdsd.selfweb.api.input.StripeWalletInput;
import static com.selfxdsd.selfweb.api.input.StripeWalletInput.*;
import com.selfxdsd.selfweb.api.output.JsonWallet;
import com.selfxdsd.selfweb.api.output.JsonWallets;
import org.slf4j.Logger;
Expand All @@ -34,6 +36,7 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -99,6 +102,7 @@ public ResponseEntity<String> wallets(
* Create a Stripe Wallet for one of the authenticated user's projects.
* @param owner Owner of the project (login of user or org name).
* @param name Repo name.
* @param billingInfo Billing information.
* @return Stripe Wallet as JSON string.
*/
@PostMapping(
Expand All @@ -107,8 +111,8 @@ public ResponseEntity<String> wallets(
)
public ResponseEntity<String> createStripeWallet(
@PathVariable final String owner,
@PathVariable final String name
) {
@PathVariable final String name,
@Valid final StripeWalletInput billingInfo) {
ResponseEntity<String> response;
final Project found = this.user.projects().getProjectById(
owner + "/" + name, user.provider().name()
Expand All @@ -119,7 +123,11 @@ public ResponseEntity<String> createStripeWallet(
try {
response = ResponseEntity.ok(
new JsonWallet(
found.createStripeWallet()
found.createStripeWallet(
new StripeBillingInfo(
billingInfo
)
)
).toString()
);
} catch (final WalletAlreadyExistsException ex) {
Expand Down
181 changes: 181 additions & 0 deletions src/main/java/com/selfxdsd/selfweb/api/input/StripeWalletInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* Copyright (c) 2020, Self XDSD Contributors
* All rights reserved.
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to read the Software only. Permission is hereby NOT GRANTED to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.selfxdsd.selfweb.api.input;

import com.selfxdsd.api.BillingInfo;
import com.selfxdsd.selfweb.api.input.validators.NoSpecialChars;
import com.selfxdsd.selfweb.api.input.validators.ValidCountry;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

/**
* Input for a new Stripe wallet (project billing info).
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 0.0.1
* @checkstyle HiddenField (200 lines)
* @checkstyle JavadocVariable (200 lines)
* @checkstyle JavadocMethod (200 lines)
*/
public final class StripeWalletInput {

@NotBlank(message = "Legal name is mandatory!")
@NoSpecialChars
private String legalName;

@NotBlank(message = "Country is mandatory!")
@NoSpecialChars
@ValidCountry
private String country;

@NotBlank(message = "Address is mandatory!")
@NoSpecialChars
private String address;

@NotBlank(message = "City is mandatory!")
@NoSpecialChars
private String city;

@NotBlank(message = "Zipcode is mandatory!")
@NoSpecialChars
private String zipcode;

@NotBlank(message = "E-Mail is mandatory!")
@Email(message = "Please provide a valid e-mail address.")
@NoSpecialChars
private String email;

@NoSpecialChars
private String other;

public String getLegalName() {
return this.legalName;
}

public void setLegalName(final String legalName) {
this.legalName = legalName;
}

public String getCountry() {
return this.country;
}

public void setCountry(final String country) {
this.country = country;
}

public String getAddress() {
return this.address;
}

public void setAddress(final String address) {
this.address = address;
}

public String getCity() {
return this.city;
}

public void setCity(final String city) {
this.city = city;
}

public String getZipcode() {
return this.zipcode;
}

public void setZipcode(final String zipcode) {
this.zipcode = zipcode;
}

public String getEmail() {
return this.email;
}

public void setEmail(final String email) {
this.email = email;
}

public String getOther() {
return this.other;
}

public void setOther(final String other) {
this.other = other;
}

/**
* Billing info from Stripe input form.
*/
public static final class StripeBillingInfo implements BillingInfo {

/**
* Input from the Stripe wallet formular.
*/
private StripeWalletInput input;

/**
* Ctor.
* @param input Input from the Stripe wallet form.
*/
public StripeBillingInfo(final StripeWalletInput input) {
this.input = input;
}

@Override
public String legalName() {
return this.input.getLegalName();
}

@Override
public String country() {
return this.input.getCountry();
}

@Override
public String address() {
return this.input.getAddress();
}

@Override
public String city() {
return this.input.getCity();
}

@Override
public String zipcode() {
return this.input.getZipcode();
}

@Override
public String email() {
return this.input.getEmail();
}

@Override
public String other() {
return this.input.getOther();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2020, Self XDSD Contributors
* All rights reserved.
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to read the Software only. Permission is hereby NOT GRANTED to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.selfxdsd.selfweb.api.input.validators;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;

/**
* Annotation for String inputs. Forbids the mentioned special chars.
* @author Mihai Andronache ([email protected]
* @version $Id$
* @since 0.0.1
* @checkstyle JavadocMethod (200 lines)
*/
@Documented
@Target({ FIELD })
@Constraint(validatedBy = NoSpecialCharsValidator.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoSpecialChars {

String message() default "No special chars allowed.";

/**
* The forbidden chars.
* By default, the forbidden chars are: "[](){};<>/\`!%^*$?
* @return String containing the forbidden chars.
*/
String forbidden() default "\"[](){};<>/\\`!%^*$?";

Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2020, Self XDSD Contributors
* All rights reserved.
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to read the Software only. Permission is hereby NOT GRANTED to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.selfxdsd.selfweb.api.input.validators;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
* Validator for the annotation {@link NoSpecialChars}.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 0.0.1
*/
public final class NoSpecialCharsValidator
implements ConstraintValidator<NoSpecialChars, String> {

/**
* The forbidden chars.
*/
private String forbidden;

@Override
public void initialize(final NoSpecialChars constraintAnnotation) {
this.forbidden = constraintAnnotation.forbidden();
}

@Override
public boolean isValid(
final String value,
final ConstraintValidatorContext context
) {
boolean isValid = true;
for (int i = 0; i < this.forbidden.length(); i++) {
if(value.contains(String.valueOf(this.forbidden.charAt(i)))) {
isValid = false;
break;
}
}
return isValid;
}
}
Loading

0 comments on commit a23ce53

Please sign in to comment.