Subversion Repositories SmartDukaan

Rev

Rev 6390 | Rev 10269 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3616 chandransh 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.payments.Attribute;
4
import in.shop2020.payments.Payment;
5
import in.shop2020.payments.PaymentStatus;
6
import in.shop2020.thrift.clients.PaymentClient;
7
 
8
import java.util.ArrayList;
9
import java.util.List;
10
 
11
import org.apache.log4j.Logger;
12
 
13
public class HdfcEmiPaymentService implements IPaymentService {
14
    private static Logger log = Logger.getLogger(HdfcEmiPaymentService.class);
15
 
16
    private String redirectURL;
17
 
18
    public HdfcEmiPaymentService() {
19
 
20
    }
21
 
6390 rajveer 22
    public long createPayment(long userId, long txnId, String paymentOption, int gatewayId){
3616 chandransh 23
        log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through HDFC EMI");
24
        CommonPaymentService cps = new CommonPaymentService();
6390 rajveer 25
        if(!cps.createPayment(userId, txnId, gatewayId)){
3616 chandransh 26
            log.error("Error while creating the basic payment");
27
            return PAYMENT_NOT_CREATED;
28
        }else{
8942 rajveer 29
            return initializePayment(cps.getPaymentId(), paymentOption, cps.getAmount());
3616 chandransh 30
        }
31
    }
32
 
8942 rajveer 33
    private long initializePayment(long merchantPaymentId, String paymentOption, double amount){
3616 chandransh 34
        List<Attribute> attributes = new ArrayList<Attribute>();
35
        attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
8942 rajveer 36
        double emiAmount = CommonPaymentService.calculateEmiAmount(paymentOption, amount);
37
        if(emiAmount > 0){
38
        	attributes.add(new Attribute(IPaymentService.EMI_AMOUNT, ""+emiAmount));
39
        }
3616 chandransh 40
        PaymentClient paymentServiceClient = null;
41
        try {
42
            paymentServiceClient = new PaymentClient();
43
        } catch (Exception e) {
44
            log.error("Error while getting payment client", e);
45
            return PAYMENT_NOT_CREATED;
46
        }
47
 
48
        try {
49
            paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
50
            this.redirectURL = paymentServiceClient.getClient().initializeHdfcEmiPayment(merchantPaymentId);
51
            return merchantPaymentId;
52
        }catch (Exception e) {
53
            log.error("Error while initializing payment.", e);
54
            return PAYMENT_NOT_CREATED;
55
        }
56
    }
57
 
58
    public String getRedirectUrl(){
59
        return this.redirectURL;
60
    }
61
 
62
    public static void main(String args[]){
63
        Payment payment = new Payment();
64
        payment.setPaymentId(216);
65
        payment.setAmount(40000);
66
        payment.setGatewayPaymentId("TESTSTSTS");
67
 
68
        // The underlying method calls are no longer valid since all the
69
        // information required to capture a payment is read from the database
70
        // itself and can't be specified through the call.
71
 
72
        //This test checks what happens when the txn id is left blank
73
        //capturePayment(payment, "");                  //Result: !ERROR!-GW00205-Invalid Subsequent Transaction.
74
 
75
        //This test checks what happends with an invalid txn id 
76
        //capturePayment(payment, "6022630101411740");  //Result: !ERROR!-GW00201-Transaction not found.
77
 
78
        //The next three tests require a valid AUTH transaction id.
79
        //This test checks what happens when we attempt to capture an amount greater than what was authorized.
80
        //capturePayment(payment, "9644960021411730");  //Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
81
 
82
        //This test checks what happens when we attempt to capture a valid transaction with the right amount. This transaction should be CAPTURED.
83
        //payment.setAmount(21698);
84
        //capturePayment(payment, "9644960021411730");  //Result: CAPTURED
85
 
86
        //This test tries to capture an already captured payment.
87
        //capturePayment(payment, "9644960021411730");  //Result: !ERROR!-GW00177-Failed Capture Greater Than Auth check.
88
    }
6050 anupam.sin 89
 
3616 chandransh 90
}