Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merchant can change subscription payment method #597

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
62 changes: 41 additions & 21 deletions src/Mollie/WC/Gateway/AbstractSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ abstract class Mollie_WC_Gateway_AbstractSubscription extends Mollie_WC_Gateway_
{

const PAYMENT_TEST_MODE = 'test';
const METHODS_NEEDING_UPDATE = ['mollie_wc_gateway_bancontact',
'mollie_wc_gateway_belfius',
'mollie_wc_gateway_directdebit',
'mollie_wc_gateway_eps',
'mollie_wc_gateway_giropay',
'mollie_wc_gateway_ideal',
'mollie_wc_gateway_kbc',
'mollie_wc_gateway_mistercash',
'mollie_wc_gateway_sofort'];
const DIRECTDEBIT = 'directdebit';

protected $isSubscriptionPayment = false;
/**
Expand Down Expand Up @@ -43,6 +53,7 @@ protected function initSubscriptionSupport()
'subscription_date_changes',
'multiple_subscriptions',
'subscription_payment_method_change',
'subscription_payment_method_change_admin',
'subscription_payment_method_change_customer',
);

Expand Down Expand Up @@ -121,17 +132,7 @@ public function update_subscription_status_for_direct_debit( $renewal_order ) {
}

// Check that payment method is SEPA Direct Debit or similar
$methods_needing_update = array (
'mollie_wc_gateway_bancontact',
'mollie_wc_gateway_belfius',
'mollie_wc_gateway_directdebit',
'mollie_wc_gateway_eps',
'mollie_wc_gateway_giropay',
'mollie_wc_gateway_ideal',
'mollie_wc_gateway_kbc',
'mollie_wc_gateway_mistercash',
'mollie_wc_gateway_sofort',
);
$methods_needing_update = self::METHODS_NEEDING_UPDATE;

if ( in_array( $current_method, $methods_needing_update ) == false ) {
return;
Expand Down Expand Up @@ -229,22 +230,30 @@ public function scheduled_subscription_payment( $renewal_total, WC_Order $renewa
do_action(Mollie_WC_Plugin::PLUGIN_ID . '_create_payment', $data, $renewal_order);
$mollieApiClient = Mollie_WC_Plugin::getApiHelper()->getApiClient($test_mode);
$validMandate = false;
$renewalOrderMethod = $renewal_order->get_payment_method();
$isRenewalMethodDirectDebit = in_array($renewalOrderMethod, self::METHODS_NEEDING_UPDATE);
$renewalOrderMethod = str_replace("mollie_wc_gateway_", "", $renewalOrderMethod);

try
{
if (!empty($mandateId)) {
Mollie_WC_Plugin::debug($this->id . ': Found mandate ID for renewal order ' . $renewal_order_id . ' with customer ID ' . $customer_id );
$mandate = $mollieApiClient->customers->get($customer_id)->getMandate($mandateId);
if ($mandate->status === 'valid') {
$bothDirectDebit = $mandate->method === self::DIRECTDEBIT
&& $isRenewalMethodDirectDebit;
$bothCreditcard = $mandate->method !== self::DIRECTDEBIT
&& !$isRenewalMethodDirectDebit;
$samePaymentMethodAsMandate = $bothDirectDebit || $bothCreditcard;
if ($mandate->status === 'valid' && $samePaymentMethodAsMandate) {
$data['method'] = $mandate->method;
$data['mandateId'] = $mandateId;
$validMandate = true;
}
} else {
}
if(!$validMandate){
// Get all mandates for the customer ID
Mollie_WC_Plugin::debug($this->id . ': Try to get all mandates for renewal order ' . $renewal_order_id . ' with customer ID ' . $customer_id );
$mandates = $mollieApiClient->customers->get($customer_id)->mandates();
$renewalOrderMethod = $renewal_order->get_payment_method();
$renewalOrderMethod = str_replace("mollie_wc_gateway_", "", $renewalOrderMethod);
foreach ($mandates as $mandate) {
if ($mandate->status === 'valid') {
$validMandate = true;
Expand All @@ -264,8 +273,20 @@ public function scheduled_subscription_payment( $renewal_total, WC_Order $renewa
// Check that there is at least one valid mandate
try {
if ( $validMandate ) {
Mollie_WC_Plugin::debug( $this->id . ': Valid mandate found for renewal order ' . $renewal_order_id );
$payment = Mollie_WC_Plugin::getApiHelper()->getApiClient( $test_mode )->payments->create( $data );
//update the valid mandate for this order
if ((property_exists($payment, 'mandateId')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to simply use isset here, because it does return false if the property exist but is null, while property_exists returns true and you need to check that is not null afterwards as in your current conditional.

so instead of:
if ((property_exists($payment, 'mandateId') && $payment->mandateId !== null)

just:
isset($payment->mandateId)

&& $payment->mandateId !== null)
&& $payment->mandateId !== $mandateId
&& !empty($subcriptionParentOrder)
) {
Mollie_WC_Plugin::debug("{$this->id}: updating to mandate {$payment->mandateId}");
$subcriptionParentOrder->update_meta_data(
'_mollie_mandate_id',
$payment->mandateId
);
$subcriptionParentOrder->save();
}
} else {
throw new \Mollie\Api\Exceptions\ApiException( sprintf( __( 'The customer (%s) does not have a valid mandate.', 'mollie-payments-for-woocommerce-mandate-problem' ), $customer_id ) );
}
Expand Down Expand Up @@ -365,12 +386,11 @@ public function updateFirstPaymentMethodToRecurringPaymentMethod( $renewal_order
'mollie_wc_gateway_ideal',
'mollie_wc_gateway_kbc',
'mollie_wc_gateway_mistercash',
'mollie_wc_gateway_sofort',
'mollie_wc_gateway_applepay'
'mollie_wc_gateway_sofort'
);

$current_method = get_post_meta( $renewal_order_id, '_payment_method', $single = true );
if ( in_array( $current_method, $methods_needing_update ) && $payment->method == 'directdebit' ) {
if ( in_array( $current_method, $methods_needing_update ) && $payment->method == self::DIRECTDEBIT) {
try {
$renewal_order->set_payment_method( 'mollie_wc_gateway_directdebit' );
$renewal_order->set_payment_method_title( 'SEPA Direct Debit' );
Expand Down Expand Up @@ -591,7 +611,7 @@ public function restore_mollie_customer_id_and_mandate( $mollie_customer_id, $mo
);

if ( in_array( $mollie_method, $methods_needing_update ) != false ) {
$mollie_method = 'directdebit';
$mollie_method = self::DIRECTDEBIT;
}

// Get all mandates for the customer
Expand All @@ -611,7 +631,7 @@ public function restore_mollie_customer_id_and_mandate( $mollie_customer_id, $mo
$sequence_type = $payment_object_resource->getSequenceTypeFromPaymentObject( $mollie_payment_id );

// Check SEPA Direct Debit payments and mandates
if ( $mollie_method == 'directdebit' && ! $mandates->hasValidMandateForMethod( $mollie_method ) && $payment_object->isPaid() && $sequence_type == 'oneoff' ) {
if ( $mollie_method == self::DIRECTDEBIT && ! $mandates->hasValidMandateForMethod($mollie_method ) && $payment_object->isPaid() && $sequence_type == 'oneoff' ) {

Mollie_WC_Plugin::debug( __METHOD__ . ' - Subscription ' . $subscription_id . ' renewal payment: no valid mandate for payment method ' . $mollie_method . ' found, trying to create one.' );

Expand Down