Skip to content

Commit

Permalink
Remove ugly static utility functions (#1).
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Jun 14, 2022
1 parent 1ae804b commit 3741b47
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function start( Payment $payment ) {
$payment_request = new PaymentRequest(
AmountTransformer::transform( $payment->get_total_amount() ),
$this->config->get_merchant_account(),
Util::get_payment_reference( $payment_id ),
\sprintf( '%s-%s-%s', \get_current_network_id(), \get_current_blog_id(), $payment_id ),
$this->get_payment_return_url( $payment_id ),
$payment_method_details
);
Expand Down Expand Up @@ -273,7 +273,7 @@ public function payment_redirect( Payment $payment ) {
$request = new PaymentSessionRequest(
AmountTransformer::transform( $payment->get_total_amount() ),
$this->config->get_merchant_account(),
Util::get_payment_reference( $payment_id ),
\sprintf( '%s-%s-%s', \get_current_network_id(), \get_current_blog_id(), $payment_id ),
$this->get_payment_return_url( $payment_id )
);

Expand Down
12 changes: 11 additions & 1 deletion src/NotificationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,17 @@ public function rest_api_adyen_notifications( WP_REST_Request $request ) {
}

foreach ( $notification_request->get_items() as $item ) {
$payment_id = Util::get_reference_payment_id( $item->get_merchant_reference() );
$payment_id = $item->get_merchant_reference();

if ( \str_contains( $payment_id, '-' ) ) {
list( $scan_network_id, $scan_blog_id, $scan_payment_id ) = \sscanf( $payment_id, '%d-%d-%s' );

$payment_id = null;

if ( \get_current_network_id() === $scan_network_id && \get_current_blog_id() === $scan_blog_id ) {
$payment_id = $scan_payment_id;
}
}

$payment = get_pronamic_payment( $payment_id );

Expand Down
46 changes: 0 additions & 46 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,50 +77,4 @@ public static function get_country_code( Payment $payment ) {

return $country_code;
}

/**
* Get reference for payment including network and blog IDs.
*
* @since 4.1.0
* @param string|int $payment_id Payment ID.
* @return string
* @throws \InvalidArgumentException Throws error on empty payment ID.
*/
public static function get_payment_reference( $payment_id ) : string {
if ( empty( $payment_id ) ) {
throw new \InvalidArgumentException( 'Payment ID cannot be empty for a unique reference to the payment.' );
}

return \sprintf(
'%s-%s-%s',
\get_current_network_id(),
\get_current_blog_id(),
$payment_id
);
}

/**
* Get payment ID from merchant reference.
*
* @param string $reference Merchant reference.
* @return string|null
*/
public static function get_reference_payment_id( $reference ) : ?string {
// Reference notation without network and blog IDs for backward compatibility.
if ( ! \str_contains( $reference, '-' ) ) {
return $reference;
}

list( $scan_network_id, $scan_blog_id, $scan_payment_id ) = \sscanf( $reference, '%d-%d-%s' );

if ( \get_current_network_id() !== $scan_network_id ) {
return null;
}

if ( \get_current_blog_id() !== $scan_blog_id ) {
return null;
}

return $scan_payment_id;
}
}

0 comments on commit 3741b47

Please sign in to comment.