Skip to content

Commit

Permalink
Merge pull request #6 from coralsio/create-stripe-payment-intent
Browse files Browse the repository at this point in the history
#support create payment intent
  • Loading branch information
saeed-corals authored Jun 10, 2024
2 parents f3ace7e + 42c9509 commit 163d1e9
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 9 deletions.
45 changes: 42 additions & 3 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ public function purchase(array $parameters = array())
return $this->createRequest('\Corals\Modules\Payment\Stripe\Message\PurchaseRequest', $parameters);
}

/**
* @param array $parameters
* @return mixed
*/
public function createPaymentIntent(array $parameters = array())
{
return $this->createRequest('\Corals\Modules\Payment\Stripe\Message\CreatePaymentIntentRequest', $parameters);
}

/**
* Refund Request.
*
Expand Down Expand Up @@ -1128,7 +1137,8 @@ public function prepareSubscriptionParameters(
User $user,
Subscription $subscription = null,
$subscription_data = null
) {
)
{
$parameters = ['customerReference' => $user->integration_id, 'plan' => $plan->code];
if ($subscription) {
$parameters['trial_end'] = $subscription->trial_ends_at ? $subscription->trial_ends_at->getTimestamp() : 'now';
Expand Down Expand Up @@ -1239,13 +1249,31 @@ public function preparePaymentTokenParameters($amount, $currency, $description,
'amount' => $amount,
'currency' => $currency,
'description' => $description,
'confirm' => true,
'paymentMethod' => $params['payment_method_id']
'confirm' => data_get($params, 'confirm', true) ?? true,
'paymentMethod' => $params['payment_method_id'],
'CustomerReference' => data_get($params, 'customerReference'),
'confirmation_method' => data_get($params, 'confirmation_method', 'manual'),
'capture_method' => data_get($params, 'capture_method', 'manual'),
];

return $parameters;
}

/**
* @param $amount
* @param array $params
* @return array
*/
public function preparePaymentIntentParameters($amount, $params = [])
{
return [
'description' => data_get($params, 'description'),
'currency' => data_get($params, 'currency'),
'statement_descriptor' => data_get($params, 'statement_descriptor'),
'amount' => $amount
];
}


public function prepareCheckPaymentTokenParameters($params = [])
{
Expand All @@ -1262,6 +1290,17 @@ public function checkPaymentToken(array $parameters = array())
return $this->createRequest('\Corals\Modules\Payment\Stripe\Message\FetchPaymentIntentRequest', $parameters);
}

public function fetchPaymentMethod(array $parameters = array())
{
return $this->createRequest('\Corals\Modules\Payment\Stripe\Message\FetchPaymentMethodRequest', $parameters);
}


public function updatePaymentIntent(array $parameters = [])
{
return $this->createRequest('\Corals\Modules\Payment\Stripe\Message\UpdatePaymentIntentRequest', $parameters);
}

public function confirmPaymentToken(array $parameters = array())
{
return $this->createRequest('\Corals\Modules\Payment\Stripe\Message\ConfirmPaymentIntentRequest', $parameters);
Expand Down
2 changes: 1 addition & 1 deletion src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function getMoney($parameterName = 'amount')
throw new InvalidRequestException(trans('Stripe::exception.amount_is_too_high'));
}

$money = $moneyParser->parse((string)$number, $currency->getCode());
$money = $moneyParser->parse((string)$number, $currency);

// Check for a negative amount.
if (!$this->negativeAmountAllowed && $money->isNegative()) {
Expand Down
72 changes: 72 additions & 0 deletions src/Message/CreatePaymentIntentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* Stripe Payment Intent Create Request.
*/

namespace Corals\Modules\Payment\Stripe\Message;


/**
* Class CreatePaymentIntentRequest
* @package Corals\Modules\Payment\Stripe\Message
*/
class CreatePaymentIntentRequest extends AbstractRequest
{
/**
* @param $value
* @return CreatePaymentIntentRequest
*/
public function getIntentRequestData()
{
return $this->getParameter('intentRequestData');
}

/**
* @param $value
* @return CreatePaymentIntentRequest
*/
public function setIntentRequestData($value)
{
return $this->setParameter('intentRequestData', $value);
}

/**
* @inheritdoc
*/
public function getData()
{
$this->validate('amount');

$data = $this->getIntentRequestData();

$data['amount'] = $this->getAmountInteger();

return $data;
}


/**
* @inheritdoc
*/
public function getHttpMethod()
{
return 'POST';
}

/**
* @inheritdoc
*/
public function getEndpoint()
{
return $this->endpoint . '/payment_intents';
}

/**
* @inheritdoc
*/
protected function createResponse($data, $headers = [])
{
return $this->response = new Response($this, $data, $headers);
}
}
60 changes: 60 additions & 0 deletions src/Message/FetchPaymentMethodRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Stripe Payment Method Fetch Request.
*/

namespace Corals\Modules\Payment\Stripe\Message;


/**
* Class FetchPaymentIntentRequest
* @package Corals\Modules\Payment\Stripe\Message
*/
class FetchPaymentMethodRequest extends AbstractRequest
{
/**
* @inheritdoc
*/
public function getData()
{
$this->validate('paymentMethodId');
}

public function setPaymentMethodId($value)
{
return $this->setParameter('paymentMethodId', $value);
}

/**
* @return mixed
*/
public function getPaymentMethodId()
{
return $this->getParameter('paymentMethodId');
}

/**
* @inheritdoc
*/
public function getHttpMethod()
{
return 'GET';
}

/**
* @inheritdoc
*/
public function getEndpoint()
{
return $this->endpoint . '/payment_methods/' . $this->getPaymentMethodId();
}

/**
* @inheritdoc
*/
protected function createResponse($data, $headers = [])
{
return $this->response = new Response($this, $data, $headers);
}
}
24 changes: 22 additions & 2 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ public function getConfirm()
return $this->getParameter('confirm');
}

public function getConfirmationMethod()
{
return $this->getParameter('confirmation_method');
}

public function getCaptureMethod()
{
return $this->getParameter('confirmation_method');
}

public function setCaptureMethod($value)
{
return $this->setParameter('capture_method', $value);
}

public function setConfirmationMethod($value)
{
return $this->setParameter('confirmation_method', $value);
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -346,8 +366,8 @@ public function getData()
$data['customer'] = $this->getCustomerReference();
}

$data['confirmation_method'] = 'manual';
$data['capture_method'] = 'manual';
$data['confirmation_method'] = $this->getConfirmationMethod() ?? 'manual';
$data['capture_method'] = $this->getCaptureMethod() ?? 'manual';

$data['confirm'] = $this->getConfirm() ? 'true' : 'false';

Expand Down
11 changes: 9 additions & 2 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function isSuccessful()
* Get the charge reference from the response of FetchChargeRequest.
*
* @return array|null
*@see \Corals\Modules\Payment\Stripe\Message\Response::getTransactionReference()
* @see \Corals\Modules\Payment\Stripe\Message\Response::getTransactionReference()
* @deprecated 2.3.3:3.0.0 duplicate of \Corals\Modules\Payment\Stripe\Message\Response::getTransactionReference()
*/
public function getChargeReference()
Expand Down Expand Up @@ -184,8 +184,15 @@ public function getPaymentTokenReference()
{
if (isset($this->data['object']) && 'payment_intent' === $this->data['object']) {

return json_encode([
'payment_intent_id' => $this->data['id'],
'status' => $this->data['status'],
'next_action' => $this->data['next_action'],
'client_secret' => $this->data['client_secret'],
'amount' => $this->data['amount'],
'payment_method' => $this->data['payment_method']
]);

return json_encode(['payment_intent_id' => $this->data['id'], 'status' => $this->data['status'], 'next_action' => $this->data['next_action'], 'client_secret' => $this->data['client_secret']]);
}

return null;
Expand Down
67 changes: 67 additions & 0 deletions src/Message/UpdatePaymentIntentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* Stripe Payment Intent Fetch Request.
*/

namespace Corals\Modules\Payment\Stripe\Message;


/**
* Class CreatePaymentIntentRequest
* @package Corals\Modules\Payment\Stripe\Message
*/
class UpdatePaymentIntentRequest extends AbstractRequest
{

/**
* @inheritdoc
*/
public function getData()
{
$this->validate('amount');

return [
'amount' => $this->getAmountInteger(),
];
}

public function setPaymentIntentId($value)
{
return $this->setParameter('paymentIntentId', $value);
}

/**
* @return mixed
*/
public function getPaymentIntentId()
{
return $this->getParameter('paymentIntentId');
}



/**
* @inheritdoc
*/
public function getHttpMethod()
{
return 'POST';
}

/**
* @inheritdoc
*/
public function getEndpoint()
{
return $this->endpoint . '/payment_intents/' . $this->getPaymentIntentId();
}

/**
* @inheritdoc
*/
protected function createResponse($data, $headers = [])
{
return $this->response = new Response($this, $data, $headers);
}
}
5 changes: 4 additions & 1 deletion src/Providers/UninstallModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Corals\Foundation\Providers\BaseUninstallModuleServiceProvider;
use Corals\Settings\Models\Setting;
use Corals\User\Models\User;
use Illuminate\Support\Facades\Schema;

class UninstallModuleServiceProvider extends BaseUninstallModuleServiceProvider
{
Expand All @@ -22,7 +23,9 @@ protected function providerBooted()

Setting::where('code', 'like', 'payment_stripe%')->delete();

User::where('gateway', 'Stripe')->update(['gateway' => NULL]);
if (Schema::hasColumn('users', 'gateway')) {
User::where('gateway', 'Stripe')->update(['gateway' => NULL]);
}

GatewayStatus::where('gateway', 'Stripe')->delete();
}
Expand Down

0 comments on commit 163d1e9

Please sign in to comment.