<commit_before><?php

use \Stripe\Stripe as Stripe;
use \Stripe\Customer as StripeCustomer;

/**
 * A form that allows the user to enter their card details and
 * save them "into" stripe.
 *  
 * The end user enters their credit card details and when the
 * form is submitted, they are pushed to Stripe via the API.
 * The form is then pre-populated with summary details of the
 * saved payment details.
 *
 * By default this form uses the default Stripe JS, if you wish
 * to overwrite this functionality (to perform custom operations)
 * then make sure you disable the use_custom_js config variable.
 *
 * @package stripe-forms
 * @subpackage forms
 * @author Mo <<EMAIL>>
 */
class StripePaymentDetailsForm extends Form
{

    /**
     * List of available months for the dropdown
     *
     * @var array
     * @config
     */
    private static $months_of_year = array(
        "1" => "1",
        "2" => "2",
        "3" => "3",
        "4" => "4",
        "5" => "5",
        "6" => "6",
        "7" => "7",
        "8" => "8",
        "9" => "9",
        "10" => "10",
        "11" => "11",
        "12" => "12",
    );

    /**
     * How many years in the future should the expiry
     * dropdown run (defaults to 5).
     *
     * @var Int
     * @config
     */
    private static $future_years = 5;
    
    /**
     * Config variable to specify if we want to use custom JS.
     * Enabling this will disable all requirements calls
     *
     * @var Boolean
     * @config
     */
    private static $use_custom_js = false;
    
    public function __construct($controller, $name = "StripePaymentDetailsForm")
    {
        $publish_key = StripeForms::publish_key();

        parent::__construct(
            $controller,
            $name,

            // Fields
            FieldList::create(
                HiddenField::create("StripeToken"),
                ReadonlyField::create("CardType"),
                TextField::create("CardNumber")
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "number"),
                DropdownField::create("ExpirationMonth", null, $this->config()->months_of_year)
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "exp_month"),
                DropdownField::create("ExpirationYear", null, $this->generate_years())
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "exp_year"),
                TextField::create("CVC")
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "cvc"),
                TextField::create("BillingZip", "Billing Zip/Post Code")
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "address_zip")
            ),

            // Actions
            FieldList::create(
                FormAction::create("doSavePaymentDetails", "Save")
                    ->addExtraClass("submit")
            )
        );

        if (!StripeForms::config()->use_custom_js) {
            Requirements::javascript("https://js.stripe.com/v2/");
            Requirements::javascriptTemplate(
                "stripe-forms/javascript/StripePaymentDetailsForm.js",
                array(
                    "PublishKey" => $publish_key,
                    "FormName" => $this->FormName()
                )
            );
        }

        $card_details = $this->get_card_details();

        if ($card_details) {
            $this->loadDataFrom($card_details);
        }
    }

    /** 
     * Generate an array of years from the current year
     *
     * @return array
     */
    protected function generate_years()
    {
        $i = new \DateTime();
        $end = new \DateTime("+ {$this->config()->future_years} years");
        $return = array(
            $i->format('Y') => $i->format('Y')
        );

        for($i; $i < $end; $i->add(new \DateInterval('P1Y'))){
            $return[$i->format('Y')] = $i->format('Y');
        }

        return $return;
    }

    /**<commit_msg>Hide card type field if it is not set<commit_after><?php

use \Stripe\Stripe as Stripe;
use \Stripe\Customer as StripeCustomer;

/**
 * A form that allows the user to enter their card details and
 * save them "into" stripe.
 *  
 * The end user enters their credit card details and when the
 * form is submitted, they are pushed to Stripe via the API.
 * The form is then pre-populated with summary details of the
 * saved payment details.
 *
 * By default this form uses the default Stripe JS, if you wish
 * to overwrite this functionality (to perform custom operations)
 * then make sure you disable the use_custom_js config variable.
 *
 * @package stripe-forms
 * @subpackage forms
 * @author Mo <<EMAIL>>
 */
class StripePaymentDetailsForm extends Form
{

    /**
     * List of available months for the dropdown
     *
     * @var array
     * @config
     */
    private static $months_of_year = array(
        "1" => "1",
        "2" => "2",
        "3" => "3",
        "4" => "4",
        "5" => "5",
        "6" => "6",
        "7" => "7",
        "8" => "8",
        "9" => "9",
        "10" => "10",
        "11" => "11",
        "12" => "12",
    );

    /**
     * How many years in the future should the expiry
     * dropdown run (defaults to 5).
     *
     * @var Int
     * @config
     */
    private static $future_years = 5;
    
    /**
     * Config variable to specify if we want to use custom JS.
     * Enabling this will disable all requirements calls
     *
     * @var Boolean
     * @config
     */
    private static $use_custom_js = false;
    
    public function __construct($controller, $name = "StripePaymentDetailsForm")
    {
        $publish_key = StripeForms::publish_key();

        parent::__construct(
            $controller,
            $name,

            // Fields
            FieldList::create(
                HiddenField::create("StripeToken"),
                HiddenField::create("CardType"),
                TextField::create("CardNumber")
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "number"),
                DropdownField::create("ExpirationMonth", null, $this->config()->months_of_year)
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "exp_month"),
                DropdownField::create("ExpirationYear", null, $this->generate_years())
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "exp_year"),
                TextField::create("CVC")
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "cvc"),
                TextField::create("BillingZip", "Billing Zip/Post Code")
                    ->setAttribute("name", "")
                    ->setAttribute("data-stripe", "address_zip")
            ),

            // Actions
            FieldList::create(
                FormAction::create("doSavePaymentDetails", "Save")
                    ->addExtraClass("submit")
            )
        );

        if (!StripeForms::config()->use_custom_js) {
            Requirements::javascript("https://js.stripe.com/v2/");
            Requirements::javascriptTemplate(
                "stripe-forms/javascript/StripePaymentDetailsForm.js",
                array(
                    "PublishKey" => $publish_key,
                    "FormName" => $this->FormName()
                )
            );
        }

        $card_details = $this->get_card_details();

        if ($card_details) {
            $this
                ->Fields()
                ->replaceField(
                    "CardType",
                    ReadonlyField::create("CardType")
                );

            $this->loadDataFrom($card_details);
        }
    }

    /** 
     * Generate an array of years from the current year
     *
     * @return array
     */
    protected function generate_years()
    {
        $i = new \DateTime();
        $end = new \DateTime("+ {$this->config()->future_years} years");
        $return = array(
            $i->format('Y') => $i->format('Y')
        );

        for($i; $i < $end; $i->add(new \DateInterval('P1Y'))){
            $return[$i->format('Y')] = $i->format('Y');
        }

        return $return;
    }

    /**
{'fraction_non_alphanumeric': 0.14053342901566798, 'fraction_numerical': 0.008132998445162062, 'mean_word_length': 1.7326797385620916, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 89, 'https://': 2, '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': '5549645', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2268, 'n_words': 573}