Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -9,11 +9,27 @@
|
||||
* @since 20 Jul 2017
|
||||
*/
|
||||
|
||||
\define('REPORT_EXCEPTIONS', TRUE);
|
||||
require_once '../../../../init.php';
|
||||
\IPS\Session\Front::i();
|
||||
use IPS\core\AdminNotification;
|
||||
use IPS\Db;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Math\Number;
|
||||
use IPS\Member;
|
||||
use IPS\nexus\Customer;
|
||||
use IPS\nexus\Fraud\MaxMind\Request;
|
||||
use IPS\nexus\Gateway;
|
||||
use IPS\nexus\Gateway\Stripe;
|
||||
use IPS\nexus\Invoice;
|
||||
use IPS\nexus\Money;
|
||||
use IPS\nexus\Transaction;
|
||||
use IPS\Output;
|
||||
use IPS\Session\Front;
|
||||
use IPS\Settings;
|
||||
|
||||
\IPS\Output::setCacheTime( false );
|
||||
define('REPORT_EXCEPTIONS', TRUE);
|
||||
require_once '../../../../init.php';
|
||||
Front::i();
|
||||
|
||||
Output::i()->pageCaching = FALSE;
|
||||
|
||||
/**
|
||||
* Stripe Webhook Handler
|
||||
@@ -23,17 +39,17 @@ class stripeWebhookHandler
|
||||
/**
|
||||
* @brief Raw Webhook Data (as a string)
|
||||
*/
|
||||
private $body;
|
||||
private mixed $body;
|
||||
|
||||
/**
|
||||
* @brief Parsed Webhook Data (as an array)
|
||||
*/
|
||||
private $data;
|
||||
private array $data;
|
||||
|
||||
/**
|
||||
* @brief Transaction
|
||||
*/
|
||||
private $transaction;
|
||||
private Transaction $transaction;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -41,7 +57,7 @@ class stripeWebhookHandler
|
||||
* @param string $body The raw body posted to this script
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $body )
|
||||
public function __construct( string $body )
|
||||
{
|
||||
$this->body = $body;
|
||||
$this->data = json_decode( $body, TRUE );
|
||||
@@ -52,24 +68,24 @@ class stripeWebhookHandler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function sourceChargeable()
|
||||
private function sourceChargeable() : string
|
||||
{
|
||||
/* Don't need to do anything for cards, since this is only for asynchronous payments */
|
||||
if ( isset( $this->data['data']['object']['type'] ) and \in_array( $this->data['data']['object']['type'], array( 'card' ) ) )
|
||||
if ( isset( $this->data['data']['object']['type'] ) and in_array( $this->data['data']['object']['type'], array( 'card' ) ) )
|
||||
{
|
||||
return 'NO_PROCESSING_REQUIRED';
|
||||
}
|
||||
|
||||
/* Check the status */
|
||||
if ( !\in_array( $this->transaction->status, array( \IPS\nexus\Transaction::STATUS_PENDING, \IPS\nexus\Transaction::STATUS_WAITING ) ) )
|
||||
if ( !in_array( $this->transaction->status, array( Transaction::STATUS_PENDING, Transaction::STATUS_WAITING ) ) )
|
||||
{
|
||||
if ( \in_array( $this->transaction->status, array( \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING, \IPS\nexus\Transaction::STATUS_PAID ) ) )
|
||||
if ( in_array( $this->transaction->status, array( Transaction::STATUS_GATEWAY_PENDING, Transaction::STATUS_PAID ) ) )
|
||||
{
|
||||
return 'ALREADY_PROCESSED';
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('BAD_STATUS');
|
||||
throw new Exception('BAD_STATUS');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,19 +93,19 @@ class stripeWebhookHandler
|
||||
$source = $this->transaction->method->api( 'sources/' . preg_replace( '/[^A-Z0-9_]/i', '', $this->data['data']['object']['id'] ), null, 'get' );
|
||||
if ( $source['client_secret'] != $this->data['data']['object']['client_secret'] )
|
||||
{
|
||||
throw new \Exception('BAD_SECRET');
|
||||
throw new Exception('BAD_SECRET');
|
||||
}
|
||||
|
||||
/* Check we're not just going to refuse this */
|
||||
$maxMind = NULL;
|
||||
if ( \IPS\Settings::i()->maxmind_key and ( !\IPS\Settings::i()->maxmind_gateways or \IPS\Settings::i()->maxmind_gateways == '*' or \in_array( $this->transaction->method->id, explode( ',', \IPS\Settings::i()->maxmind_gateways ) ) ) )
|
||||
if ( Settings::i()->maxmind_key and ( !Settings::i()->maxmind_gateways or Settings::i()->maxmind_gateways == '*' or in_array( $this->transaction->method->id, explode( ',', Settings::i()->maxmind_gateways ) ) ) )
|
||||
{
|
||||
$maxMind = new \IPS\nexus\Fraud\MaxMind\Request( FALSE );
|
||||
$maxMind = new Request( FALSE );
|
||||
$maxMind->setIpAddress( $this->transaction->ip );
|
||||
$maxMind->setTransaction( $this->transaction );
|
||||
}
|
||||
$fraudResult = $this->transaction->runFraudCheck( $maxMind );
|
||||
if ( $fraudResult === \IPS\nexus\Transaction::STATUS_REFUSED )
|
||||
if ( $fraudResult === Transaction::STATUS_REFUSED )
|
||||
{
|
||||
$this->transaction->executeFraudAction( $fraudResult, FALSE );
|
||||
$this->transaction->sendNotification();
|
||||
@@ -99,7 +115,7 @@ class stripeWebhookHandler
|
||||
else
|
||||
{
|
||||
$this->transaction->auth = $this->transaction->method->auth( $this->transaction, array( $this->transaction->method->id . '_card' => $source['id'] ) );
|
||||
$this->transaction->status = \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING;
|
||||
$this->transaction->status = Transaction::STATUS_GATEWAY_PENDING;
|
||||
$this->transaction->save();
|
||||
}
|
||||
|
||||
@@ -112,18 +128,18 @@ class stripeWebhookHandler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function chargeSucceeded()
|
||||
private function chargeSucceeded() : string
|
||||
{
|
||||
/* Check the status */
|
||||
if ( !\in_array( $this->transaction->status, [ \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING, IPS\nexus\Transaction::STATUS_PENDING ] ) )
|
||||
if ( !in_array( $this->transaction->status, [ Transaction::STATUS_GATEWAY_PENDING, IPS\nexus\Transaction::STATUS_PENDING ] ) )
|
||||
{
|
||||
if ( $this->transaction->status === \IPS\nexus\Transaction::STATUS_PAID )
|
||||
if ( $this->transaction->status === Transaction::STATUS_PAID )
|
||||
{
|
||||
return 'ALREADY_PROCESSED';
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('BAD_STATUS');
|
||||
throw new Exception('BAD_STATUS');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,21 +147,21 @@ class stripeWebhookHandler
|
||||
try
|
||||
{
|
||||
$charge = $this->transaction->method->api( 'charges/' . preg_replace( '/[^A-Z0-9_]/i', '', $this->data['data']['object']['id'] ), null, 'get' );
|
||||
if ( !\in_array( $charge['status'], array( 'succeeded', 'paid' ) ) )
|
||||
if ( !in_array( $charge['status'], array( 'succeeded', 'paid' ) ) )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception )
|
||||
{
|
||||
throw new \Exception('INVALID_CHARGE');
|
||||
throw new Exception('INVALID_CHARGE');
|
||||
}
|
||||
|
||||
/* Create a MaxMind request */
|
||||
$maxMind = NULL;
|
||||
if ( \IPS\Settings::i()->maxmind_key and ( !\IPS\Settings::i()->maxmind_gateways or \IPS\Settings::i()->maxmind_gateways == '*' or \in_array( $this->transaction->method->id, explode( ',', \IPS\Settings::i()->maxmind_gateways ) ) ) )
|
||||
if ( Settings::i()->maxmind_key and ( !Settings::i()->maxmind_gateways or Settings::i()->maxmind_gateways == '*' or in_array( $this->transaction->method->id, explode( ',', Settings::i()->maxmind_gateways ) ) ) )
|
||||
{
|
||||
$maxMind = new \IPS\nexus\Fraud\MaxMind\Request( FALSE );
|
||||
$maxMind = new Request( FALSE );
|
||||
$maxMind->setIpAddress( $this->transaction->ip );
|
||||
$maxMind->setTransaction( $this->transaction );
|
||||
}
|
||||
@@ -158,11 +174,11 @@ class stripeWebhookHandler
|
||||
}
|
||||
|
||||
/* If we're not being fraud blocked, we can approve */
|
||||
if ( $fraudResult === \IPS\nexus\Transaction::STATUS_PAID )
|
||||
if ( $fraudResult === Transaction::STATUS_PAID )
|
||||
{
|
||||
$this->transaction->member->log( 'transaction', array(
|
||||
'type' => 'paid',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_PAID,
|
||||
'status' => Transaction::STATUS_PAID,
|
||||
'id' => $this->transaction->id,
|
||||
'invoice_id' => $this->transaction->invoice->id,
|
||||
'invoice_title' => $this->transaction->invoice->title,
|
||||
@@ -182,39 +198,39 @@ class stripeWebhookHandler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function chargeSucceededNew()
|
||||
private function chargeSucceededNew() : string
|
||||
{
|
||||
/* Check the status */
|
||||
if ( !\in_array( $this->transaction->status, [ \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING, IPS\nexus\Transaction::STATUS_PENDING ] ) )
|
||||
if ( !in_array( $this->transaction->status, [ Transaction::STATUS_GATEWAY_PENDING, IPS\nexus\Transaction::STATUS_PENDING ] ) )
|
||||
{
|
||||
if ( $this->transaction->status === \IPS\nexus\Transaction::STATUS_PAID )
|
||||
if ( $this->transaction->status === Transaction::STATUS_PAID )
|
||||
{
|
||||
return 'ALREADY_PROCESSED';
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('BAD_STATUS');
|
||||
throw new Exception('BAD_STATUS');
|
||||
}
|
||||
}
|
||||
|
||||
/* If the invoice this is for is already paid, this may be a duplicate charge, so don't capture */
|
||||
if ( $this->transaction->invoice->status === \IPS\nexus\Invoice::STATUS_PAID )
|
||||
if ( $this->transaction->invoice->status === Invoice::STATUS_PAID )
|
||||
{
|
||||
throw new \Exception('INVOICE_ALREADY_PAID');
|
||||
throw new Exception('INVOICE_ALREADY_PAID');
|
||||
}
|
||||
|
||||
/* Validate the charge with Stripe */
|
||||
try
|
||||
{
|
||||
$charge = $this->transaction->method->api( 'charges/' . preg_replace( '/[^A-Z0-9_]/i', '', $this->data['data']['object']['id'] ), null, 'get' );
|
||||
if ( !\in_array( $charge['status'], array( 'succeeded', 'paid' ) ) )
|
||||
if ( !in_array( $charge['status'], array( 'succeeded', 'paid' ) ) )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception )
|
||||
{
|
||||
throw new \Exception('INVALID_CHARGE');
|
||||
throw new Exception('INVALID_CHARGE');
|
||||
}
|
||||
|
||||
/* If this was done by an admin, we just capture and approve straight away */
|
||||
@@ -224,11 +240,11 @@ class stripeWebhookHandler
|
||||
$this->transaction->capture();
|
||||
$this->transaction->member->log( 'transaction', array(
|
||||
'type' => 'paid',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_PAID,
|
||||
'status' => Transaction::STATUS_PAID,
|
||||
'id' => $this->transaction->id,
|
||||
'invoice_id' => $this->transaction->invoice->id,
|
||||
'invoice_title' => $this->transaction->invoice->title,
|
||||
), \IPS\Member::load( $this->data['data']['object']['metadata']['Admin'] ) );
|
||||
), Member::load( $this->data['data']['object']['metadata']['Admin'] ) );
|
||||
$this->transaction->approve();
|
||||
}
|
||||
|
||||
@@ -237,15 +253,15 @@ class stripeWebhookHandler
|
||||
{
|
||||
/* Create a MaxMind request */
|
||||
$maxMind = NULL;
|
||||
if ( \IPS\Settings::i()->maxmind_key and ( !\IPS\Settings::i()->maxmind_gateways or \IPS\Settings::i()->maxmind_gateways == '*' or \in_array( $this->transaction->method->id, explode( ',', \IPS\Settings::i()->maxmind_gateways ) ) ) )
|
||||
if ( Settings::i()->maxmind_key and ( !Settings::i()->maxmind_gateways or Settings::i()->maxmind_gateways == '*' or in_array( $this->transaction->method->id, explode( ',', Settings::i()->maxmind_gateways ) ) ) )
|
||||
{
|
||||
$maxMind = new \IPS\nexus\Fraud\MaxMind\Request( FALSE );
|
||||
$maxMind = new Request( FALSE );
|
||||
$maxMind->setIpAddress( $this->transaction->ip );
|
||||
$maxMind->setTransaction( $this->transaction );
|
||||
}
|
||||
|
||||
/* Set the authorize date (it's already actually been authorized) */
|
||||
$this->transaction->auth = \IPS\DateTime::ts( $this->data['data']['object']['created'] )->add( new \DateInterval( 'P7D' ) );
|
||||
$this->transaction->auth = \IPS\DateTime::ts( $this->data['data']['object']['created'] )->add( new DateInterval( 'P7D' ) );
|
||||
|
||||
/* Check Fraud Rules and capture */
|
||||
$this->transaction->checkFraudRulesAndCapture( $maxMind );
|
||||
@@ -263,18 +279,18 @@ class stripeWebhookHandler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function chargeFailed()
|
||||
private function chargeFailed() : string
|
||||
{
|
||||
/* Check the status */
|
||||
if ( !\in_array( $this->transaction->status, [ \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING, IPS\nexus\Transaction::STATUS_PENDING ] ) )
|
||||
if ( !in_array( $this->transaction->status, [ Transaction::STATUS_GATEWAY_PENDING, IPS\nexus\Transaction::STATUS_PENDING ] ) )
|
||||
{
|
||||
if ( $this->transaction->status === \IPS\nexus\Transaction::STATUS_REFUSED )
|
||||
if ( $this->transaction->status === Transaction::STATUS_REFUSED )
|
||||
{
|
||||
return 'ALREADY_PROCESSED';
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('BAD_STATUS');
|
||||
throw new Exception('BAD_STATUS');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,23 +300,23 @@ class stripeWebhookHandler
|
||||
$charge = $this->transaction->method->api( 'charges/' . preg_replace( '/[^A-Z0-9_]/i', '', $this->data['data']['object']['id'] ), null, 'get' );
|
||||
if ( $charge['status'] !== 'failed' )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception )
|
||||
{
|
||||
throw new \Exception('INVALID_CHARGE');
|
||||
throw new Exception('INVALID_CHARGE');
|
||||
}
|
||||
|
||||
/* Mark it failed */
|
||||
$this->transaction->status = \IPS\nexus\Transaction::STATUS_REFUSED;
|
||||
$this->transaction->status = Transaction::STATUS_REFUSED;
|
||||
$extra = $this->transaction->extra;
|
||||
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_REFUSED, 'noteRaw' => $this->data['data']['object']['failure_message'] );
|
||||
$extra['history'][] = array( 's' => Transaction::STATUS_REFUSED, 'noteRaw' => $this->data['data']['object']['failure_message'] );
|
||||
$this->transaction->extra = $extra;
|
||||
$this->transaction->save();
|
||||
$this->transaction->member->log( 'transaction', array(
|
||||
'type' => 'paid',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_REFUSED,
|
||||
'status' => Transaction::STATUS_REFUSED,
|
||||
'id' => $this->transaction->id,
|
||||
'invoice_id' => $this->transaction->invoice->id,
|
||||
'invoice_title' => $this->transaction->title,
|
||||
@@ -321,15 +337,15 @@ class stripeWebhookHandler
|
||||
private function paymentIntentFailed(): string
|
||||
{
|
||||
/* Check the status */
|
||||
if ( !\in_array( $this->transaction->status, [ \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING, IPS\nexus\Transaction::STATUS_PENDING ] ) )
|
||||
if ( !in_array( $this->transaction->status, [ Transaction::STATUS_GATEWAY_PENDING, Transaction::STATUS_PENDING ] ) )
|
||||
{
|
||||
if ( $this->transaction->status === \IPS\nexus\Transaction::STATUS_REFUSED )
|
||||
if ( $this->transaction->status === Transaction::STATUS_REFUSED )
|
||||
{
|
||||
return 'ALREADY_PROCESSED';
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('BAD_STATUS');
|
||||
throw new Exception('BAD_STATUS');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,23 +355,23 @@ class stripeWebhookHandler
|
||||
$pi = $this->transaction->method->api( 'payment_intents/' . $this->data['data']['object']['id'], null, 'get' );
|
||||
if ( !is_array( $pi['last_payment_error'] ) )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( Exception $e )
|
||||
{
|
||||
throw new \Exception('INVALID_PI');
|
||||
throw new Exception('INVALID_PI');
|
||||
}
|
||||
|
||||
/* Mark it failed */
|
||||
$this->transaction->status = \IPS\nexus\Transaction::STATUS_REFUSED;
|
||||
$this->transaction->status = Transaction::STATUS_REFUSED;
|
||||
$extra = $this->transaction->extra;
|
||||
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_REFUSED, 'noteRaw' => $this->data['data']['object']['last_payment_error']['message'] );
|
||||
$extra['history'][] = array( 's' => Transaction::STATUS_REFUSED, 'noteRaw' => $this->data['data']['object']['last_payment_error']['message'] );
|
||||
$this->transaction->extra = $extra;
|
||||
$this->transaction->save();
|
||||
$this->transaction->member->log( 'transaction', array(
|
||||
'type' => 'paid',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_REFUSED,
|
||||
'status' => Transaction::STATUS_REFUSED,
|
||||
'id' => $this->transaction->id,
|
||||
'invoice_id' => $this->transaction->invoice->id,
|
||||
'invoice_title' => $this->transaction->title,
|
||||
@@ -367,24 +383,24 @@ class stripeWebhookHandler
|
||||
/* Return */
|
||||
return 'OK';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A chargeback/dispute has been made against a transaction so we need to mark it as such locally
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function disputeCreated()
|
||||
private function disputeCreated() : string
|
||||
{
|
||||
/* Validate the dispute with Stripe */
|
||||
try
|
||||
{
|
||||
$dispute = $this->transaction->method->api( 'disputes/' . preg_replace( '/[^A-Z0-9_]/i', '', $this->data['data']['object']['id'] ), null, 'get' );
|
||||
if ( !\in_array( $dispute['status'], array( 'needs_response', 'warning_needs_response' ) ) )
|
||||
if ( !in_array( $dispute['status'], array( 'needs_response', 'warning_needs_response' ) ) )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
|
||||
if ( \substr( $this->transaction->gw_id, 0, 3 ) === 'pi_' )
|
||||
if ( substr( $this->transaction->gw_id, 0, 3 ) === 'pi_' )
|
||||
{
|
||||
$paymentIntent = $this->transaction->method->api( 'charges?payment_intent=' . $this->transaction->gw_id, null, 'get' );
|
||||
|
||||
@@ -400,23 +416,23 @@ class stripeWebhookHandler
|
||||
|
||||
if ( !$ok )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
elseif ( $dispute['charge'] !== $this->transaction->gw_id )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception )
|
||||
{
|
||||
throw new \Exception('INVALID_DISPUTE');
|
||||
throw new Exception('INVALID_DISPUTE');
|
||||
}
|
||||
|
||||
/* Mark the transaction as disputed */
|
||||
$this->transaction->status = \IPS\nexus\Transaction::STATUS_DISPUTED;
|
||||
$this->transaction->status = Transaction::STATUS_DISPUTED;
|
||||
$extra = $this->transaction->extra;
|
||||
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_DISPUTED, 'on' => $this->data['data']['object']['created'], 'ref' => $this->data['data']['object']['id'] );
|
||||
$extra['history'][] = array( 's' => Transaction::STATUS_DISPUTED, 'on' => $this->data['data']['object']['created'], 'ref' => $this->data['data']['object']['id'] );
|
||||
$this->transaction->extra = $extra;
|
||||
$this->transaction->save();
|
||||
|
||||
@@ -425,16 +441,16 @@ class stripeWebhookHandler
|
||||
{
|
||||
$this->transaction->member->log( 'transaction', array(
|
||||
'type' => 'status',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_DISPUTED,
|
||||
'status' => Transaction::STATUS_DISPUTED,
|
||||
'id' => $this->transaction->id
|
||||
) );
|
||||
}
|
||||
|
||||
/* Mark the invoice as not paid (revoking benefits) */
|
||||
$this->transaction->invoice->markUnpaid( \IPS\nexus\Invoice::STATUS_CANCELED );
|
||||
$this->transaction->invoice->markUnpaid( Invoice::STATUS_CANCELED );
|
||||
|
||||
/* Send admin notification */
|
||||
\IPS\core\AdminNotification::send( 'nexus', 'Transaction', \IPS\nexus\Transaction::STATUS_DISPUTED, TRUE, $this->transaction );
|
||||
AdminNotification::send( 'nexus', 'Transaction', Transaction::STATUS_DISPUTED, TRUE, $this->transaction );
|
||||
|
||||
/* Return */
|
||||
return 'OK';
|
||||
@@ -445,13 +461,13 @@ class stripeWebhookHandler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function disputeClosed()
|
||||
private function disputeClosed() : string
|
||||
{
|
||||
/* Validate the dispute with Stripe */
|
||||
try
|
||||
{
|
||||
$dispute = $this->transaction->method->api( 'disputes/' . preg_replace( '/[^A-Z0-9_]/i', '', $this->data['data']['object']['id'] ) );
|
||||
if ( \substr( $this->transaction->gw_id, 0, 3 ) === 'pi_' )
|
||||
if ( substr( $this->transaction->gw_id, 0, 3 ) === 'pi_' )
|
||||
{
|
||||
$paymentIntent = $this->transaction->method->api( 'charges?payment_intent=' . $this->transaction->gw_id, null, 'get' );
|
||||
|
||||
@@ -467,27 +483,27 @@ class stripeWebhookHandler
|
||||
|
||||
if ( !$ok )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
elseif ( $dispute['charge'] !== $this->transaction->gw_id )
|
||||
{
|
||||
throw new \Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception )
|
||||
{
|
||||
throw new \Exception('INVALID_DISPUTE');
|
||||
throw new Exception('INVALID_DISPUTE');
|
||||
}
|
||||
|
||||
/* Did we win or lose? */
|
||||
if ( \in_array( $dispute['status'], array( 'won', 'warning_closed' ) ) )
|
||||
if ( in_array( $dispute['status'], array( 'won', 'warning_closed' ) ) )
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
elseif ( \in_array( $dispute['status'], array( 'lost' ) ) )
|
||||
elseif ( in_array( $dispute['status'], array( 'lost' ) ) )
|
||||
{
|
||||
$this->transaction->status = \IPS\nexus\Transaction::STATUS_REFUNDED;
|
||||
$this->transaction->status = Transaction::STATUS_REFUNDED;
|
||||
$this->transaction->save();
|
||||
}
|
||||
|
||||
@@ -499,12 +515,12 @@ class stripeWebhookHandler
|
||||
* Run
|
||||
*
|
||||
* @param string $signature The signature provided by this request
|
||||
* @return void
|
||||
* @return string
|
||||
*/
|
||||
public function run( $signature )
|
||||
public function run( string $signature ) : string
|
||||
{
|
||||
/* Ignore anything we don't care about */
|
||||
if ( !isset( $this->data['type'] ) or !\in_array( $this->data['type'], array( 'source.chargeable', 'charge.succeeded', 'charge.failed', 'charge.dispute.created', 'charge.dispute.closed'/*, 'payment_intent.payment_failed'*/ ) ) )
|
||||
if ( !isset( $this->data['type'] ) or !in_array( $this->data['type'], array( 'source.chargeable', 'charge.succeeded', 'charge.failed', 'charge.dispute.created', 'charge.dispute.closed', 'payment_intent.payment_failed' ) ) )
|
||||
{
|
||||
return 'UNNEEDED_TYPE';
|
||||
}
|
||||
@@ -515,21 +531,19 @@ class stripeWebhookHandler
|
||||
{
|
||||
if ( isset( $this->data['data']['object']['metadata']['Transaction ID'] ) )
|
||||
{
|
||||
/* @note SELECT_FROM_WRITE_SERVER transaction race condition */
|
||||
$this->transaction = \IPS\nexus\Transaction::constructFromData( \IPS\Db::i()->select( '*', 'nexus_transactions', array( 't_id=?', $this->data['data']['object']['metadata']['Transaction ID'] ), flags: \IPS\Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
$this->transaction = Transaction::constructFromData( Db::i()->select( '*', 'nexus_transactions', array( 't_id=?', $this->data['data']['object']['metadata']['Transaction ID'] ), flags: Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
}
|
||||
elseif ( isset( $this->data['data']['object']['redirect']['return_url'] ) )
|
||||
{
|
||||
$url = new \IPS\Http\Url( $this->data['data']['object']['redirect']['return_url'] );
|
||||
/* @note SELECT_FROM_WRITE_SERVER transaction race condition */
|
||||
$this->transaction = \IPS\nexus\Transaction::constructFromData( \IPS\Db::i()->select( '*', 'nexus_transactions', array( 't_id=?', $url->queryString['nexusTransactionId'] ), flags: \IPS\Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
$url = new Url( $this->data['data']['object']['redirect']['return_url'] );
|
||||
$this->transaction = Transaction::constructFromData( Db::i()->select( '*', 'nexus_transactions', array( 't_id=?', $url->queryString['nexusTransactionId'] ), flags: Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
}
|
||||
elseif ( isset( $this->data['data']['object']['charge'] ) )
|
||||
{
|
||||
$paymentIntentId = NULL;
|
||||
foreach ( \IPS\nexus\Gateway::roots() as $method )
|
||||
foreach ( Gateway::roots() as $method )
|
||||
{
|
||||
if ( $method instanceof \IPS\nexus\Gateway\Stripe )
|
||||
if ( $method instanceof Stripe )
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -540,7 +554,7 @@ class stripeWebhookHandler
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception )
|
||||
{
|
||||
// Do nothing - try the next one
|
||||
}
|
||||
@@ -549,13 +563,11 @@ class stripeWebhookHandler
|
||||
|
||||
if ( $paymentIntentId )
|
||||
{
|
||||
/* @note SELECT_FROM_WRITE_SERVER transaction race condition */
|
||||
$this->transaction = \IPS\nexus\Transaction::constructFromData( \IPS\Db::i()->select( '*', 'nexus_transactions', array( '( t_gw_id=? OR t_gw_id=? )', $paymentIntentId, $this->data['data']['object']['charge'] ), flags: \IPS\Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
$this->transaction = Transaction::constructFromData( Db::i()->select( '*', 'nexus_transactions', array( '( t_gw_id=? OR t_gw_id=? )', $paymentIntentId, $this->data['data']['object']['charge'] ), flags: Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* @note SELECT_FROM_WRITE_SERVER transaction race condition */
|
||||
$this->transaction = \IPS\nexus\Transaction::constructFromData( \IPS\Db::i()->select( '*', 'nexus_transactions', array( 't_gw_id=?', $this->data['data']['object']['charge'] ), flags: \IPS\Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
{
|
||||
$this->transaction = Transaction::constructFromData( Db::i()->select( '*', 'nexus_transactions', array( 't_gw_id=?', $this->data['data']['object']['charge'] ), flags: Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
}
|
||||
}
|
||||
elseif ( isset( $this->data['data']['object']['id'] ) and mb_substr( $this->data['data']['object']['id'], 0, 4 ) !== 'src_' )
|
||||
@@ -576,24 +588,23 @@ class stripeWebhookHandler
|
||||
|
||||
try
|
||||
{
|
||||
/* @note SELECT_FROM_WRITE_SERVER transaction race condition */
|
||||
$this->transaction = \IPS\nexus\Transaction::constructFromData( \IPS\Db::i()->select( '*', 'nexus_transactions', $where, flags: \IPS\Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
$this->transaction = Transaction::constructFromData( Db::i()->select( '*', 'nexus_transactions', $where, flags: Db::SELECT_FROM_WRITE_SERVER )->first() );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
catch (UnderflowException $e )
|
||||
{
|
||||
/* If we can't find a transaction, but there is an "IP Address" in the metadata, this is from the actual checkout process: create a transaction */
|
||||
if ( isset( $this->data['data']['object']['metadata']['Invoice ID'] ) and isset( $this->data['data']['object']['metadata']['IP Address'] ) and isset( $this->data['data']['object']['metadata']['Payment Method ID'] ) )
|
||||
{
|
||||
$invoice = \IPS\nexus\Invoice::load( \intval( $this->data['data']['object']['metadata']['Invoice ID'] ) );
|
||||
$invoice = Invoice::load( intval( $this->data['data']['object']['metadata']['Invoice ID'] ) );
|
||||
|
||||
$this->transaction = new \IPS\nexus\Transaction;
|
||||
$this->transaction->member = isset( $this->data['data']['object']['metadata']['Customer ID'] ) ? \IPS\nexus\Customer::load( \intval( $this->data['data']['object']['metadata']['Customer ID'] ) ) : $invoice->member;
|
||||
$this->transaction = new Transaction;
|
||||
$this->transaction->member = isset( $this->data['data']['object']['metadata']['Customer ID'] ) ? Customer::load( intval( $this->data['data']['object']['metadata']['Customer ID'] ) ) : $invoice->member;
|
||||
$this->transaction->invoice = $invoice;
|
||||
$this->transaction->amount = new \IPS\nexus\Money( new \IPS\Math\Number( mb_substr( $this->data['data']['object']['amount'], 0, -2 ) . '.' . mb_substr( $this->data['data']['object']['amount'], -2 ) ), mb_strtoupper( $this->data['data']['object']['currency'] ) );
|
||||
$this->transaction->amount = new Money( new Number( mb_substr( $this->data['data']['object']['amount'], 0, -2 ) . '.' . mb_substr( $this->data['data']['object']['amount'], -2 ) ), mb_strtoupper( $this->data['data']['object']['currency'] ) );
|
||||
$this->transaction->ip = $this->data['data']['object']['metadata']['IP Address'];
|
||||
$this->transaction->gw_id = isset( $this->data['data']['object']['payment_intent'] ) ? $this->data['data']['object']['payment_intent'] : $this->data['data']['object']['id'];
|
||||
$this->transaction->status = \IPS\nexus\Transaction::STATUS_PENDING;
|
||||
$this->transaction->method = \IPS\nexus\Gateway::load( \intval( $this->data['data']['object']['metadata']['Payment Method ID'] ) );
|
||||
$this->transaction->gw_id = $this->data['data']['object']['payment_intent'] ?? $this->data['data']['object']['id'];
|
||||
$this->transaction->status = Transaction::STATUS_PENDING;
|
||||
$this->transaction->method = Gateway::load( intval( $this->data['data']['object']['metadata']['Payment Method ID'] ) );
|
||||
$this->transaction->save();
|
||||
|
||||
$new = TRUE;
|
||||
@@ -609,20 +620,20 @@ class stripeWebhookHandler
|
||||
return 'NO_TRANSACTION_INFORMATION';
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception )
|
||||
{
|
||||
if ( $this->data['type'] === 'charge.failed' )
|
||||
{
|
||||
return 'COULD_NOT_FIND_TRANSACTION'; // Sometimes if a transaction fails we don't create a transaction record
|
||||
}
|
||||
|
||||
throw new \Exception('COULD_NOT_FIND_TRANSACTION');
|
||||
throw new Exception('COULD_NOT_FIND_TRANSACTION');
|
||||
}
|
||||
|
||||
/* Validate the signature */
|
||||
if ( !( $this->transaction->method instanceof \IPS\nexus\Gateway\Stripe ) )
|
||||
if ( !( $this->transaction->method instanceof Stripe ) )
|
||||
{
|
||||
throw new \Exception('INVALID_GATEWAY');
|
||||
throw new Exception('INVALID_GATEWAY');
|
||||
}
|
||||
$settings = json_decode( $this->transaction->method->settings, TRUE );
|
||||
if ( isset( $settings['webhook_secret'] ) and $settings['webhook_secret'] ) // In case they upgraded and haven't provided one
|
||||
@@ -630,7 +641,7 @@ class stripeWebhookHandler
|
||||
$sig = array();
|
||||
foreach ( explode( ',', $signature ) as $row )
|
||||
{
|
||||
if ( \strpos( $row, '=' ) !== FALSE )
|
||||
if ( strpos( $row, '=' ) !== FALSE )
|
||||
{
|
||||
list( $k, $v ) = explode( '=', trim( $row ) );
|
||||
$sig[ trim( $k ) ][] = trim( $v );
|
||||
@@ -642,14 +653,14 @@ class stripeWebhookHandler
|
||||
$signedPayload = $sig['t'][0] . '.' . $this->body;
|
||||
$signature = hash_hmac( 'sha256', $signedPayload, $settings['webhook_secret'] );
|
||||
|
||||
if ( !\in_array( $signature, $sig['v1'] ) )
|
||||
if ( !in_array( $signature, $sig['v1'] ) )
|
||||
{
|
||||
throw new \Exception('INVALID_SIGNING_SECRET');
|
||||
throw new Exception('INVALID_SIGNING_SECRET');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('INVALID_SIGNING_SECRET');
|
||||
throw new Exception('INVALID_SIGNING_SECRET');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -668,12 +679,14 @@ class stripeWebhookHandler
|
||||
return $this->disputeCreated();
|
||||
case 'charge.dispute.closed':
|
||||
return $this->disputeClosed();
|
||||
/*case 'payment_intent.payment_failed':
|
||||
return $this->paymentIntentFailed();*/
|
||||
case 'payment_intent.payment_failed':
|
||||
return $this->paymentIntentFailed();
|
||||
default:
|
||||
return 'UNNEEDED_TYPE';
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -681,10 +694,10 @@ $class = new stripeWebhookHandler( trim( @file_get_contents('php://input') ) );
|
||||
try
|
||||
{
|
||||
sleep( 5 ); // Stripe can send web hooks very quickly, we need to delay to ensure we've processed the transaction first.
|
||||
$response = $class->run( isset( $_SERVER['HTTP_STRIPE_SIGNATURE'] ) ? $_SERVER['HTTP_STRIPE_SIGNATURE'] : '' );
|
||||
\IPS\Output::i()->sendOutput( $response, 200, 'text/plain' );
|
||||
$response = $class->run($_SERVER['HTTP_STRIPE_SIGNATURE'] ?? '');
|
||||
Output::i()->sendOutput( $response, 200, 'text/plain' );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (Exception $e)
|
||||
{
|
||||
\IPS\Output::i()->sendOutput( $e->getMessage(), 500, 'text/plain' );
|
||||
Output::i()->sendOutput( $e->getMessage(), 500, 'text/plain' );
|
||||
}
|
||||
Reference in new issue
Block a user