The setup you require is complex, and not doable with a simple JavaScript function.
CRED does not support natively any check for amount of Files upload.
You could work with that JS function provided in the linked post, or with a CRED API validation code.
https://toolset.com/documentation/user-guides/cred-api/#cfv
It requires a advanced application logic, since you also involve WooCommerce and want to redirect to Forms with payment in case the amount of Media Uploaded is reached.
I am not sure if your Media will be a types Media repeatable field, or native WordPress Media, or even a standalone Post.
What i would suggest is actually a mix of different forms, which are conditionally displayed with Access according the Role.
https://toolset.com/documentation/user-guides/access-control-for-cred-forms/
Each Form creates the same Post Type eventually, but would hold different Fields (as example a single instance Media field for Role A and a repeatable instance for Role B)
On Role B CRED forms you apply a Custom CRED API validation to check the repeatable instance is not greater than 5 or any amount of instances.
To let the user buy a package you will need yet another Form, as elaborated here, you need a CRED Form for each Product / package you want to let the user pay the additional uploads with:
https://toolset.com/documentation/user-guides/using-cred-commerce-to-add-payments-to-forms/
Also, actually the user should hold a Custom Field, so you can additionally evaluate by this field if the user is allowed to upload more images (after a purchase), otherwise it will simply keep the Role evaluation and never let the user to upload more images.
Also this would require a Custom CRED API code to update the User Meta on the fly when you submit the payment form.
For this you will use this Custom API function:
https://toolset.com/documentation/user-guides/cred-commerce-api/
The easier approach is actually to let the user decide from the first registration on your Site how much media he / she wants to upload.
Then, according to this you will register the user with a specific CRED Form that automatically will add the product / payment.
This will then create the user in the specific role, and allow the user to upload [xy] amount of media according your validation settings.
https://toolset.com/documentation/user-guides/cred-user-forms/
https://toolset.com/documentation/user-guides/charging-payments-with-cred-to-register-users/
Please let me know if you have further questions regarding the issue mentioned in this Thread
Thank you for your patience.