Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a Card #363

Closed
Grafikart opened this issue Jul 17, 2017 · 10 comments
Closed

Create a Card #363

Grafikart opened this issue Jul 17, 2017 · 10 comments
Labels

Comments

@Grafikart
Copy link

Grafikart commented Jul 17, 2017

Hello

Is there a way to create a card from this library ?
I want to translate this call but the Card Class doesn't contain a create method.

I could do a PR to add these features but the use of static method is a problem, this class would have to overwrite the _create, _list, _update methods from ApiResource (since the URL expects a parameter).

class Card extends ApiResource
{

    public static function create(string $customer, ?array $params = null, ?array $options = null): StripeObject
    {
        self::_validateParams($params);
        $url = "/v1/customers/" . $customer . "/sources";
        [$response, $opts] = static::_staticRequest('post', $url, $params, $options);
        $obj = Util::convertToStripeObject($response->json, $opts);
        $obj->setLastResponse($response);
        return $obj;
    }

}
@remi-stripe
Copy link
Contributor

@Grafikart You don't need to add anything as it's already supported in the library and covered in the documentation. To create a card right now you have to go through the Customer who owns it. The code looks like this:

$customer = \Stripe\Customer::retrieve("cus_XXXXX");
$customer->sources->create(array("source" => "tok_visa"));

@Grafikart
Copy link
Author

Feel free to ignore this comment if you don't agree (this is an opinion, i'm not trying to enforce my point of view :))

Your answer does the job but it forces you to do 2 API calls to create a card. Moreover, I think the method "create" you are showing is not as obvious as others (I could guess the other methods reading from the Stripe API).

@remi-stripe
Copy link
Contributor

@Grafikart Ah okay I see what you're asking now. You want to create a card in one API call as is done in some other libraries like the Node.js one?

If so it would definitely be useful to add but not specifically for cards as the path is the same for any Source really including ACH, SEPA, etc. The issue is that passing a $customerId as the first parameter feels weird and not something we do in other resources in the PHP library.

A different fix would be to special-case the $params['customer'] to change the URL we submit too in the create call but I'm not sure it's the kind of "hacks" we'd want in the library.

cc @brandur-stripe @ob-stripe for thoughts?

@remi-stripe remi-stripe reopened this Jul 17, 2017
@evan-stripe
Copy link

Hmm. I think most of our other libraries have a constructor that lets you create an object from an ID without having to fetch it, e.g.

customer = Stripe::Customer.new('cus_123abc')
customer.sources.create(blah: blah)

@remi-stripe
Copy link
Contributor

@evan-stripe Doesn't that feel wrong though? Sounds more like a hack than a reliable solution to rely on the fact that passing an id will set it in the right place. In that case I definitely prefer either adding $customerId to the Source creation or adding a createSource() to the Customer class.

@Grafikart
Copy link
Author

Grafikart commented Jul 17, 2017

The problem could be also expanded to retrieve sources from a customer

$customer = new Customer('cus_123abs');
$customer->createSource(); // Create a new source 
$customer->getSources(); // Retrieve the sources for this customer

Or to keep consistancy with the rest of the library, keeping everything static :

Source::all(['customer' => 'cus_abc']);
Source::create(['customer' => 'cus_abc']);

@ob-stripe
Copy link
Contributor

ob-stripe commented Jul 18, 2017

@Grafikart The problem is that sources are first-order API resources, while cards & bank accounts are not (i.e. you can retrieve the former with just their ID, while you need to retrieve the latter through the customer object to which they are attached). This is how the API itself is designed and is not something that can be fixed in the client libraries.

If you are really dead-set on skipping the customer retrieval request, I'd suggest something similar to what @evan-stripe said. This should let you retrieve an existing card:

$card = \Stripe\Card::constructFrom(array(
  'id' => 'card_...',
  'customer' => 'cus_...'
), array());
$card->refresh();

You can also create a new card like this:

$customerId = "cus_...";
$url = "/v1/customers/$customerId/sources";
$sources = \Stripe\Collection::constructFrom(array("url" => $url), array());
$card = $sources->create(array("source" => "tok_visa"));

It's definitely a bit hackish and relies on internal methods that might change without warnings though.

I suppose we could add some static methods on customer objects so you could do something like this:

$card = \Stripe\Customer::retrieveCard("cus_...", "card_...");

or:

$card = \Stripe\Customer::createCard("cus_...", array("source" => "tok_visa"));

We'd need to add new static methods for every CRUD request for second-order sources. It's definitely doable but given that it's a fairly rare use-case I'm not sure it's worth the trouble.

@MaxInMoon
Copy link

+1 for a "built-in" create card method

@krishnamt
Copy link

@Grafikart You don't need to add anything as it's already supported in the library and covered in the documentation. To create a card right now you have to go through the Customer who owns it. The code looks like this:

$customer = \Stripe\Customer::retrieve("cus_XXXXX");
$customer->sources->create(array("source" => "tok_visa"));

=========================
I have tried the same approach, but im getting
Fatal error: Uncaught Stripe\Error\InvalidRequest: No such token: tok_visa in ApiRequestor.php

@remi-stripe
Copy link
Contributor

I'd recommend contacting our support team: https://support.stripe.com/contact

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants