Menu
Coddy logo textTech

Recap - Payment Processor

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 37 of 91.

challenge icon

Challenge

Easy

Let's build a complete payment processing system that brings together everything you've learned about polymorphism. You'll create an interface-based architecture where different payment methods can be processed through a single, unified processor — demonstrating how polymorphism makes code flexible and extensible.

You'll organize your code across five files:

  • PaymentMethod.php — Define a PaymentMethod interface with two method signatures: process(float $amount): string for handling the payment, and a static method getMethodName(): string that returns the payment type name. This contract ensures any payment method can be processed uniformly.
  • CreditCard.php — Create a CreditCard class that implements PaymentMethod. Include the interface file. The class should have a private $lastFourDigits property set through the constructor. Implement process() to return "Processing $[amount] via Credit Card ending in [lastFourDigits]" (format amount to 2 decimal places). Implement the static getMethodName() using late static binding to return "Credit Card".
  • PayPal.php — Create a PayPal class that implements PaymentMethod. Include the interface file. The class should have a private $email property set through the constructor. Implement process() to return "Processing $[amount] via PayPal account [email]" (format amount to 2 decimal places). Implement the static getMethodName() to return "PayPal".
  • PaymentProcessor.php — Create a PaymentProcessor class that orchestrates payments. Include the interface file. Add a method executePayment(PaymentMethod $method, float $amount): string that uses interface type hinting to accept any payment method. This method should return the result of calling process() on the payment method. Add another method describeMethod(PaymentMethod $method): string that returns "Payment via: [methodName]" using the static getMethodName() from the passed payment method's class.
  • main.php — Include all the payment class files and the processor. Create a PaymentProcessor instance. You'll receive three inputs: the last four digits of a credit card, a PayPal email address, and a payment amount. Create both a CreditCard and a PayPal instance with their respective identifiers. Convert the amount to a float. Use the processor to execute a payment with the credit card first, then with PayPal. Print each result on its own line. Finally, print the method description for the PayPal payment method.

The beauty of this design is that your PaymentProcessor doesn't know anything about credit cards or PayPal specifically — it only knows about the PaymentMethod interface. You could add a BankTransfer or Cryptocurrency class tomorrow, and the processor would handle them without any modifications. That's the power of interface-based polymorphism combined with late static binding!

Try it yourself

<?php
// Include all required files
require_once 'CreditCard.php';
require_once 'PayPal.php';
require_once 'PaymentProcessor.php';

// Read inputs
$lastFourDigits = trim(fgets(STDIN));
$paypalEmail = trim(fgets(STDIN));
$amount = floatval(trim(fgets(STDIN)));

// TODO: Create a PaymentProcessor instance

// TODO: Create a CreditCard instance with the last four digits

// TODO: Create a PayPal instance with the email

// TODO: Execute payment with credit card and print the result

// TODO: Execute payment with PayPal and print the result

// TODO: Print the method description for the PayPal payment method
?>

All lessons in Object Oriented Programming