Skip to content

Commit

Permalink
Require Payment Method for charge method
Browse files Browse the repository at this point in the history
With Stripe's new Payment Method API, there's no way to set a default for single charges, only for invoices & billing purposes. Because of this requirement, it's best that the payment method is a required argument for the charge method.
  • Loading branch information
driesvints committed Jul 8, 2019
1 parent 3c635ae commit 27c9812
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
10 changes: 3 additions & 7 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laravel\Cashier;

use Exception;
use InvalidArgumentException;
use Stripe\Card as StripeCard;
use Stripe\Token as StripeToken;
use Illuminate\Support\Collection;
Expand All @@ -24,11 +23,11 @@ trait Billable
* Make a "one off" charge on the customer for the given amount.
*
* @param int $amount
* @param string $paymentMethod
* @param array $options
* @return \Laravel\Cashier\Payment
* @throws \InvalidArgumentException
*/
public function charge($amount, array $options = [])
public function charge($amount, $paymentMethod, array $options = [])
{
$options = array_merge([
'confirmation_method' => 'automatic',
Expand All @@ -37,15 +36,12 @@ public function charge($amount, array $options = [])
], $options);

$options['amount'] = $amount;
$options['payment_method'] = $paymentMethod;

if ($this->stripe_id) {
$options['customer'] = $this->stripe_id;
}

if (! array_key_exists('payment_method', $options) && ! array_key_exists('customer', $options)) {
throw new InvalidArgumentException('No payment method provided.');
}

$payment = new Payment(
StripePaymentIntent::create($options, Cashier::stripeOptions())
);
Expand Down
19 changes: 3 additions & 16 deletions tests/Integration/ChargesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laravel\Cashier\Tests\Integration;

use Laravel\Cashier\Payment;
use Stripe\Error\InvalidRequest;
use Laravel\Cashier\Exceptions\PaymentActionRequired;

class ChargesTest extends IntegrationTestCase
Expand All @@ -12,30 +11,19 @@ public function test_customer_can_be_charged()
{
$user = $this->createCustomer('customer_can_be_charged');
$user->createAsStripeCustomer();
$user->updateCard('tok_visa');

$response = $user->charge(1000);
$response = $user->charge(1000, 'pm_card_visa');

$this->assertInstanceOf(Payment::class, $response);
$this->assertEquals(1000, $response->rawAmount());
$this->assertEquals($user->stripe_id, $response->customer);
}

public function test_customer_cannot_be_charged_without_a_payment_method()
{
$user = $this->createCustomer('customer_cannot_be_charged_without_a_payment_method');
$user->createAsStripeCustomer();

$this->expectException(InvalidRequest::class);

$user->charge(1000);
}

public function test_non_stripe_customer_can_be_charged()
{
$user = $this->createCustomer('non_stripe_customer_can_be_charged');

$response = $user->charge(1000, ['payment_method' => 'pm_card_visa']);
$response = $user->charge(1000, 'pm_card_visa');

$this->assertInstanceOf(Payment::class, $response);
$this->assertEquals(1000, $response->rawAmount());
Expand Down Expand Up @@ -71,10 +59,9 @@ public function test_charging_may_require_an_extra_action()
{
$user = $this->createCustomer('charging_may_require_an_extra_action');
$user->createAsStripeCustomer();
$user->updateCard('tok_threeDSecure2Required');

try {
$user->charge(1000);
$user->charge(1000, 'pm_card_threeDSecure2Required');

$this->fail('Expected exception '.PaymentActionRequired::class.' was not thrown.');
} catch (PaymentActionRequired $e) {
Expand Down

0 comments on commit 27c9812

Please sign in to comment.