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

CheckoutGateway: Stripe Connect and Authorize/Capture support #236

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/CheckoutGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions src/Message/Checkout/AuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Stripe Checkout Session Request.
*/

namespace Omnipay\Stripe\Message\Checkout;

/**
* Stripe Checkout Session Request
*
* @see \Omnipay\Stripe\Gateway
* @link https://stripe.com/docs/api/checkout/sessions
*/
class AuthorizeRequest extends PurchaseRequest
{
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.
*/
$paymentIntentData['capture_method'] = 'manual';

$data['payment_intent_data'] = $paymentIntentData;

return $data;
}
}
200 changes: 200 additions & 0 deletions src/Message/Checkout/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Omnipay\Stripe\Message\Checkout;

use Money\Formatter\DecimalMoneyFormatter;

/**
* Stripe Checkout Session Request
*
Expand Down Expand Up @@ -145,17 +147,215 @@ 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) && $this->getMode() !== 'setup') {
$data['payment_intent_data'] = $paymentIntentData;
}

return $data;
}

Expand Down