<commit_before>import reactGA from 'react-ga';
import {Navigation} from 'react-router';
import amountModifier from '../scripts/amount-modifier';
import listener from '../scripts/listener.js';
import dispatcher from '../scripts/dispatcher.js';
import form from '../scripts/form.js';

module.exports = {
  mixins: [Navigation],
  getInitialState: function() {
    return {
      paymentType: "",
      submitting: false,
      showCvcHint: false,
      currency: this.props.currency
    };
  },
  componentDidMount: function() {
    form.updateState("currency", this.state.currency);
    form.updateState("presets", this.props.presets);
    form.updateField("amount", this.props.amount);
    form.updateField("frequency", this.props.frequency);
    form.updateField("country", this.props.country);

    listener.on("stateUpdated", this.onStateUpdated);
    listener.on("toggleCvcHint", this.onToggleCvcHint);
    listener.on("toPage", this.toThisPage);
    listener.on("nextPage", this.nextPage);
  },
  componentWillUnmount: function() {
    listener.off("stateUpdated", this.onStateUpdated);
    listener.off("toggleCvcHint", this.onToggleCvcHint);
    listener.off("toPage", this.toThisPage);
    listener.off("nextPage", this.nextPage);
  },
  onStateUpdated: function(e) {
    var detail = e.detail;
    var state = detail.state;
    var value = detail.value;
    if (state === "currency") {
      this.setState({
        currency: value
      });
    }
  },
  onToggleCvcHint: function() {
    this.setState({
      showCvcHint: !this.state.showCvcHint
    });
  },
  nextPage: function(e) {
    dispatcher.fire("toPage", {
      page: this.state.activePage+1
    });
  },
  toThisPage: function(e) {
    var index = e.detail.page;
    if (this.state.activePage === index) {
      return;
    }
    this.setState({
      activePage: index
    });

    // Index starts at 0, and our page tracking starts at 1.
    var page = index+1;
    // Build the page the way it is expected.
    var currentPage = window.location.pathname + "#page-" + page;
    // These are virtual pageviews, so we track them manually in GA
    reactGA.pageview(currentPage);
  },
  submit: function(action, props, success, error) {
    this.setState({
      submitting: true
    });

    props.locale = this.props.locales[0];
    var currency = this.state.currency;
    if (currency) {
      props.currency = currency.code;
    }

    fetch(action, {
      method: 'post',
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(props)
    }).then(function(response) {
      var responseContent;
      var callback = success;
      if (!response.headers.get("content-type")) {
        responseContent = response.text();
      } else {
        responseContent = response.json();
      }
      if (!response.ok) {
        callback = error;
      }
      responseContent.then(function(result) {
        if (callback) {
          callback(result);
        }
      });
    });
  },
  stripeSuccess: function(data) {
    var transactionId = data.id;
    var amount;
    var currency;
    var email = data.email || "";
    var country = data.country || "";
    var donationFrequency = data.frequency;
    this.setState({
      submitting: false
    });
    if (donationFrequency === "monthly") {
      currency = data.currency;
      // Stripe plans are a multiple of the currencies equivilent of Cents
      // e.g. £5/month = 500 £0.01 subscriptions
      amount = data.quantity;
    } else {
      amount = data.amount;
      currency = data.currency;
    }

    var location = "thank-you";
    // If we are already signed up, send to share.
    if (data.signup) {
      location = "share";
      email = "";
      country = "";
    }

    var params = '?payment=Stripe&str_amount=' + amount + '&str_currency=' + currency + '&str_id=' +transactionId + '&str_frequency=' + donationFrequency;

    if (email) {
      params += "&email=" + email;
    }
    if (country) {
      params += "&country=" + country;
    }

    this.transitionTo('/' + this.props.locales[0] + '/' + location + '/?' + params);
  },
  stripeError: function(errorCode, errorType) {
    var cardErrorCodes = {
      "invalid_number": {
        field: "cardNumber",
        message: this.getIntlMessage('invalid_number')
      },
      "invalid_expiry_month": {
        field: "expMonth",
        message: this.getIntlMessage('invalid_expiry_month')
      },
      "invalid_expiry_year": {
        field: "expYear",
        message: this.getIntlMessage('invalid_expiry_year')
      },
      "invalid_cvc": {
        field: "cvc",
        message: this.getIntlMessage('invalid_CVC')
      },
      "incorrect_number": {
        field: "cardNumber",
        message: this.getIntlMessage('incorrect_number')
      },
      "expired_card": {
        field: "cardNumber",
        message: this.getIntlMessage('expired_card')
      },
      "incorrect_cvc": {
        field: "cvc",
        message: this.getIntlMessage('incorrect_CVC')
      },
      "incorrect_zip": {
        field: "code",
        message: this.getIntlMessage('invalid_zip')
      },
      "card_declined": {
        field: "cardNumber",
        message: this.getIntlMessage('declined_card')
      },
      "processing_error": {
        field: "cardNumber",
        message: this.getIntlMessage('transaction_try_another')
      }
    };

    var cardError = cardErrorCodes[errorCode];
    if (errorType === "card_error" && cardError) {
      form.error(cardError.field, cardError.message);
    } else {
      form.error("other", this.getIntlMessage('try_again_later'));
    }
    this.setState({
      submitting: false
    });
  },
  stripe: function(validate, props) {
    var submit = this.submit;
    var success = this.stripeSuccess;
    var error = this.stripeError;
    var valid = form.validate(validate);
    var submitProps = {};
    if (!valid) {
      return;
    }
    var description = this.getIntlMessage("mozilla_donation");
    this.setState({
      submitting: true
    });
    submitProps = form.buildProps(props);
    if (submitProps.frequency === "monthly") {
      description = this.getIntlMessage("mozilla_monthly_donation");
    }
    Stripe.setPublishableKey(process.env.STRIPE_PUBLIC_KEY);
    Stripe.card.createToken({
      number: submitProps.cardNumber,
      cvc: submitProps.cvc,
      exp_month: submitProps.expMonth,
      exp_year: submitProps.expYear,
      address_city: submitProps.city,
      address_country: submitProps.country,<commit_msg>adding error code to error message if it's a generic error.<commit_after>import reactGA from 'react-ga';
import {Navigation} from 'react-router';
import amountModifier from '../scripts/amount-modifier';
import listener from '../scripts/listener.js';
import dispatcher from '../scripts/dispatcher.js';
import form from '../scripts/form.js';

module.exports = {
  mixins: [Navigation],
  getInitialState: function() {
    return {
      paymentType: "",
      submitting: false,
      showCvcHint: false,
      currency: this.props.currency
    };
  },
  componentDidMount: function() {
    form.updateState("currency", this.state.currency);
    form.updateState("presets", this.props.presets);
    form.updateField("amount", this.props.amount);
    form.updateField("frequency", this.props.frequency);
    form.updateField("country", this.props.country);

    listener.on("stateUpdated", this.onStateUpdated);
    listener.on("toggleCvcHint", this.onToggleCvcHint);
    listener.on("toPage", this.toThisPage);
    listener.on("nextPage", this.nextPage);
  },
  componentWillUnmount: function() {
    listener.off("stateUpdated", this.onStateUpdated);
    listener.off("toggleCvcHint", this.onToggleCvcHint);
    listener.off("toPage", this.toThisPage);
    listener.off("nextPage", this.nextPage);
  },
  onStateUpdated: function(e) {
    var detail = e.detail;
    var state = detail.state;
    var value = detail.value;
    if (state === "currency") {
      this.setState({
        currency: value
      });
    }
  },
  onToggleCvcHint: function() {
    this.setState({
      showCvcHint: !this.state.showCvcHint
    });
  },
  nextPage: function(e) {
    dispatcher.fire("toPage", {
      page: this.state.activePage+1
    });
  },
  toThisPage: function(e) {
    var index = e.detail.page;
    if (this.state.activePage === index) {
      return;
    }
    this.setState({
      activePage: index
    });

    // Index starts at 0, and our page tracking starts at 1.
    var page = index+1;
    // Build the page the way it is expected.
    var currentPage = window.location.pathname + "#page-" + page;
    // These are virtual pageviews, so we track them manually in GA
    reactGA.pageview(currentPage);
  },
  submit: function(action, props, success, error) {
    this.setState({
      submitting: true
    });

    props.locale = this.props.locales[0];
    var currency = this.state.currency;
    if (currency) {
      props.currency = currency.code;
    }

    fetch(action, {
      method: 'post',
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(props)
    }).then(function(response) {
      var responseContent;
      var callback = success;
      if (!response.headers.get("content-type")) {
        responseContent = response.text();
      } else {
        responseContent = response.json();
      }
      if (!response.ok) {
        callback = error;
      }
      responseContent.then(function(result) {
        if (callback) {
          callback(result);
        }
      });
    });
  },
  stripeSuccess: function(data) {
    var transactionId = data.id;
    var amount;
    var currency;
    var email = data.email || "";
    var country = data.country || "";
    var donationFrequency = data.frequency;
    this.setState({
      submitting: false
    });
    if (donationFrequency === "monthly") {
      currency = data.currency;
      // Stripe plans are a multiple of the currencies equivilent of Cents
      // e.g. £5/month = 500 £0.01 subscriptions
      amount = data.quantity;
    } else {
      amount = data.amount;
      currency = data.currency;
    }

    var location = "thank-you";
    // If we are already signed up, send to share.
    if (data.signup) {
      location = "share";
      email = "";
      country = "";
    }

    var params = '?payment=Stripe&str_amount=' + amount + '&str_currency=' + currency + '&str_id=' +transactionId + '&str_frequency=' + donationFrequency;

    if (email) {
      params += "&email=" + email;
    }
    if (country) {
      params += "&country=" + country;
    }

    this.transitionTo('/' + this.props.locales[0] + '/' + location + '/?' + params);
  },
  stripeError: function(errorCode, errorType) {
    var cardErrorCodes = {
      "invalid_number": {
        field: "cardNumber",
        message: this.getIntlMessage('invalid_number')
      },
      "invalid_expiry_month": {
        field: "expMonth",
        message: this.getIntlMessage('invalid_expiry_month')
      },
      "invalid_expiry_year": {
        field: "expYear",
        message: this.getIntlMessage('invalid_expiry_year')
      },
      "invalid_cvc": {
        field: "cvc",
        message: this.getIntlMessage('invalid_CVC')
      },
      "incorrect_number": {
        field: "cardNumber",
        message: this.getIntlMessage('incorrect_number')
      },
      "expired_card": {
        field: "cardNumber",
        message: this.getIntlMessage('expired_card')
      },
      "incorrect_cvc": {
        field: "cvc",
        message: this.getIntlMessage('incorrect_CVC')
      },
      "incorrect_zip": {
        field: "code",
        message: this.getIntlMessage('invalid_zip')
      },
      "card_declined": {
        field: "cardNumber",
        message: this.getIntlMessage('declined_card')
      },
      "processing_error": {
        field: "cardNumber",
        message: this.getIntlMessage('transaction_try_another')
      }
    };

    var cardError = cardErrorCodes[errorCode];
    if (errorType === "card_error" && cardError) {
      form.error(cardError.field, cardError.message);
    } else {
      form.error("other", this.getIntlMessage('try_again_later') + " [" + errorType + "]");
    }
    this.setState({
      submitting: false
    });
  },
  stripe: function(validate, props) {
    var submit = this.submit;
    var success = this.stripeSuccess;
    var error = this.stripeError;
    var valid = form.validate(validate);
    var submitProps = {};
    if (!valid) {
      return;
    }
    var description = this.getIntlMessage("mozilla_donation");
    this.setState({
      submitting: true
    });
    submitProps = form.buildProps(props);
    if (submitProps.frequency === "monthly") {
      description = this.getIntlMessage("mozilla_monthly_donation");
    }
    Stripe.setPublishableKey(process.env.STRIPE_PUBLIC_KEY);
    Stripe.card.createToken({
      number: submitProps.cardNumber,
      cvc: submitProps.cvc,
      exp_month: submitProps.expMonth,
      exp_year: submitProps.expYear,
      address_city: submitProps.city,
      address_country: submitProps.country,
{'fraction_non_alphanumeric': 0.1390249416019893, 'fraction_numerical': 0.0019591590686459195, 'mean_word_length': 2.6652858326429163, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5474383', 'n_tokens_mistral': 4083, 'n_tokens_neox': 4057, 'n_words': 816}