mercredi 25 avril 2018

Best way(pattern) implement payment process by many types of payment in android

I have many types of payment in android app:

1. By barcode
2. By Nfc
3. By magnetic
4. By code
5. By somthing
........

I use Stratage pattern for this. I have Interface:

    public interface PaymentStrategy<T> {
    T pay(BigDecimal amount);
         }

I implement each type of payment. For example Barcode:

public class PaymentByBarcode implements PaymentStrategy<PaymentResponse> {

    private String barcode;
    private String userLogin;

    public PaymentByBarcode(String barcode, String userLogin) {
        this.barcode = barcode;
        this.userLogin = userLogin;
    }

    @Override
    public PaymentResponse pay(BigDecimal amount) {
        PaymentRequest paymentRequest = PaymentRequest
            .builder()
            .barcode(barcode)
            .userLogin(userLogin)
            .paymentType(PaymentType.BARCODE)
            .amount(amount)
            .build();
        PaymentResponse paymentResponse = makePaymentOnServer(paymentRequest);
        return paymentResponse;
    }
}

By person number:

public class PaymentByPersonNumber implements PaymentStrategy<PaymentResponse> {

    private String barcode;
    private String personNumber;

    public PaymentByPersonNumber(String barcode, String personNumber) {
        this.barcode = barcode;
        this.personNumber = personNumber;
    }

    @Override
    public PaymentResponse pay(BigDecimal amount) {
        PaymentRequest paymentRequest = PaymentRequest
            .builder()
            .personNumber(personNumber)
            .amount(amount)
            .paymentType(PaymentType.PERSON_NUMBER)
            .build();
        PaymentResponse paymentResponse = makePaymentOnServer(paymentRequest);
        return paymentResponse;
    }
}

And I have executor:

public class PaymentExcutor<T> {
    private PaymentStrategy<T> paymentStrategy;

    public PaymentExcutor(PaymentStrategy<T> paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    T ececutePayment(BigDecimal amount){
        return paymentStrategy.pay(amount);
    }
}

And for each tipy of payment I create concrete PaymentStrategy, set it to executor and try make payment:

PaymentStrategy paymentByBarcode = new PaymentByBarcode("12345", "test");
            PaymentExcutor<PaymentResponse> paymentExcutor = new PaymentExcutor<>(paymentByBarcode);
            PaymentResponse paymentResponse = paymentExcutor.ececutePayment(new BigDecimal(100));

This pattern work fine. I can add new type of payment easy and not rewrite code. But now I have some problem. In some cases I need confirm payment. Server send response wait confirmation of payment. On android side I need set one field (isConfirm = true) and send this paymentRequest(with isConfirm = true) to server. Bue=t I do not understend how can I doit with this pattern. My questions:

Can you say how similar tasks are implemented? Can there be an example? code on githab? article?

Aucun commentaire:

Enregistrer un commentaire