For person A to be protected (meaning able to recover some or all of the money should the other party try to welsh on the deal), the two of them must have entered into a valid, binding contract where both parties acknowledge and agree to the debt and the terms. Such a contract is subject to the Statute of Frauds, a collection of laws governing contracts which is mostly borrowed from English common law.
The basics are that in all cases, a "contract" is only formed when both parties agree, technically when one party accepts an offer made by the other party. Both the offer and acceptance must be made sincerely. For a contract, once entered, to be enforceable, proof of the contract's existence and terms must itself exist. Certain types of transactions (real estate, large amounts of money) require contracts to be in written form, and witnessed by a trusted third party (in most cases this party is required to be a notary public). And contracts must have a certain amount of quid-pro-quo; contracts that provide a unilateral benefit can be thrown out on a case-by-case basis. A contract that simply states that Person B owes Person A money, without stating what benefit Person A had provided Person B in return for the money (in this case A gives B the money to begin with), is unenforceable. The benefits must of course be legal on both sides; a contract to deliver 5 tons of cocaine will not be upheld by any court in any free country, and neither will any contract attempting to enforce hush money, kickbacks, bribery etc (though some toe the line; one could argue that a signing bonus is tantamount to bribery). In some cases even seemingly benign clauses, like "escape clauses" allowing one party a "free out", can make the contract unenforceable as they could be abused to the severe detriment of one party. There are also jurisdiction-specific rules, such as limits on "finance charges" for debts not owed to a "bank" (a bar, for instance, cannot charge 10% on an outstanding tab in the United States). This is HUGE for your example, because if Person A had specified an interest rate in excess of the allowed rate for non-bank lenders, not only will the contract get thrown out even though Person B agreed to the terms, but Person A could find themselves on the hook for punitive damages payable to Person B, FAR in excess of the contracted amount.
Given that the agreement meets all tests of validity for a contract, if either party fails to perform in accordance with the contract, causing a loss or "tort" for the other party, the injured party can sue. Generally the two options are "strict performance" (the injuring party is ordered by the court to comply exactly with the terms of the contract), or payment of net actual damages and dissolution of the contract. In your example, if Person A had lent Person B money, strict performance would mean payment of the debt in the installments agreed, at the rate agreed; actual damages would be payment of the outstanding balance plus current interest charges (without any further penalty). Notice that it's "net" damages; if Person A was to issue the loan in installments, and missed one, causing Person B to suffer damages from the loss of expected cash flow directly resulting in their failure to pay according to the terms, then Person B's proven damages are subtracted from A's; very often, the plaintiff in a suit to recover money can end up owing the defendant for a prior failure to perform. There are further laws governing bankruptcy; basically, if the other person cannot satisfy the contract and cannot pay damages, they will pay what they can, and the contract is terminated with prejudice ("no blood from a turnip").