mollie / mollie-api-php

Mollie API client for PHP
http://www.mollie.com
BSD 2-Clause "Simplified" License
552 stars 191 forks source link

Exception is thrown while action is successfully completed #555

Closed Loots-it closed 3 years ago

Loots-it commented 3 years ago

Specifications

Describe the issue

I get this error when I try to delete a payment (a payment that is cancelable):

Error executing API call (422: Unprocessable Entity): The payment cannot be cancelled. Documentation: https://docs.mollie.com/guides/handling-errors

However the payment is actually cancelled, and wasn't cancelled before the api call. This is the code I'm using:

        if ($payment && $payment->isCancelable)
        {
            $api->payments->cancel($paymentId, ['testmode' => !runningAppInProduction()]);
        }

I didn't have the time to check whether it's a issue from the api itself, or from this package, but in any case, I thought it would be worth mentioning it here.

sandervanhooft commented 3 years ago

Thanks for reporting this @Loots-it .

What happens if you run this?

$payload = [
  'description' => 'Issue 555 test',
  'amount' => [
    'currency' => 'EUR',
    'value' => '12.34',
  ],
  'redirectUrl' => 'https://www.example.com',
  'method' => 'banktransfer',
];

$payment = $api->payments->create($payload);

if($payment && $payment->isCancelable)
{
  $api->payments->cancel($payment->id);
}
Loots-it commented 3 years ago

I can't exactly run that in my current project as I'm currently working with oauth, but this modified script works as expected:

        $payload = [
            'description' => 'Issue 555 test',
            'amount' => [
                'currency' => 'EUR',
                'value' => '12.34',
            ],
            'redirectUrl' => 'https://www.example.com',
            'method' => 'banktransfer',
            'profileId' => 'secret...',
            'testmode' => true,
        ];

        $payment = $api->payments->create($payload);

        if($payment && $payment->isCancelable)
        {
            $api->payments->cancel($payment->id, ['testmode' => true]);
        }
Loots-it commented 3 years ago

I double checked to see if I made an error but I can't find one. Also, it's not like something happened in the meantime with the payment:

        $payment = $api->payments->get($paymentId, ['testmode' => !runningAppInProduction()]);
        if ($payment && $payment->isCancelable)
        {
            $api->payments->cancel($payment->id, ['testmode' => !runningAppInProduction()]);
        }
sandervanhooft commented 3 years ago

So if the method is set to banktransfer things work as expected. If method is not set (to banktransfer at least), the error occurs.

sandervanhooft commented 3 years ago

I'll notify Mollie support. But quickest way may be to contact support directly with your profileId and payment ids.

Loots-it commented 3 years ago

So if the method is set to banktransfer things work as expected. If method is not set (to banktransfer at least), the error occurs.

That's not the reason, I'm not sure what the difference is. If I try to replicate this with constant values and put it in one script it works fine. This is the code I'm using to create the payment. I double checked to see if I tried to cancel the payment twice but this wasn't the case.

        $payment =  $paymentsEndPoint->create([
            'amount' => [
                'currency' => 'EUR',
                'value' => $moneyFormatter->format($price),
            ],
            'method' => PaymentMethod::BANKTRANSFER,
            'description' => "Factuur $this->number",
            'redirectUrl' => config('app.url'),
            'customerId' => $client ? $client->mollie_customer_id : null,
            'locale' => $locale,
            'dueDate' => nowInBrussels()->addDays(100)->toDateString(),
            'webhookUrl' => route('webhooks.mollie.connect'),
            'profileId' => $this->owner->mollie_profile_id,
            'testmode' => !runningAppInProduction(),
        ]);
Loots-it commented 3 years ago

I'll notify Mollie support. But quickest way may be to contact support directly with your profileId and payment ids.

Yes I will do this. Thanks for your help!

sandervanhooft commented 3 years ago

Closing this for now, let me know if it should be reopened.