From 17abea04f06d864489d2573e96bf3fb8d89c620d Mon Sep 17 00:00:00 2001 From: Andreas Radloff Date: Tue, 4 Jun 2024 14:29:01 +0200 Subject: [PATCH 1/3] Use ChaeckoutGateway to authorize --- src/CheckoutGateway.php | 2 +- src/Message/Checkout/AuthorizeRequest.php | 32 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/Message/Checkout/AuthorizeRequest.php diff --git a/src/CheckoutGateway.php b/src/CheckoutGateway.php index 9dfd769d..0d0786ef 100644 --- a/src/CheckoutGateway.php +++ b/src/CheckoutGateway.php @@ -51,7 +51,7 @@ public function fetchTransaction(array $parameters = array()) */ public function authorize(array $parameters = array()) { - return $this->createRequest('\Omnipay\Stripe\Message\AuthorizeRequest', $parameters); + return $this->createRequest('\Omnipay\Stripe\Message\Checkout\AuthorizeRequest', $parameters); } /** diff --git a/src/Message/Checkout/AuthorizeRequest.php b/src/Message/Checkout/AuthorizeRequest.php new file mode 100644 index 00000000..b77e1bb8 --- /dev/null +++ b/src/Message/Checkout/AuthorizeRequest.php @@ -0,0 +1,32 @@ + 'manual' + ]; + + return $data; + } +} From fe97ac32856142983fc1a5c9f7e82c531ea8581d Mon Sep 17 00:00:00 2001 From: Andreas Radloff Date: Wed, 5 Jun 2024 13:43:15 +0200 Subject: [PATCH 2/3] Support Stripe Connect in Checkout Gateway --- src/Message/Checkout/AuthorizeRequest.php | 8 +- src/Message/Checkout/PurchaseRequest.php | 200 ++++++++++++++++++++++ 2 files changed, 205 insertions(+), 3 deletions(-) diff --git a/src/Message/Checkout/AuthorizeRequest.php b/src/Message/Checkout/AuthorizeRequest.php index b77e1bb8..c585bea4 100644 --- a/src/Message/Checkout/AuthorizeRequest.php +++ b/src/Message/Checkout/AuthorizeRequest.php @@ -18,14 +18,16 @@ public function getData() { $data = parent::getData(); + $paymentIntentData = $data['payment_intent_data'] ?? []; + /** * @see https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-payment_intent_data-capture_method * * You will use PaymentIntents\CaptureRequest to capture and process a previously created authorization. */ - $data['payment_intent_data'] = [ - 'capture_method' => 'manual' - ]; + $paymentIntentData['capture_method'] = 'manual'; + + $data['payment_intent_data'] = $paymentIntentData; return $data; } diff --git a/src/Message/Checkout/PurchaseRequest.php b/src/Message/Checkout/PurchaseRequest.php index d3d2386d..13ec28f5 100644 --- a/src/Message/Checkout/PurchaseRequest.php +++ b/src/Message/Checkout/PurchaseRequest.php @@ -6,6 +6,8 @@ namespace Omnipay\Stripe\Message\Checkout; +use Money\Formatter\DecimalMoneyFormatter; + /** * Stripe Checkout Session Request * @@ -145,16 +147,214 @@ public function getClientReferenceId() return $this->getParameter('client_reference_id'); } + /** + * Set the customer_creation parameter + * + * @param string $value + * + * @return \Omnipay\Common\Message\AbstractRequest|PurchaseRequest + */ + public function setCustomerCreation($value) + { + return $this->setParameter('customer_creation', $value); + } + + /** + * Get the customer_creation parameter + * + * @return string + */ + public function getCustomerCreation() + { + return $this->getParameter('customer_creation'); + } + + /** + * @return mixed + */ + public function getDestination() + { + return $this->getParameter('destination'); + } + + /** + * @param string $value + * + * @return AbstractRequest provides a fluent interface. + */ + public function setDestination($value) + { + return $this->setParameter('destination', $value); + } + + + /** + * Connect only + * + * @return mixed + */ + public function getTransferGroup() + { + return $this->getParameter('transferGroup'); + } + + /** + * @param string $value + * + * @return AbstractRequest provides a fluent interface. + */ + public function setTransferGroup($value) + { + return $this->setParameter('transferGroup', $value); + } + + /** + * Connect only + * + * @return mixed + */ + public function getOnBehalfOf() + { + return $this->getParameter('onBehalfOf'); + } + + /** + * @param string $value + * + * @return AbstractRequest provides a fluent interface. + */ + public function setOnBehalfOf($value) + { + return $this->setParameter('onBehalfOf', $value); + } + + + /** + * @return string + * @throws \Omnipay\Common\Exception\InvalidRequestException + */ + public function getApplicationFee() + { + $money = $this->getMoney('applicationFee'); + + if ($money !== null) { + $moneyFormatter = new DecimalMoneyFormatter($this->getCurrencies()); + + return $moneyFormatter->format($money); + } + + return ''; + } + + /** + * Get the payment amount as an integer. + * + * @return integer + * @throws \Omnipay\Common\Exception\InvalidRequestException + */ + public function getApplicationFeeInteger() + { + $money = $this->getMoney('applicationFee'); + + if ($money !== null) { + return (integer) $money->getAmount(); + } + + return 0; + } + + /** + * @param string $value + * + * @return AbstractRequest provides a fluent interface. + */ + public function setApplicationFee($value) + { + return $this->setParameter('applicationFee', $value); + } + + /** + * @return mixed + */ + public function getStatementDescriptor() + { + return $this->getParameter('statementDescriptor'); + } + + /** + * @param string $value + * + * @return AbstractRequest provides a fluent interface. + */ + public function setStatementDescriptor($value) + { + $value = str_replace(array('<', '>', '"', '\''), '', $value); + + return $this->setParameter('statementDescriptor', $value); + } + + /** + * @return mixed + */ + public function getStatementDescriptorSuffix() + { + return $this->getParameter('statementDescriptorSuffix'); + } + + /** + * @param string $value + * + * @return AbstractRequest provides a fluent interface. + */ + public function setStatementDescriptorSuffix($value) + { + $value = str_replace(array('<', '>', '"', '\''), '', $value); + + return $this->setParameter('statementDescriptorSuffix', $value); + } + public function getData() { + $paymentIntentData = array(); + + if ($this->getStatementDescriptor()) { + $paymentIntentData['statement_descriptor'] = $this->getStatementDescriptor(); + } + + if ($this->getStatementDescriptorSuffix()) { + $paymentIntentData['statement_descriptor_suffix'] = $this->getStatementDescriptorSuffix(); + } + + if ($this->getDestination()) { + $paymentIntentData['transfer_data']['destination'] = $this->getDestination(); + } + + if ($this->getOnBehalfOf()) { + $paymentIntentData['on_behalf_of'] = $this->getOnBehalfOf(); + } + + if ($this->getApplicationFee()) { + $paymentIntentData['application_fee_amount'] = $this->getApplicationFeeInteger(); + } + + if ($this->getTransferGroup()) { + $paymentIntentData['transfer_group'] = $this->getTransferGroup(); + } + $data = array( + 'client_reference_id' => $this->getClientReferenceId(), 'success_url' => $this->getSuccessUrl(), 'cancel_url' => $this->getCancelUrl(), 'payment_method_types' => $this->getPaymentMethodTypes(), 'mode' => $this->getMode(), + 'customer_creation' => $this->getCustomerCreation(), 'line_items' => $this->getLineItems() ); + + if (!empty($paymentIntentData)) { + $data['payment_intent_data'] = $paymentIntentData; + } return $data; } From 678e9ad4376fbe28a0de99a04dd61b39c4f57235 Mon Sep 17 00:00:00 2001 From: Andreas Radloff Date: Mon, 17 Jun 2024 13:50:28 +0200 Subject: [PATCH 3/3] Fix for mode=setup --- src/Message/Checkout/PurchaseRequest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Message/Checkout/PurchaseRequest.php b/src/Message/Checkout/PurchaseRequest.php index 13ec28f5..5a3136f6 100644 --- a/src/Message/Checkout/PurchaseRequest.php +++ b/src/Message/Checkout/PurchaseRequest.php @@ -351,8 +351,8 @@ public function getData() 'customer_creation' => $this->getCustomerCreation(), 'line_items' => $this->getLineItems() ); - - if (!empty($paymentIntentData)) { + + if (!empty($paymentIntentData) && $this->getMode() !== 'setup') { $data['payment_intent_data'] = $paymentIntentData; }