Assign role based on membership

I have three paid memberships. I want to assign contributor role to my members but not when they just register but when they make the payment. The role also should be assigned when I manually add them from dashboard.

Any help is appreciated.