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

[10.0] Require Payment Method for charge method #697

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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