Commit 53d5ba59 authored by Michael Iseard's avatar Michael Iseard
Browse files

Add "Add missing transactions" command to debug

parent e7449cb8
......@@ -541,7 +541,7 @@ class Admin {
new AdminNotice( __( 'Database re-created', 'kudos-donations' ) );
break;
case 'kudos_sync_payments':
case 'kudos_sync_transactions':
$mollie = $this->mollie;
$updated = $mollie->sync_transactions();
if ( $updated ) {
......@@ -558,6 +558,25 @@ class Admin {
break;
}
new AdminNotice( __( 'No transactions need updating', 'kudos-donations' ) );
break;
case 'kudos_add_missing_transactions':
$mollie = $this->mollie;
$updated = $mollie->add_missing_transactions();
if ( $updated ) {
new AdminNotice(
sprintf(
/* translators: %s: Number of records. */
_n( 'Added %s transaction',
'Added %s transactions',
$updated,
'kudos-donations' ),
$updated
)
);
break;
}
new AdminNotice( __( 'No transactions need adding', 'kudos-donations' ) );
}
do_action( 'kudos_admin_actions_extra', $action );
......
......@@ -732,7 +732,72 @@ class MollieVendor implements VendorInterface {
$order_id = $payment->metadata->order_id ?? null;
$mapper->get_repository( TransactionEntity::class );
if($order_id) {
if ( $order_id ) {
/**
* Find existing transaction.
* @var TransactionEntity $transaction
*/
$transaction = $mapper->get_one_by( [
'order_id' => $order_id,
'status' => 'open',
] );
if ( $transaction ) {
$transaction->set_fields(
[
'status' => $payment->status,
'customer_id' => $payment->customerId,
'value' => $amount->value,
'currency' => $amount->currency,
'sequence_type' => $payment->sequenceType,
'method' => $payment->method,
'mode' => $payment->mode,
'subscription_id' => $payment->subscriptionId,
'campaign_id' => $payment->metadata ? $payment->metadata->campaign_id : null,
]
);
$mapper->save( $transaction );
$updated ++;
}
}
}
} catch ( ApiException $e ) {
$this->logger->error( $e->getMessage() );
}
}
}
return $updated;
}
/**
* Adds missing transactions from Mollie.
* Returns the number of transactions added.
*
* @return int
*/
public function add_missing_transactions(): int {
$added = 0;
$mapper = $this->mapper;
$mapper->get_repository( DonorEntity::class );
$donors = $mapper->get_all_by();
/** @var DonorEntity $donor */
foreach ( $donors as $donor ) {
$customer_id = $donor->customer_id;
if ( $donor->mode !== $this->api_mode ) {
$this->set_api_mode( $donor->mode );
}
$customer = $this->get_customer( $customer_id );
if ( $customer ) {
try {
$payments = $customer->payments();
foreach ( $payments as $payment ) {
$order_id = $payment->metadata->order_id ?? null;
if ( $order_id ) {
$mapper->get_repository( TransactionEntity::class );
/**
* Find existing transaction.
......@@ -744,30 +809,26 @@ class MollieVendor implements VendorInterface {
// Add new transaction if none found.
if ( ! $transaction ) {
$transaction = new TransactionEntity( [
'order_id' => $order_id,
] );
} else {
$transaction->set_fields([
'created' => $payment->createdAt
]);
}
$transaction->set_fields(
[
$transaction = new TransactionEntity( [
'order_id' => $order_id,
'created' => $payment->createdAt,
'status' => $payment->status,
'customer_id' => $payment->customerId,
'value' => $amount->value,
'currency' => $amount->currency,
'value' => $payment->amount->value,
'currency' => $payment->amount->currency,
'sequence_type' => $payment->sequenceType,
'method' => $payment->method,
'mode' => $payment->mode,
'subscription_id' => $payment->subscriptionId,
'transaction_id' => $payment->id,
'campaign_id' => $payment->metadata ? $payment->metadata->campaign_id : null,
]
);
$mapper->save( $transaction );
$updated ++;
] );
$mapper->save( $transaction );
$added ++;
}
}
}
} catch ( ApiException $e ) {
......@@ -776,6 +837,6 @@ class MollieVendor implements VendorInterface {
}
}
return $updated;
return $added;
}
}
......@@ -200,10 +200,17 @@ $tab = $_GET['tab'] ?? $default_tab;
<hr/>
<p>Mollie actions.</p>
<form action="<?php echo esc_url( $url ); ?>" method='post'>
<?php wp_nonce_field( 'kudos_sync_payments' ); ?>
<form action="<?php echo esc_url( $url ); ?>" method='post' style="display: inline">
<?php wp_nonce_field( 'kudos_sync_transactions' ); ?>
<button class="button-secondary confirm" type='submit' name='kudos_action'
value='kudos_sync_transactions'>Sync transactions
</button>
</form>
<form action="<?php echo esc_url( $url ); ?>" method='post' style="display: inline">
<?php wp_nonce_field( 'kudos_add_missing_transactions' ); ?>
<button class="button-secondary confirm" type='submit' name='kudos_action'
value='kudos_sync_payments'>Sync payments
value='kudos_add_missing_transactions'>Add missing transactions
</button>
</form>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment