Viincenttt / MollieApi

This project allows you to easily add the Mollie payment provider to your application.
MIT License
152 stars 85 forks source link

Test mode #208

Closed renetitulaer closed 3 years ago

renetitulaer commented 3 years ago

Hi,

I can't get the test mode to work. It only works with oauth, ok. But there's check: if (!this.apiKey.StartsWith("access")) { if (isConstructor) { throw new InvalidOperationException( "The provided token isn't an oauth token. You have invoked the method with oauth parameters thus an oauth accesstoken is required."); }

            throw new ArgumentException("The provided token isn't an oauth token.");
        }

But I only have a test api key which doesn't start with access_ so I get an error. This curl command works with my test api key:

curl -X POST https://api.mollie.com/v2/payments ^ -H "Authorization: Bearer " ^ -d "amount[currency]=EUR" ^ -d "amount[value]=10.00" ^ -d "description=Order #12345" ^ -d "redirectUrl=https://webshop.example.org/order/12345/" ^ -d "webhookUrl=https://webshop.example.org/payments/webhook/" ^ -d "metadata={\"order_id\": \"12345\"}"

Viincenttt commented 3 years ago

Hi,

If you are using a test api key, you can just set testmode to false. A test API key can never generate real payments and will always create test payments. The testmode parameter can only be used in combination with oauth tokens.

Kind regards, Vincent

renetitulaer commented 3 years ago

Hi, thank you for the response.

so what I understood is that there are two ways to run in test mode, using the test api key or setting the property testmode to true.

I use the test api key: IPaymentClient paymentClient = new PaymentClient("test_..."); PaymentRequest paymentRequest = new IdealPaymentRequest() { Amount = new Amount(Currency.EUR, amount), Description = description, RedirectUrl = $"{returnUrl}", //WebhookUrl = webhookUrl, }; PaymentResponse paymentResponse = await paymentClient.CreatePaymentAsync(paymentRequest);

This didn't work with Molie.Api but when I removed the check in the source code it does: protected void ValidateApiKeyIsOauthAccesstoken(bool isConstructor = false) { //if (!this.apiKey.StartsWith("access")) { // if (isConstructor) { // throw new InvalidOperationException( // "The provided token isn't an oauth token. You have invoked the method with oauth parameters thus an oauth accesstoken is required."); // }

        //    throw new ArgumentException("The provided token isn't an oauth token.");
        //}
    }

So it looks like the package doesn't support using the test api key.

Regards, René

Viincenttt commented 3 years ago

Hi,

I can assure you it works with a test api key, as I don't even have a real api key myself. I have not been able to reproduce the issue that you have mentioned using the code that you posted. The following code executes without issues for me using a test api key:

var paymentClient = new PaymentClient("test_...");
            PaymentRequest paymentRequest = new IdealPaymentRequest() {
                Amount = new Amount(Currency.EUR, 5m),
                Description = "blah",
                RedirectUrl = $"http://www.google.com",
                //WebhookUrl = webhookUrl,
            };
            PaymentResponse paymentResponse = await paymentClient.CreatePaymentAsync(paymentRequest);

Take a look at the CreatePaymentAsync method inside of the PaymentClient class. You'll see the following code:

if (!string.IsNullOrWhiteSpace(paymentRequest.ProfileId) || paymentRequest.Testmode.HasValue || paymentRequest.ApplicationFee != null) {
    this.ValidateApiKeyIsOauthAccesstoken();
}

This means that the ValidateApiKeyIsOauthAccesstoken is only executed if one of the following conditions is true:

  1. You are passing in the ProfileId parameter
  2. You are passing in the TestMode parameter
  3. You are passing in the ApplicationFee parameter

Since you aren't doing any of these things in the sample code you have provided, the ValidateApiKeyIsOauthAccesstoken should not be called and is not called when I run the code.

renetitulaer commented 3 years ago

Hi Vincent,

you are right. Don't know what I did do wrong before but now it works.

Thanks you for your response and doing it this quick.

Also thanks for making the library and sharing it.

Best regards, René

Viincenttt commented 3 years ago

Hi @renetitulaer ,

No problem! Good to hear that you got it working!

Kind regards, Vincent