예제 #1
0
        public async Task <IActionResult> Callback(Guid paymentId, IFormCollection form)
        {
            if (paymentId == Guid.Empty)
            {
                VerifyGatewayResult failModel = VerifyGatewayResult.Failed("Ödeme bilgisi geçersiz.");
                return(View("Fail", failModel));
            }

            //get transaction by identifier
            PaymentTransaction payment = await _paymentService.GetByOrderNumber(paymentId);

            if (payment == null)
            {
                VerifyGatewayResult failModel = VerifyGatewayResult.Failed("Ödeme bilgisi geçersiz.");
                return(View("Fail", failModel));
            }

            PaymentGatewayRequest bankRequest = JsonConvert.DeserializeObject <PaymentGatewayRequest>(payment.BankRequest);

            if (bankRequest == null)
            {
                VerifyGatewayResult failModel = VerifyGatewayResult.Failed("Ödeme bilgisi geçersiz.");
                return(View("Fail", failModel));
            }

            //create provider
            IPaymentProvider     provider      = _paymentProviderFactory.Create(bankRequest.BankName);
            VerifyGatewayRequest verifyRequest = new VerifyGatewayRequest
            {
                BankName       = bankRequest.BankName,
                BankParameters = bankRequest.BankParameters
            };

            VerifyGatewayResult verifyResult = await provider.VerifyGateway(verifyRequest, bankRequest, form);

            verifyResult.OrderNumber = bankRequest.OrderNumber;

            //save bank response
            payment.BankResponse = JsonConvert.SerializeObject(new
            {
                verifyResult,
                parameters = form.Keys.ToDictionary(key => key, value => form[value].ToString())
            });

            payment.TransactionNumber = verifyResult.TransactionId;
            payment.ReferenceNumber   = verifyResult.ReferenceNumber;
            payment.BankResponse      = verifyResult.Message;

            if (verifyResult.Installment > 1)
            {
                payment.Installment = verifyResult.Installment;
            }

            if (verifyResult.ExtraInstallment > 1)
            {
                payment.ExtraInstallment = verifyResult.ExtraInstallment;
            }

            if (verifyResult.Success)
            {
                //mark as paid
                payment.MarkAsPaid(DateTime.Now);
                await _paymentService.Update(payment);

                return(View("Success", verifyResult));
            }

            //mark as not failed(it's mean error)
            payment.MarkAsFailed(verifyResult.ErrorMessage, $"{verifyResult.Message} - {verifyResult.ErrorCode}");

            //update payment transaction
            await _paymentService.Update(payment);

            return(View("Fail", verifyResult));
        }