paytrail / paytrail-for-woocommerce

Paytrail payment service for WooCommerce
MIT License
4 stars 10 forks source link

PaymentRequest - Custom Validation #175

Open agendad opened 1 month ago

agendad commented 1 month ago

Is your feature request related to a problem? Please describe.

We ran into an issue where successful PaymentRequests contained items with invalid vatPrecentage values that are not supported by the merchant of the WooCommerce site.

This was due to how we were handling negative fees in order to be compatible with Paytrail API. Apparently negative fees are now supported so this is no longer an issue.

We understand that Paytrail API validates vatPercentages between 0-100 and obviously has no idea what the merchant would like the vatPercentage to be.

Describe the solution you'd like

In order to avoid similar issues in future it would be helpful if it were possible to have a filter that allows the developer to validate the Paytrail\SDK\Request\PaymentRequest before it is sent to the Paytrail API.

In this instance, we would like to go through the items array and look for any vatPercentage values that do not align with WooCommerce settings. If there are any items with invalid vatPercentage, return false and display an error message at checkout without ever sending the PaymentRequest to the Paytrail API.

Additional context

Example Paytrail\SDK\Request\PaymentRequest JSON that has items containing invalid vatPercentage values.

{
  "stamp": "1-198100-1729244314",
  "reference": "198100",
  "amount": 2447,
  "currency": "EUR",
  "language": "EN",
  "items": [
    {
      "unitPrice": 2418,
      "units": 1,
      "vatPercentage": 34,
      "productCode": "1001",
      "description": "Test product 1",
      "stamp": "108"
    },
    {
      "unitPrice": 29,
      "units": 1,
      "vatPercentage": 0,
      "productCode": "rounding-row",
      "description": "Rounding"
    }
  ],
  "customer": {
    "email": "email@customer.com",
    "firstName": "Testiasiakas",
    "lastName": "Testaaja",
    "phone": "+35812345678",
    "companyName": "Test Company"
  },
  "invoicingAddress": {
    "streetAddress": "T\u00c4M\u00c4 ON TESTI TILAUS. EI TOIMITUSTA",
    "postalCode": "12600",
    "city": "Helsinki",
    "county": "",
    "country": "FI"
  },
  "redirectUrls": {
    "success": "https://staging.shop.fi/en/checkout/order-received/198100/?key=wc_order_hnxiUmOGq1mtp",
    "cancel": "https://staging.shop.fi/en/checkout/order-pay/198100/?pay_for_order=true&key=wc_order_hnxiUmOGq1mtp"
  },
  "callbackUrls": {
    "success": "https://staging.shop.fi/paytrail/callback/index",
    "cancel": "https://staging.shop.fi/paytrail/callback/index"
  },
  "callbackDelay": 3
}
kotivuori commented 3 weeks ago

Thanks for reporting @agendad, we will look into this.