Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple cards in same customer in stripe payment gateway using php

i was implementing stripe payment in testing mode.Here i got an error like Same token is used again. is there any different way to add multiple cards. or i need to call retrive function in a separate page so that conflict of same token never come again.And how should i set a card default.

public function createToken($data)
{

        $TokenResult=Token::create(array(
            "card" => array(
            "name" => $data['name'],
            "number" => $data['card_number'],
            "exp_month" => $data['month'],
            "exp_year" => $data['year'],
            "cvc" => $data['cvc']
            )));
        //echo "<pre>";;
        //print_r($TokenResult);
        $this->token=$TokenResult['id'];//store token id into token variable
        $this->chargeCard($this->token);    //call chargecard function via passing token id
}
/*
* function to create customer
*/
public function createCustomer($data,$token=null)//pass form data and token id
{
    $customer=Customer::create(array(
    "email"=>$data['email'],
    "description" => $data['name'],
    "source" => $token // obtained with Stripe.js
    ));
    $customerId=$customer['id'];
    $this->retriveCustomer($customerId,$token);
}
/*
* function to retrive current customers for adding multiple cards to same customers*/
public function retriveCustomer($customerid,$token)
{
    echo $this->token;
    //die('here');
    $retriveResult=Customer::retrieve($customerid);
    $retriveResult->sources->create(array("source" =>$this->token));
    return $retriveResult;
}
like image 531
ashish bansal Avatar asked Sep 19 '25 19:09

ashish bansal


1 Answers

First, please note that unless you are PCI certified and allowed to directly manipulate card data, you should never have access to card numbers in your server-side code.

Card tokens should be created client-side, via Checkout or Elements. Your server should only deal with client-side created card tokens and never with PCI-sensitive information (card numbers and CVCs). This will greatly decrease the burden of PCI compliance and make you eligible for PCI SAQ A.

In PHP, this is how you'd add a card to an existing customer object:

$customer = \Stripe\Customer::retrieve("cus_...");
$card = $customer->sources->create(array(
    "source" => $token  // token created by Checkout or Elements
));
like image 93
Ywain Avatar answered Sep 21 '25 15:09

Ywain