Beispiel #1
0
        /// <summary>
        /// verify px payment match transaction
        /// if pay success set transaction as paid
        /// </summary>
        /// <param name="resultId"></param>
        /// <param name="isSuccess"></param>
        /// <returns></returns>
        public static bool VerifyPxPayPayment(string resultId, bool isSuccess, out int outPaymentId)
        {
            outPaymentId = 0;
            try
            {
                //check response
                StallApplication.BizInfoFormat("[ACCOUNTANT-PXPAY]start to get pxpay payment result={0}", resultId);
                ResponseOutput output = _pxPay.ProcessResponse(resultId);
                if (output == null)
                {
                    StallApplication.BizError("[ACCOUNTANT-PXPAY]can not get pxpay payment result - resposne is null");
                    return(false);
                }
                if (!(isSuccess ? "1" : "0").Equals(output.Success))
                {
                    StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]payment result not match except {0} - actual {1}", output.Success, isSuccess);
                    StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]{0}", output);
                    return(false);
                }

                //set payment
                int     paymentId = int.Parse(output.TxnId.Split('-')[0]);
                decimal amount    = decimal.Parse(output.AmountSettlement);
                using (StallEntities db = new StallEntities())
                {
                    Payment payment = db.Payments.FirstOrDefault(o => o.Id == paymentId);
                    if (payment == null)
                    {
                        StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]payment {0} can not mactch payment {1}", resultId, outPaymentId);
                        StallApplication.BizErrorFormat("[ACCOUNTANT-PXPAY]{0}", output);
                        return(false);
                    }

                    payment.ResponseTime = DateTime.Now;
                    outPaymentId         = paymentId;
                    bool result = true;
                    if (!isSuccess)
                    {
                        //pay fail
                        StallApplication.BizErrorFormat("[ACCOUNT-PAPAY]pay failed payment id={0}", payment.Id);
                    }
                    else
                    {
                        //pay success
                        if (payment.Amount != amount)
                        {
                            StallApplication.BizErrorFormat("[ACCOUNT-PAPAY]pxpay amount {0} <> transaction amount {1}", amount, payment.Amount);
                            result = false;
                        }
                        else
                        {
                            payment.HasPaid = true;
                        }
                    }
                    payment.PxPayResponse = output.ToString();
                    db.SaveChanges();
                    return(result);
                }
            }
            catch (Exception ex)
            {
                StallApplication.SysError("can not get pxpay payment result", ex);
                return(false);
            }
        }