payment with stripe in php

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script src="vendor/jquery/jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
function cardValidation () {
    var valid = true;
    var name = $('#name').val();
    var email = $('#email').val();
    var cardNumber = $('#card-number').val();
    var month = $('#month').val();
    var year = $('#year').val();
    var cvc = $('#cvc').val();

    $("#error-message").html("").hide();

    if (name.trim() == "") {
        valid = false;
    }
    if (email.trim() == "") {
    	   valid = false;
    }
    if (cardNumber.trim() == "") {
    	   valid = false;
    }

    if (month.trim() == "") {
    	    valid = false;
    }
    if (year.trim() == "") {
        valid = false;
    }
    if (cvc.trim() == "") {
        valid = false;
    }

    if(valid == false) {
        $("#error-message").html("All Fields are required").show();
    }

    return valid;
}
//set your publishable key
Stripe.setPublishableKey("<?php echo STRIPE_PUBLISHABLE_KEY; ?>");

//callback to handle the response from stripe
function stripeResponseHandler(status, response) {
    if (response.error) {
        //enable the submit button
        $("#submit-btn").show();
        $( "#loader" ).css("display", "none");
        //display the errors on the form
        $("#error-message").html(response.error.message).show();
    } else {
        //get token id
        var token = response['id'];
        //insert the token into the form
        $("#frmStripePayment").append("<input type='hidden' name='token' value='" + token + "' />");
        //submit form to the server
        $("#frmStripePayment").submit();
    }
}
function stripePay(e) {
    e.preventDefault();
    var valid = cardValidation();

    if(valid == true) {
        $("#submit-btn").hide();
        $( "#loader" ).css("display", "inline-block");
        Stripe.createToken({
            number: $('#card-number').val(),
            cvc: $('#cvc').val(),
            exp_month: $('#month').val(),
            exp_year: $('#year').val()
        }, stripeResponseHandler);

        //submit from callback
        return false;
    }
}
</script>

4
3
Mr Victor 110 points

                                    &lt;?php
use \PhpPot\Service\StripePayment;

if (!empty($_POST[&quot;token&quot;])) {
    require_once 'StripePayment.php';
    $stripePayment = new StripePayment();
    
    $stripeResponse = $stripePayment-&gt;chargeAmountFromCard($_POST);
    
    require_once &quot;DBController.php&quot;;
    $dbController = new DBController();
    
    $amount = $stripeResponse[&quot;amount&quot;] /100;
    
    $param_type = 'ssdssss';
    $param_value_array = array(
        $_POST['email'],
        $_POST['item_number'],
        $amount,
        $stripeResponse[&quot;currency&quot;],
        $stripeResponse[&quot;balance_transaction&quot;],
        $stripeResponse[&quot;status&quot;],
        json_encode($stripeResponse)
    );
    $query = &quot;INSERT INTO tbl_payment (email, item_number, amount, currency_code, txn_id, payment_status, payment_response) values (?, ?, ?, ?, ?, ?, ?)&quot;;
    $id = $dbController-&gt;insert($query, $param_type, $param_value_array);
    
    if ($stripeResponse['amount_refunded'] == 0 &amp;&amp; empty($stripeResponse['failure_code']) &amp;&amp; $stripeResponse['paid'] == 1 &amp;&amp; $stripeResponse['captured'] == 1 &amp;&amp; $stripeResponse['status'] == 'succeeded') {
       $successMessage = &quot;Stripe payment is completed successfully. The TXN ID is &quot; . $stripeResponse[&quot;balance_transaction&quot;];
    }
}
?&gt;

4 (3 Votes)
0
3.88
8
Geno 110 points

                                    &lt;?php
namespace PhpPot\Service;

require_once 'vendor/stripe/autoload.php';

use \Stripe\Stripe;
use \Stripe\Customer;
use \Stripe\ApiOperations\Create;
use \Stripe\Charge;

class StripePayment
{

    private $apiKey;

    private $stripeService;

    public function __construct()
    {
        require_once &quot;config.php&quot;;
        $this-&gt;apiKey = STRIPE_SECRET_KEY;
        $this-&gt;stripeService = new \Stripe\Stripe();
        $this-&gt;stripeService-&gt;setVerifySslCerts(false);
        $this-&gt;stripeService-&gt;setApiKey($this-&gt;apiKey);
    }

    public function addCustomer($customerDetailsAry)
    {
        
        $customer = new Customer();
        
        $customerDetails = $customer-&gt;create($customerDetailsAry);
        
        return $customerDetails;
    }

    public function chargeAmountFromCard($cardDetails)
    {
        $customerDetailsAry = array(
            'email' =&gt; $cardDetails['email'],
            'source' =&gt; $cardDetails['token']
        );
        $customerResult = $this-&gt;addCustomer($customerDetailsAry);
        $charge = new Charge();
        $cardDetailsAry = array(
            'customer' =&gt; $customerResult-&gt;id,
            'amount' =&gt; $cardDetails['amount']*100 ,
            'currency' =&gt; $cardDetails['currency_code'],
            'description' =&gt; $cardDetails['item_name'],
            'metadata' =&gt; array(
                'order_id' =&gt; $cardDetails['item_number']
            )
        );
        $result = $charge-&gt;create($cardDetailsAry);

        return $result-&gt;jsonSerialize();
    }
}

3.88 (8 Votes)
0
0
3
Francois T. 105 points

                                    &lt;?php if(!empty($successMessage)) { ?&gt;
&lt;div id=&quot;success-message&quot;&gt;&lt;?php echo $successMessage; ?&gt;&lt;/div&gt;
&lt;?php  } ?&gt;
&lt;div id=&quot;error-message&quot;&gt;&lt;/div&gt;

&lt;form id=&quot;frmStripePayment&quot; action=&quot;&quot; method=&quot;post&quot;&gt;
    &lt;div class=&quot;field-row&quot;&gt;
        &lt;label&gt;Card Holder Name&lt;/label&gt; &lt;span id=&quot;card-holder-name-info&quot;
            class=&quot;info&quot;&gt;&lt;/span&gt;&lt;br&gt; &lt;input type=&quot;text&quot; id=&quot;name&quot;
            name=&quot;name&quot; class=&quot;demoInputBox&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;field-row&quot;&gt;
        &lt;label&gt;Email&lt;/label&gt; &lt;span id=&quot;email-info&quot; class=&quot;info&quot;&gt;&lt;/span&gt;&lt;br&gt;
        &lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; class=&quot;demoInputBox&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;field-row&quot;&gt;
        &lt;label&gt;Card Number&lt;/label&gt; &lt;span id=&quot;card-number-info&quot;
            class=&quot;info&quot;&gt;&lt;/span&gt;&lt;br&gt; &lt;input type=&quot;text&quot; id=&quot;card-number&quot;
            name=&quot;card-number&quot; class=&quot;demoInputBox&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;field-row&quot;&gt;
        &lt;div class=&quot;contact-row column-right&quot;&gt;
            &lt;label&gt;Expiry Month / Year&lt;/label&gt; &lt;span id=&quot;userEmail-info&quot;
                class=&quot;info&quot;&gt;&lt;/span&gt;&lt;br&gt; &lt;select name=&quot;month&quot; id=&quot;month&quot;
                class=&quot;demoSelectBox&quot;&gt;
                &lt;option value=&quot;08&quot;&gt;08&lt;/option&gt;
                &lt;option value=&quot;09&quot;&gt;9&lt;/option&gt;
                &lt;option value=&quot;10&quot;&gt;10&lt;/option&gt;
                &lt;option value=&quot;11&quot;&gt;11&lt;/option&gt;
                &lt;option value=&quot;12&quot;&gt;12&lt;/option&gt;
            &lt;/select&gt; &lt;select name=&quot;year&quot; id=&quot;year&quot;
                class=&quot;demoSelectBox&quot;&gt;
                &lt;option value=&quot;18&quot;&gt;2018&lt;/option&gt;
                &lt;option value=&quot;19&quot;&gt;2019&lt;/option&gt;
                &lt;option value=&quot;20&quot;&gt;2020&lt;/option&gt;
                &lt;option value=&quot;21&quot;&gt;2021&lt;/option&gt;
                &lt;option value=&quot;22&quot;&gt;2022&lt;/option&gt;
                &lt;option value=&quot;23&quot;&gt;2023&lt;/option&gt;
                &lt;option value=&quot;24&quot;&gt;2024&lt;/option&gt;
                &lt;option value=&quot;25&quot;&gt;2025&lt;/option&gt;
                &lt;option value=&quot;26&quot;&gt;2026&lt;/option&gt;
                &lt;option value=&quot;27&quot;&gt;2027&lt;/option&gt;
                &lt;option value=&quot;28&quot;&gt;2028&lt;/option&gt;
                &lt;option value=&quot;29&quot;&gt;2029&lt;/option&gt;
                &lt;option value=&quot;30&quot;&gt;2030&lt;/option&gt;
            &lt;/select&gt;
        &lt;/div&gt;
        &lt;div class=&quot;contact-row cvv-box&quot;&gt;
            &lt;label&gt;CVC&lt;/label&gt; &lt;span id=&quot;cvv-info&quot; class=&quot;info&quot;&gt;&lt;/span&gt;&lt;br&gt;
            &lt;input type=&quot;text&quot; name=&quot;cvc&quot; id=&quot;cvc&quot;
                class=&quot;demoInputBox cvv-input&quot;&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;input type=&quot;submit&quot; name=&quot;pay_now&quot; value=&quot;Submit&quot;
            id=&quot;submit-btn&quot; class=&quot;btnAction&quot;
            onClick=&quot;stripePay(event);&quot;&gt;

        &lt;div id=&quot;loader&quot;&gt;
            &lt;img alt=&quot;loader&quot; src=&quot;LoaderIcon.gif&quot;&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;input type='hidden' name='amount' value='0.5'&gt; &lt;input type='hidden'
        name='currency_code' value='USD'&gt; &lt;input type='hidden'
        name='item_name' value='Test Product'&gt; &lt;input type='hidden'
        name='item_number' value='PHPPOTEG#1'&gt;
&lt;/form&gt;

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
stripe payment response php stripe payment api PHP stripe integration for php stripe connect integration php methods to integrate stripe payment gateway in php stripe payment using php docs how to make payment by stripe using js and php Stripe payout php stripe create payout method php stripe create payment method php integrate stripe payment in php php script to integrate stripe stripe subscription payment integration in php stripe php library subscription php stripe connect stripe php master get customer stripe php Stripe Checkout Integration in PHP php stripe ach stripe only php how to generate a payment in stripe using php stripe intergration php stripe stripe php create source stripe connect php documentation stripe php document stripe with pure php example stripe php payment integration stripe api for php stripe php create charge stripe payment gateway php example php payment stripe sdk integration stripe connect in php demo how to integreate stripe into php set up stripe connect php StripePlaid::make php stripe documentation php Stripe\StripeClient( php stripe connect api php payment with stripe php example read stripe response in php simple stripe php class stripe-php method list stripe integration php documentation stripe create source php how to stripe payment with parameters after success in php Stripe payment Intents example php stripe charges php stripe php create customer stripe php api stripe payment intent php php-stripe add usage stripe subscription using php stripe api php example stripe stripe php stripe payment in $ stripe subscription php example charge stripe php how to add payment method in stripe using php how to create customer in stripe using php stripe/stripe-php response from stripe server stripe/stripe-php response adding stripe php charge and pay latter stripe php stripe sample php stripe php reference payment with specific card stripe php stripe custom payment form php how to purchase item using stripe php stripe payment in core php stripe integration custom php how to stripe payment hold until when i done in php stripe get php stripe payment php get customer integrated stripe payment using php stripe integration with php stripe php make payment take stripe payment php how to set custom amount in stripe payment integration php create stripe address in php payment using stripe api php create customer in stripe php stripe connect php Stripe payout api php stripe integration in php stripe/stripe-php doc payment status in stripe php package stripe payment gateway examples in php stripe make card payment php stripe database php stripe databased php php stripe integration example create paymentMethods for customer stripe php stripe subscription example php demo stripe payment gateway integration in php demo how to rpitn stripe repsonse php display the response of stripe api in php stripe create card -stripe.com example php stripe example php makey payment with stripe php Stripe cliente example php php stripe exasmple how to integrate stripe payment gateway in php stripe php demo stripe checkout payment php stripe integration php example stripe checkout payment response php how to integrate stripe payment using php paymentintent stripe php stripe payment integration in php php stripe subscription stripe integrate in php stripe api php create payment -&quot;stripe.com&quot; stripe api php create payment stripe into php stripe php payment post stripe.js and php stripe single payment php php stripe charge php stripe customer php stripe card add code php using stripe api implement php page php stripe example all in one stripe payment integration php code stripe php payment example use stripe in php stripe implementation in php use stripe gateway in php stripe paymentintent example php require stripe php tutorial stripe php latest stripe php stripe integration php stripe tutorial php stripe payment methpd stripe api payment php stripe payment method in php stripe in php stripe how to connect stripe account create payment form stripe subscription create using payment method in php stripe stripe checkout php The Stripe PHP library stripe api docs stripe jobs stripe with php stripe php library How to integrate the Stripe Payment gateway in using PHP stripe php example code stripe api php stripe php tutorial stripe payment sdk php php setup future payments stripe example \Stripe\PaymentIntent in php stripe payment gateway php integration with stripe-php example payment stripe php php stripe send payment php stripe payout create customer in stripe using php stripe payment php 7.2 stripe monthly payments php stripe payment with card php stripe-php connect how to integrate stripe payment gateway in to website php php add stripe to website stripe api integration in php integrate stripe with php examples stripe create customer php stripe paymentinstant using php payment api stripe in php stripe paymentintent php stripe php subscription example php stripe laravel stripe/stripe-php info customer stripe php integration stripe using php stripe card create php stripe php run php -s stripe stripe payment checkout php simple stripe php stripe api example php stripe install php stripe payment gateway integration in php stripe bancontact payment php code how to integrate stripe in php stripe store tutorial php Stripe add payment method to customer php stripe library php stripe subscription php stripe php api library stripe payment gateway in php stripe composer doc styripe php php stripe api create the card pay payment button in php stripe payment gateway form in html stripe payment gateway stripe charges in php stripe form php stripe generate payment link from pure php stripe create charge link via php stripe payment methods stripe payment stripe full php etup php integrate stripe into website stripe php api stripe payment gateway php stripe api phph stripe payment gateway integration server side php stripe php integration stripe integration plain php v3 stripe integration plain php stripe php charge example stripe auto composer php interrogate stripe stripe get post data php stripe payment design in php stripe payment gateway in php demo debit card php stripe payment php how to add stripe checkout form in mysql database setApikey in stripe.js v3 example php Stripe PHP example stripe custom checkout example php implement stripe payment method in php implement stripe in php stripe client php stripe php package Checkout with Stripe API laravel integrate stripe payment gateway in php stripe php library usage for process credit card stripe payment in php stripe create charge example in php php stripe integration how to sve stripe key in php app stripe payment example php strip payment example php php stripe payment example stripe web integration php stripe php integration example stripe php cretae payment stripe php library stripe stripe.php stripe payment php example payment with stripe in php
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source