Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  padding: 20px;
}
.heading {
  text-align: center;
  font-size: 20px;
  margin-bottom: 20px;
}
.wrapper {
  max-width: 460px;
  margin: 0 auto;
}
form {
  margin-bottom: 30px;
  background: #fff;
  border: 1px solid #e0e0e0;
  box-sizing: border-box;
  border-radius: 12px;
  padding: 20px;
}
@media (min-width: 600px) {
  form {
    padding: 40px;
  }
}
button {
  display: block;
  position: relative;
  align-items: center;
  appearance: none;
  display: inline-flex;
  justify-content: center;
  line-height: 1;
  padding: 0 20px;
  height: 60px;
  text-decoration: none;
  background-color: #000645;
  border: none;
  color: #fff;
  border-radius: 3px;
  font-size: 16px;
  margin-top: 16px;
  width: 100%;
  cursor: pointer;
}
button:hover {
  box-shadow: 0px 1px 12px rgba(33, 0, 150, 0.25);
}
button:disabled {
  cursor: not-allowed;
}
.button-loading::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #000645;
}
.button-loading::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  width: 24px;
  height: 24px;
  border-color: currentColor;
  border-style: solid;
  border-radius: 99999px;
  border-width: 2px;
  border-left-color: transparent;
  color: #fff;
  animation: rotate 450ms linear 0ms infinite;
}
label {
  display: block;
  margin-bottom: 4px;
  font-size: 14px;
  color: #000645;
}
.field-container {
  margin-bottom: 20px;
}
.field-columns {
  display: flex;
  justify-content: space-between;
}
.field-columns > * {
  flex: 0 1 40%;
}
.hosted-field {
  height: 40px;
  padding: 0 12px;
  border: 1px solid #e0e0e0;
  border-radius: 3px;
}
.hosted-field-invalid {
  border-color: #cc0001;
}
.hosted-field-valid {
  border-color: #10b981;
}
.hosted-field-focus {
  border-color: #210096;
  box-shadow: 0px 1px 12px rgba(33, 0, 150, 0.25);
}
.error {
  display: inline-block;
  color: #cc0001;
  background-color: #ffe6e6;
  padding: 2px 8px;
  margin-top: 12px;
  font-size: 14px;
  border-radius: 3px;
}
.disclaimer {
  color: #828282;
  font-size: 14px;
  margin-top: 24px;
  text-align: center;
}
.response {
  display: none;
  margin-top: 16px;
  color: #fff;
  padding: 10px 20px;
  text-align: center;
  font-size: 14px;
}
.response-success {
  display: block;
  background-color: #dff9ec;
  color: #1b643a;
}
.response-error {
  display: block;
  background-color: #ffe6e6;
  color: #cc0001;
}
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

              
            
!

JS

              
                const fieldStyles = {
  input: {
    color: "#000645",
    "font-family": "system-ui, -apple-system, BlinkMacSystemFont, sans-serif",
    "letter-spacing": "0.02em"
  },
  ".invalid": {
    color: "#cc0001"
  },
  "::placeholder": {
    color: "#757575"
  },
  ".invalid .card-icon": {
    color: "#cc0001"
  }
};

function HostedFieldsForm({ onSuccess }) {
  const [isSubmitting, setIsSubmitting] = React.useState(false);
  const [error, setError] = React.useState("");
  const [fieldErrors, setFieldErrors] = React.useState({});

  React.useEffect(() => {
    // Initialise hosted fields
    const hostedFields = assembly.hostedFields({ environment: "pre-live" });
    const cardName = hostedFields.create("cardName", {
      placeholder: "Full Name",
      styles: fieldStyles
    });
    const cardNumber = hostedFields.create("cardNumber", {
      placeholder: "•••• •••• •••• ••••",
      styles: fieldStyles
    });
    const cardExpiry = hostedFields.create("cardExpiry", {
      placeholder: "MM/YY",
      styles: fieldStyles
    });
    const cardCvv = hostedFields.create("cardCvv", {
      placeholder: "•••",
      styles: fieldStyles
    });
    cardName.mount("#card-name-field");
    cardNumber.mount("#card-number-field");
    cardExpiry.mount("#card-expiry-field");
    cardCvv.mount("#card-cvv-field");

    const inputs = [cardName, cardNumber, cardExpiry, cardCvv];

    inputs.forEach(function (field) {
      field.on("change", (event) => {
        setFieldErrors((prevState) => ({
          ...prevState,
          [event.fieldType]: event.error ? event.error.message : undefined
        }));
      });
    });

    const form = document.querySelector("form");

    form.addEventListener("submit", (event) => {
      event.preventDefault();
      setIsSubmitting(true);
      setError(false);

      hostedFields
        .createCardAccount({
          token: "YOUR_AUTH_TOKEN",
          user_id: "USER_ID"
        })
        .then(function (response) {
          // handle create card account succeeded
          setIsSubmitting(false);
          onSuccess();
        })
        .catch(function (response) {
          // handle errors
          setIsSubmitting(false);
          if (response.errors && response.errors.token) {
            setError("Your token is not authorized");
          } else {
            setError("There was an error creating your card account.");
          }
        });
    });

    return function cleanup() {
      hostedFields.destroy();
    };
  }, []);

  return (
    <form>
      <div className="cardName-container field-container">
        <label htmlFor="card-name-field">Cardholder Name</label>
        <div id="card-name-field" className="hosted-field"></div>
        {fieldErrors.cardName && (
          <div className="error">{fieldErrors.cardName}</div>
        )}
      </div>
      <div className="cardNumber-container field-container">
        <label htmlFor="card-number-field">Card Number</label>
        <div id="card-number-field" className="hosted-field"></div>
        {fieldErrors.cardNumber && (
          <div className="error">{fieldErrors.cardNumber}</div>
        )}
      </div>
      <div className="field-columns">
        <div className="cardExpiry-container field-container">
          <label htmlFor="card-expiry-field">Expiry Date</label>
          <div id="card-expiry-field" className="hosted-field"></div>
          {fieldErrors.cardExpiry && (
            <div className="error">{fieldErrors.cardExpiry}</div>
          )}
        </div>
        <div className="cardCvv-container field-container">
          <label htmlFor="card-cvv-field">CVV</label>
          <div id="card-cvv-field" className="hosted-field"></div>
          {fieldErrors.cardCvv && (
            <div className="error">{fieldErrors.cardCvv}</div>
          )}
        </div>
      </div>
      <button
        className={isSubmitting ? "button-loading" : ""}
        disabled={isSubmitting}
      >
        Submit
      </button>
      {error && <div className="response response-error">{error}</div>}
      <div className="disclaimer">
        Payment securely processed by Assembly Payments.
      </div>
    </form>
  );
}

function App() {
  const [showSuccessView, setShowSuccessView] = React.useState(false);

  return (
    <div className="App">
      <div className="wrapper">
        <h2 className="heading">React integration example</h2>
        {showSuccessView ? (
          <div>
            <div className="response response-success">
              Card account successfully created.
            </div>
            <button onClick={() => setShowSuccessView(false)}>
              Create another card account
            </button>
          </div>
        ) : (
          <HostedFieldsForm onSuccess={() => setShowSuccessView(true)} />
        )}
      </div>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console