I think this should be what you need:
SELECT yourtable.*
FROM
yourtable INNER JOIN (
SELECT ip_address,
MAX(CASE WHEN type='purchase' THEN id END) max_purchase,
MAX(CASE WHEN type='visit' THEN id END) max_visit
FROM yourtable
GROUP BY ip_address) m
ON yourtable.id = COALESCE(max_purchase, max_visit)
Please see fiddle here.
My subquery will return the maximum purchase id (or null if there's no purchase) and the maximum visit id. Then I'm joining the table with COALESCE, if max_purchase is not null the join will be on max_purchase, otherwise it will be on max_visit.