From 27c98128abadd28ba2ae917bb2ec86a95bcbcf3c Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 8 Jul 2019 18:23:39 +0200 Subject: [PATCH] Require Payment Method for charge method 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. --- src/Billable.php | 10 +++------- tests/Integration/ChargesTest.php | 19 +++---------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/Billable.php b/src/Billable.php index e754a2f3..39f2a62e 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -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; @@ -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', @@ -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()) ); diff --git a/tests/Integration/ChargesTest.php b/tests/Integration/ChargesTest.php index d8e290e1..6a84757c 100644 --- a/tests/Integration/ChargesTest.php +++ b/tests/Integration/ChargesTest.php @@ -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 @@ -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()); @@ -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) {