Skip to content
Snippets Groups Projects
Commit a14f3977 authored by Jonathan Shaw's avatar Jonathan Shaw Committed by Jonathan Sacksick
Browse files

Issue #3259211: Provide more parameters to createPaymentIntent().

parent 66b6b4de
No related branches found
No related tags found
2 merge requests!16Update READEM.txt,!10Issue #3259211: Provide more parameters to createPaymentIntent()
......@@ -136,8 +136,10 @@ class StripeReview extends CheckoutPaneBase {
else {
$payment_process_pane = $this->checkoutFlow->getPane('payment_process');
assert($payment_process_pane instanceof CheckoutPaneInterface);
$capture = $payment_process_pane->getConfiguration()['capture'];
$intent = $stripe_plugin->createPaymentIntent($this->order, $capture);
$intent_attributes = [
'capture_method' => $payment_process_pane->getConfiguration()['capture'] ? 'automatic' : 'manual',
];
$intent = $stripe_plugin->createPaymentIntent($this->order, $intent_attributes);
}
if ($intent->status === PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD) {
$payment_method = $this->order->get('payment_method')->entity;
......
......@@ -19,6 +19,7 @@ use Drupal\commerce_price\Price;
use Drupal\commerce_stripe\Event\TransactionDataEvent;
use Drupal\commerce_stripe\Event\StripeEvents;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Form\FormStateInterface;
......@@ -440,30 +441,37 @@ class Stripe extends OnsitePaymentGatewayBase implements StripeInterface {
/**
* {@inheritdoc}
*/
public function createPaymentIntent(OrderInterface $order, $capture = TRUE) {
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$payment_method = $order->get('payment_method')->entity;
public function createPaymentIntent(OrderInterface $order, $intent_attributes = [], PaymentInterface $payment = NULL) {
if (is_bool($intent_attributes)) {
$intent_attributes = [
'capture_method' => $intent_attributes ? 'automatic' : 'manual',
];
@trigger_error('Passing a boolean representing capture method as the second parameter to StripeInterface::createPaymentIntent() is deprecated in commerce_stripe:8.x-1.0 and this parameter must be an array of payment intent attributes in commerce_stripe:9.x-2.0. See https://www.drupal.org/project/commerce_stripe/issues/3259211', E_USER_DEPRECATED);
}
$payment_method_remote_id = $payment_method->getRemoteId();
$customer_remote_id = $this->getRemoteCustomerId($order->getCustomer());
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$payment_method = $payment ? $payment->getPaymentMethod() : $order->get('payment_method')->entity;
/** @var \Drupal\commerce_price\Price */
$amount = $payment ? $payment->getAmount() : $order->getTotalPrice();
$amount = $this->minorUnitsConverter->toMinorUnits($order->getTotalPrice());
$order_id = $order->id();
$capture_method = $capture ? 'automatic' : 'manual';
$intent_array = [
'amount' => $amount,
'currency' => strtolower($order->getTotalPrice()->getCurrencyCode()),
$default_intent_attributes = [
'amount' => $this->minorUnitsConverter->toMinorUnits($amount),
'currency' => strtolower($amount->getCurrencyCode()),
'payment_method_types' => ['card'],
'metadata' => [
'order_id' => $order_id,
'order_id' => $order->id(),
'store_id' => $order->getStoreId(),
],
'payment_method' => $payment_method_remote_id,
'capture_method' => $capture_method,
'payment_method' => $payment_method->getRemoteId(),
'capture_method' => 'automatic',
];
$customer_remote_id = $this->getRemoteCustomerId($order->getCustomer());
if (!empty($customer_remote_id)) {
$intent_array['customer'] = $customer_remote_id;
$default_intent_attributes['customer'] = $customer_remote_id;
}
$intent_array = NestedArray::mergeDeep($default_intent_attributes, $intent_attributes);
try {
$intent = PaymentIntent::create($intent_array);
$order->setData('stripe_intent', $intent->id)->save();
......
......@@ -3,6 +3,7 @@
namespace Drupal\commerce_stripe\Plugin\Commerce\PaymentGateway;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Entity\PaymentInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsAuthorizationsInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsRefundsInterface;
......@@ -25,12 +26,16 @@ interface StripeInterface extends OnsitePaymentGatewayInterface, SupportsAuthori
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
* @param bool $capture
* Whether the created payment intent capture is automatic or manual.
* @param bool|array $intent_attributes
* (optional) Either an array of intent attributes or a boolean indicating
* whether the intent capture is automatic or manual. Passing a boolean is
* deprecated in 1.0-rc6. From 2.0 this parameter must be an array.
* @param \Drupal\commerce_payment\Entity\PaymentInterface $payment
* (optional) The payment.
*
* @return \Stripe\PaymentIntent
* The payment intent.
*/
public function createPaymentIntent(OrderInterface $order, $capture = TRUE);
public function createPaymentIntent(OrderInterface $order, $intent_attributes = [], PaymentInterface $payment = NULL);
}
......@@ -6,6 +6,7 @@ use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_payment\Entity\PaymentInterface;
use Drupal\commerce_payment\Entity\PaymentMethod;
use Drupal\commerce_payment\Entity\PaymentMethodInterface;
use Drupal\commerce_price\Price;
......@@ -63,6 +64,50 @@ class PaymentIntentTest extends StripeIntegrationTestBase {
$this->assertEquals($confirmed_status, $intent->status);
}
/**
* Tests creating payment intents with additional parameters.
*/
public function testCreatePaymentIntentAdditionalParameters() {
$gateway = $this->generateGateway();
$plugin = $gateway->getPlugin();
assert($plugin instanceof StripeInterface);
$order_payment_method = $this->prophesize(PaymentMethodInterface::class);
$order_payment_method->getRemoteId()->willReturn('pm_card_threeDSecureOptional');
$order = $this->prophesize(OrderInterface::class);
$order->get('payment_method')->willReturn((object) [
'entity' => $order_payment_method->reveal(),
]);
$order->getTotalPrice()->willReturn(new Price('15.00', 'USD'));
$order->getStoreId()->willReturn(1111);
$order->id()->willReturn(9999);
$order->getCustomer()->willReturn(User::getAnonymousUser());
$order->setData('stripe_intent', Argument::containingString('pi_'))->willReturn($order->reveal());
$order->save()->willReturn(NULL);
$payment_payment_method = $this->prophesize(PaymentMethodInterface::class);
$payment_payment_method->getRemoteId()->willReturn('pm_card_threeDSecure2Required');
$payment = $this->prophesize(PaymentInterface::class);
$payment->getPaymentMethod()->willReturn($payment_payment_method->reveal());
$payment->getAmount()->willReturn(new Price('22.00', 'GBP'));
$intent_attributes = [
'capture_method' => 'manual',
'description' => 'a test intent',
];
$intent = $plugin->createPaymentIntent($order->reveal(), $intent_attributes, $payment->reveal());
$this->assertEquals(PaymentIntent::STATUS_REQUIRES_CONFIRMATION, $intent->status);
// The passed intent attributes should override the defaults.
$this->assertEquals('manual', $intent->capture_method);
$this->assertEquals('a test intent', $intent->description);
// Price and payment method come from payment not order.
$this->assertEquals($intent->currency, 'gbp');
$this->assertEquals($intent->amount, 2200);
}
/**
* Tests that the order total syncs the payment intent total.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment