1Ứng dụng đăng ký sẵn các factory theo mã cổng: Java gom mọi bean PaymentProviderFactory vào map, Go truyền map{"VNPAY": ..., "MOMO": NewMomo(...)} vào NewCheckout. Nghiệp vụ không tự tạo lớp cụ thể nào.The application registers factories by provider key: Java collects every PaymentProviderFactory bean into a map, Go passes map{"VNPAY": ..., "MOMO": NewMomo(...)} to NewCheckout. Business code never constructs a concrete class.
2Khách chọn VNPay cho đơn DH1001 trị giá 250.000 đồng. startPayment("VNPAY", ...) gọi factory("VNPAY") và nhận VnPayFactory, thay vì chọn client và verifier riêng rẽ.The customer chooses VNPay for order DH1001 worth 250,000 VND. startPayment("VNPAY", ...) calls factory("VNPAY") and gets VnPayFactory, rather than choosing a client and verifier independently.
3paymentClient() trả về sản phẩm họ VNPay (một lambda) dùng tmnCode của factory; service gọi createPaymentUrl() qua interface PaymentClient. Sản phẩm VNPay tự nhân số tiền với 100 theo quy ước của cổng.paymentClient() returns a VNPay-family product (a lambda) using the factory's tmnCode; the service calls createPaymentUrl() through the PaymentClient interface. The VNPay product multiplies the amount by 100 per the gateway's convention.
4IPN của VNPay tới handleIpn("VNPAY", params). Ứng dụng phải truyền đúng mã cổng của giao dịch để factory("VNPAY") chọn lại đúng họ; signatureVerifier() trả về bộ kiểm tra SHA512 dùng cùng secret.VNPay's IPN reaches handleIpn("VNPAY", params). The app must pass the transaction's provider so factory("VNPAY") selects the same family again; signatureVerifier() returns the SHA512 verifier using the same secret.
5Service gọi verify() qua interface SignatureVerifier và nhận kết quả. Chữ ký đúng mới là một điều kiện; tầng xử lý thanh toán còn phải kiểm tra đơn, số tiền và trạng thái.The service calls verify() through the SignatureVerifier interface and gets the result. A valid signature is one prerequisite; payment handling must still check the order, amount and status.
6Giờ chạy họ thứ hai bằng code Go: Checkout.Start(ctx, "MOMO", "DH1002", 150000) lấy momoFactory từ map. Checkout vẫn chỉ biết interface ProviderFactory.Now the second family runs in the Go code: Checkout.Start(ctx, "MOMO", "DH1002", 150000) picks momoFactory from the map. Checkout still only knows the ProviderFactory interface.
7momoFactory.Client() trả về momoClient mang partnerCode của factory; CreatePaymentURL() tạo link Momo, không nhân 100 như VNPay. Checkout gọi nó qua interface Client.momoFactory.Client() returns a momoClient carrying the factory's partnerCode; CreatePaymentURL() builds the Momo link, without VNPay's x100. Checkout calls it through the Client interface.
8Khi IPN Momo tới HandleIPN("MOMO", params), momoFactory.Verifier() trả về momoVerifier dùng HMAC-SHA256 với cùng secret. Verify() sai thì HandleIPN trả error "MOMO: invalid signature".When Momo's IPN reaches HandleIPN("MOMO", params), momoFactory.Verifier() returns a momoVerifier using HMAC-SHA256 with the same secret. If Verify() fails, HandleIPN returns the error "MOMO: invalid signature".
9Tổng kết: mỗi factory chỉ sinh sản phẩm cùng họ, nên client VNPay luôn đi với verifier SHA512 còn momoClient luôn đi với momoVerifier HMAC-SHA256. Thêm cổng mới chỉ cần thêm một factory.Summary: each factory only produces products of its own family, so the VNPay client always pairs with the SHA512 verifier and momoClient with the HMAC-SHA256 momoVerifier. A new gateway only needs one more factory.