Skip to content
Merged
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
426 changes: 293 additions & 133 deletions CRM/Core/Payment/Square.php

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions CRM/Core/Payment/SquareIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
* Entry point: onReceiveWebhook() — called from CRM_Core_Payment_Square::handlePaymentNotification().
*
* Handles:
* subscription.created, subscription.updated, subscription.canceled
* invoice.created, invoice.payment_made, invoice.payment_failed
* subscription.created, subscription.updated
* invoice.created, invoice.payment_made, invoice.scheduled_charge_failed
* payment.updated, refund.created, refund.updated
*
* Square has no subscription.canceled event: a cancellation arrives as a
* subscription.updated with a canceled_date, while its status is still
* ACTIVE.
*
* Webhook lifecycle:
* 1. onReceiveWebhook() validates the event type, deduplicates via
* civicrm_paymentprocessor_webhook (a table owned by the mjwshared
Expand Down Expand Up @@ -111,10 +115,9 @@ public static function getSupportedEventTypes(): array {
return [
'subscription.created',
'subscription.updated',
'subscription.canceled',
'invoice.created',
'invoice.payment_made',
'invoice.payment_failed',
'invoice.scheduled_charge_failed',
'payment.updated',
'refund.created',
'refund.updated',
Expand Down Expand Up @@ -409,19 +412,13 @@ public function processWebhookEvent(array $payload, string $eventType): bool {
break;

case 'subscription.updated':
// Also how a cancellation arrives: Square sets a canceled_date.
if (!empty($this->subscription_id)) {
$this->_paymentProcessor->syncSubscriptionFromSquare($this->subscription_id);
CRM_Core_Payment_SquareDebugLogger::log("Square IPN: subscription.updated synced for {$this->subscription_id}");
}
break;

case 'subscription.canceled':
if (!empty($this->subscription_id)) {
$this->_paymentProcessor->syncSubscriptionCancellationFromSquare($this->subscription_id);
CRM_Core_Payment_SquareDebugLogger::log("Square IPN: subscription.canceled synced for {$this->subscription_id}");
}
break;

case 'invoice.created':
// Nothing to record. Square publishes invoice.created while the
// invoice is still a DRAFT it has not tried to charge, so it is never
Expand All @@ -430,7 +427,7 @@ public function processWebhookEvent(array $payload, string $eventType): bool {
// contribution CiviCRM created at checkout. The installment is
// recorded once Square confirms the charge, via invoice.payment_made
// or payment.updated (see
// CRM_Core_Payment_Square::recordSubscriptionInstallment()).
// CRM_Square_Reconciler::recordSubscriptionInstallment()).
CRM_Core_Payment_SquareDebugLogger::log("Square IPN: invoice.created noted for invoice {$this->invoice_id} (subscription " . ($this->subscription_id ?? 'null') . '); nothing is recorded until Square confirms payment.');
break;

Expand All @@ -439,7 +436,7 @@ public function processWebhookEvent(array $payload, string $eventType): bool {
CRM_Core_Payment_SquareDebugLogger::log("Square IPN: invoice.payment_made processed for invoice {$this->invoice_id}");
break;

case 'invoice.payment_failed':
case 'invoice.scheduled_charge_failed':
$invoice = $obj['invoice'] ?? [];
if (!empty($invoice)) {
$this->_paymentProcessor->syncInvoicePaymentFailedFromSquare($invoice);
Expand Down
19 changes: 19 additions & 0 deletions CRM/Core/Payment/SquareOutcomeUnknownException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Class CRM_Core_Payment_SquareOutcomeUnknownException.
*
* Thrown at checkout when Square may have charged the card but did not
* confirm it: a 5xx or 429 after the SDK's own retries, a timeout, an
* unreadable response, or a failure after Square accepted the charge.
*
* Deliberately not a PaymentProcessorException: on a contribution page that
* would make CiviCRM mark the contribution Failed, while the payment.updated
* webhook may yet show it was paid. As a plain CRM_Core_Exception the
* contribution stays Pending, for the webhook to complete. Event
* registration only catches PaymentProcessorException, so doPayment()
* converts it for that component.
*/
class CRM_Core_Payment_SquareOutcomeUnknownException extends CRM_Core_Exception {

}
80 changes: 80 additions & 0 deletions CRM/Square/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Converts between CiviCRM amounts and Square's smallest currency units.
*
* Square amounts are integers in the currency's smallest unit: cents for
* USD, but whole yen for JPY, which has no minor unit. Treating every
* currency as having cents would charge ¥10,000 for ¥100.
*/
class CRM_Square_Currency {

/**
* ISO 4217 currencies whose minor unit is not two decimal places.
*/
private const DECIMALS = [
// No minor unit.
'BIF' => 0,
'CLP' => 0,
'DJF' => 0,
'GNF' => 0,
'ISK' => 0,
'JPY' => 0,
'KMF' => 0,
'KRW' => 0,
'PYG' => 0,
'RWF' => 0,
'UGX' => 0,
'UYI' => 0,
'VND' => 0,
'VUV' => 0,
'XAF' => 0,
'XOF' => 0,
'XPF' => 0,
// Three decimal places.
'BHD' => 3,
'IQD' => 3,
'JOD' => 3,
'KWD' => 3,
'LYD' => 3,
'OMR' => 3,
'TND' => 3,
];

/**
* The number of decimal places in a currency's amounts.
*
* @param string|null $currency
* ISO 4217 code.
*
* @return int
*/
public static function decimals(?string $currency): int {
return self::DECIMALS[strtoupper(trim((string) $currency))] ?? 2;
}

/**
* An amount in Square's smallest unit of the currency (e.g. cents).
*
* @param float|int|string $amount
* @param string|null $currency
*
* @return int
*/
public static function toMinorUnits($amount, ?string $currency): int {
return (int) round((float) $amount * (10 ** self::decimals($currency)));
}

/**
* An amount from Square's smallest unit of the currency.
*
* @param int|string $minorUnits
* @param string|null $currency
*
* @return float
*/
public static function fromMinorUnits($minorUnits, ?string $currency): float {
return (float) $minorUnits / (10 ** self::decimals($currency));
}

}
Loading
Loading