worknenjoy / gitpay

Bounties for issues on demand. Be rewarded by learning, using Git workflow and continuous integration
http://gitpay.me
Other
181 stars 161 forks source link

Can't make payments from coupons #894

Closed alexanmtz closed 2 years ago

alexanmtz commented 2 years ago

When we try to pay with payments from coupons, this is the result on the server:

{ code: 'parameter_invalid_integer',
     doc_url: 'https://stripe.com/docs/error-codes/parameter-invalid-integer',
     message: 'This value must be greater than or equal to 1.',
     param: 'amount',
     type: 'invalid_request_error',
     headers: 
      { server: 'nginx',
        date: 'Mon, 25 Apr 2022 13:41:01 GMT',
        'content-type': 'application/json',
        'content-length': '266',
        connection: 'close',
        'access-control-allow-credentials': 'true',
        'access-control-allow-methods': 'GET, POST, HEAD, OPTIONS, DELETE',
        'access-control-allow-origin': '*',
        'access-control-expose-headers': 'Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required',
        'access-control-max-age': '300',
        'cache-control': 'no-cache, no-store',
        'idempotency-key': '[80d3934a-2c9a-4f5d-97dc-a586b0245a7f](https://my.papertrailapp.com/systems/gitpay/events?q=%2280d3934a-2c9a-4f5d-97dc-a586b0245a7f%22&focus=1455242457737101334&selected=1455242457737101334)',
        'original-request': 'req_RnDSGSvae2ejPy',
        'request-id': 'req_RnDSGSvae2ejPy',
        'stripe-version': '2020-03-02',
        'strict-transport-security': 'max-age=31556926; includeSubDomains; preload' },
     statusCode: 400,
     requestId: 'req_RnDSGSvae2ejPy' }
VictorAlessander commented 2 years ago

It seems that if the coupon is for 100%, the amount will be zero and an amount equal to zero will cause this error. So, perhaps we are going to need to avoid a stripe payment request when the amount is less than zero.

I'll dig into it.

VictorAlessander commented 2 years ago

When the coupon discount is calculated based on the payment value it could remain to be paid at a value greater than zero but less than one (eg.: 0.4351), so, when it's going to calculate the rounded final amount using the function calFinalPrice, the value returned is zero.

Function calcFinalPrice that is defined on Plan's model: image

@alexanmtz we have two options here:

  1. Add a conditional to evaluate if the final value returned by calcFinalPrice is less than 1 but greater than zero so then in this case always return 1.
  2. Change the conditional related to the value after having applied the coupon discount if the value is less than one or 0.5, instead of the current validation that's evaluating if the value is equal to zero

Screenshot related to the point number 2: image

A useful explanation about Math.round here

alexanmtz commented 2 years ago

@VictorAlessander I think the second option sounds better, but let's be clear how this will work.

But we need to pay attention that when we apply the discount and is 100%, the order value is still the original amount, and when we transfer the values it will should pay the amount without the coupon to the assigned user, and when this happen it will use the platform balance, so in this case is like Gitpay is paying for the order.

If the order is $100 and the discount is 30% for example, the payer will have the discount but the order is still $100 and we when we transfer the $100 (with fees) will be paid. but because we gave the discount it will use $70 from the original payment, and $30 from our balance, but I think this will work automatically.

Every payment goes to our stripe balance and used to pay when the task is done, so when we transfer it will use this balance.

VictorAlessander commented 2 years ago

@alexanmtz

Yeah, I think that this flow will remain, this logic is referred to the payment to be made by the funding user that's the flow is going to create a charge to do it.

So I'm going to proceed with option two and we can validate on a sandbox environment if we gonna have any unexpected behavior.

alexanmtz commented 2 years ago

@VictorAlessander, the issue is still happening

error on task controller { Error: This value must be greater than or equal to 1.
    at Constructor._Error (/app/node_modules/stripe/lib/Error.js:12:17)
    at Constructor (/app/node_modules/stripe/lib/utils.js:124:13)
    at Function.StripeError.generate (/app/node_modules/stripe/lib/Error.js:57:12)
    at IncomingMessage.<anonymous> (/app/node_modules/stripe/lib/StripeResource.js:170:39)
    at emitNone (events.js:110:20)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  type: 'StripeInvalidRequestError',
VictorAlessander commented 2 years ago

@alexanmtz Ok, could you give me some info?

eg.: payment value, coupon percentage discount

alexanmtz commented 2 years ago

@VictorAlessander it seems to work now with a new test, check your notifications and let me know it works 👍