Skip to content

Commit

Permalink
Merge pull request #697 from laravel/require-payment-method-on-charge
Browse files Browse the repository at this point in the history
[10.0] Require Payment Method for charge method
  • Loading branch information
taylorotwell authored Jul 9, 2019
2 parents 3c635ae + 27c9812 commit c3ac1cb
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 c3ac1cb

Please sign in to comment.