• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-server / 1593965839

18 Dec 2024 10:58AM UTC coverage: 73.514% (+0.7%) from 72.829%
1593965839

Pull #253

gitlab-ci

mineralsfree
chore(gui): aligned tests with edit billing profile

Ticket: MEN-7466
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #253: MEN-7466-feat: updated billing section in My Organization settings

4257 of 6185 branches covered (68.83%)

Branch coverage included in aggregate %.

53 of 87 new or added lines in 11 files covered. (60.92%)

43 existing lines in 11 files now uncovered.

40083 of 54130 relevant lines covered (74.05%)

22.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.98
/frontend/src/js/components/settings/CardSection.tsx
1
// Copyright 2020 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useState } from 'react';
15

16
import { Button } from '@mui/material';
17

18
import InfoText from '@northern.tech/common-ui/infotext';
19
import Loader from '@northern.tech/common-ui/loader';
20
import storeActions from '@northern.tech/store/actions';
21
import { Organization } from '@northern.tech/store/organizationSlice/types';
22
import { useAppDispatch } from '@northern.tech/store/store';
23
import { cancelUpgrade } from '@northern.tech/store/thunks';
24
import { CardElement, useElements, useStripe } from '@stripe/react-stripe-js';
25

26
import stripeImage from '../../../assets/img/powered_by_stripe.png';
27

28
const { setSnackbar } = storeActions;
9✔
29

30
interface CardSectionProps {
31
  isSignUp: boolean;
32
  onClose?: () => void;
33
  organization: Organization;
34
  isValid?: boolean;
35
  onSubmit: () => Promise<void>;
36
  onCardConfirmed: () => void;
37
}
38
const CardSection = ({ isSignUp, onClose, organization, onSubmit, onCardConfirmed, isValid = true }: CardSectionProps) => {
9✔
39
  const stripe = useStripe();
1✔
40
  const elements = useElements();
1✔
41
  const [errors, setErrors] = useState(false);
1✔
42
  const [loading, setLoading] = useState(false);
1✔
43
  const [empty, setEmpty] = useState(true);
1✔
44
  const dispatch = useAppDispatch();
1✔
45
  const setSnackbarMessage = (message: string) => dispatch(setSnackbar(message));
1✔
46
  const onCancel = () => {
1✔
NEW
47
    dispatch(cancelUpgrade(organization.id));
×
NEW
48
    if (onClose) {
×
NEW
49
      onClose();
×
50
    }
51
  };
52

53
  const confirmCard = async secret => {
1✔
54
    // Use elements.getElement to get a reference to the mounted Element.
55
    const cardElement = elements.getElement(CardElement);
×
56

57
    // Use your card Element with other Stripe.js APIs
58
    try {
×
59
      const result = await stripe.confirmCardSetup(secret, {
×
60
        payment_method: {
61
          card: cardElement
62
        }
63
      });
64

65
      if (result.error) {
×
NEW
66
        setSnackbarMessage(`Error while confirming card: ${result.error.message}`);
×
67
        onCancel();
×
68
      } else {
NEW
69
        setSnackbarMessage(`Card confirmed. Updating your account...`);
×
NEW
70
        onCardConfirmed();
×
71
      }
72
    } catch (error) {
73
      console.error(error);
×
NEW
74
      setSnackbarMessage(`Something went wrong while submitting the form. Please contact support.`);
×
75
      onCancel();
×
76
    }
77
  };
78

79
  const handleSubmit = async event => {
1✔
80
    event.preventDefault();
×
81
    setLoading(true);
×
NEW
82
    return onSubmit()
×
83
      .then(confirmCard)
84
      .finally(() => setLoading(false));
×
85
  };
86

87
  const stripeElementChange = event => {
1✔
88
    setEmpty(false);
×
89
    if (event.complete) {
×
90
      // enable payment button
91
      setErrors(false);
×
92
    } else if (event.error) {
×
93
      // show validation to customer
94
      setErrors(true);
×
95
    }
96
  };
97

98
  return (
1✔
99
    <form className="margin-top-small" onSubmit={handleSubmit} onReset={onCancel}>
100
      <CardElement className="warning" onChange={event => stripeElementChange(event)} />
×
101
      {!!errors && <p className="warning">There is an error in the form. Please check that your details are correct</p>}
1!
102

103
      <div id="poweredByStripe">
104
        <div>All standard credit card fees apply (e.g. foreign transaction fee, if your card has one)</div>
105
        <img src={stripeImage} />
106
      </div>
107

108
      {isSignUp && <InfoText>Billing will be scheduled monthly, starting from today. You can cancel at any time.</InfoText>}
2✔
109

110
      <div className="flexbox center-aligned margin-top-small">
111
        {!isSignUp && (
1!
112
          <Button type="reset" disabled={loading} style={{ marginRight: 15 }}>
113
            Cancel
114
          </Button>
115
        )}
116
        <Button variant="contained" color="secondary" type="submit" disabled={errors || loading || empty || !isValid}>
3!
117
          {isSignUp ? 'Sign up' : 'Save'}
1!
118
        </Button>
119
      </div>
120
      <Loader show={loading} />
121
    </form>
122
  );
123
};
124

125
export default CardSection;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc