示例#1
0
        public void TestChargeAmountValidations()
        {
            var charge = new ChargeCreateInfo();

            Assert.False(charge.Valid);
            Assert.Contains(new KeyValuePair <string, string>("Amount", "must be greater than 0"), charge.Errors);
        }
示例#2
0
        public void TestChargeCurrencyValidations()
        {
            var charge = new ChargeCreateInfo();

            charge.Amount = 10000;
            Assert.False(charge.Valid);
            Assert.Contains(new KeyValuePair <string, string>("Currency", "cannot be blank"), charge.Errors);
        }
示例#3
0
        public void TestCreateInvalidChargeAmount()
        {
            var charge = new ChargeCreateInfo();

            charge.Amount      = -1;
            charge.Currency    = "THB";
            charge.Description = "Test charge";
            charge.ReturnUri   = "http://localhost:3000/";
            charge.Capture     = true;
            charge.CardId      = "123";
            Assert.Throws <InvalidChargeException>(delegate
            {
                client.ChargeService.CreateCharge(charge);
            });
        }
示例#4
0
        /// <summary>
        /// Omise 泰国支付
        /// </summary>
        /// <param name="omisePaymentModel">omise支付对象</param>
        /// <returns>处理结果</returns>
        public static ResultModel OmisePayment(OmisePaymentService.OmisePaymentModel omisePaymentModel)
        {
            Logger.Error("Omise_Log", string.Format("进入OmisePayment PaymentOrderID:{0}", omisePaymentModel.PaymentOrderId));
            PaymentOrderView requestPaymentOrder = new PaymentOrderView()
            {
                PaymentOrderID = omisePaymentModel.PaymentOrderId,
                UserID         = Convert.ToInt64(omisePaymentModel.UserId)
            };

            //处理结果
            OmisePaymentResultType resultCode = OmisePaymentResultType.Fail;
            ResultModel            result     = _paymentOrderService.GetPaymentOrderBy(requestPaymentOrder);

            if (!result.IsValid || result.Data == null || result.Data.Flag != (int)(OrderEnums.PaymentFlag.NonPaid) || omisePaymentModel.Amount != result.Data.ProductAmount)
            {
                resultCode = OmisePaymentResultType.ParamError;
                Logger.Error("Omise_Log", string.Format("参数异常 PaymentOrderID={0},UserId={1},Amount={2}", requestPaymentOrder.PaymentOrderID, requestPaymentOrder.UserID, omisePaymentModel.Amount));
            }
            else
            {
                requestPaymentOrder = result.Data;
                var chargeInfo = new ChargeCreateInfo();
                chargeInfo.Amount      = (int)(requestPaymentOrder.ProductAmount * 100); //Create a charge with amount 100 THB, here we are passing with the smallest currency unit which is 10000 satangs  交易金额 必须是整数(真实金额*100)。
                chargeInfo.Currency    = "THB";
                chargeInfo.Description = requestPaymentOrder.PaymentOrderID;             //目前这个参数很有可能是我们的订单号或支付单号用这个。
                chargeInfo.Capture     = true;                                           //TRUE means auto capture the charge, FALSE means authorize only. Default is FALSE
                chargeInfo.CardId      = omisePaymentModel.Token;                        //Token generated with Omise.js or Card.js
                //var client = new Omise.Client(YOUR_SECRET_KEY, [YOUR_PUBLIC_KEY]);
                //chargeInfo.ReturnUri = "http://localhost:54741/Home/complete";



                try
                {
                    Client c = new Client(GetConfig.OmiseSecretKey());//new Client("skey_test_50xql6uiv02pemu8z9o");
                    //chargeInfo
                    //c.ChargeService.CreateCharge(chargeInfo);
                    var charge = c.ChargeService.CreateCharge(chargeInfo);
                    //charge.Description
                    //charge.FailureCode
                    //charge.Captured
                    if (charge.Captured && string.IsNullOrEmpty(charge.FailureCode))
                    {
                        var paymentOrder = new PaymentOrderView()
                        {
                            PaymentOrderID = charge.Description,
                            outOrderId     = charge.TransactionId,
                            RealAmount     = charge.Amount / 100.00m
                        };

                        requestPaymentOrder.RealAmount = paymentOrder.RealAmount;

                        if (paymentOrder.RealAmount != requestPaymentOrder.ProductAmount)
                        {
                            Logger.Error("Omise_Log", string.Format("支付成功,但是支付金额小于需要支付的金额 PaymentOrderID={0},outOrderId={1},RealAmount={2}", paymentOrder.PaymentOrderID, paymentOrder.outOrderId, paymentOrder.RealAmount));
                            resultCode = OmisePaymentResultType.LessAmount;
                        }
                        else
                        {
                            //支付成功
                            Logger.Error("Omise_Log", string.Format("支付成功  PaymentOrderID={0},outOrderId={1},RealAmount={2}", paymentOrder.PaymentOrderID, paymentOrder.outOrderId, paymentOrder.RealAmount));

                            ResultModel updateResult = _paymentOrderService.PaymentOrder(paymentOrder);
                            //日志记录
                            if (updateResult.IsValid)
                            {
                                //支付成功,更新数据库成功
                                Logger.Error("Omise_Log", string.Format("支付成功,更新数据库成功 PaymentOrderID={0},outOrderId={1},RealAmount={2}", paymentOrder.PaymentOrderID, paymentOrder.outOrderId, paymentOrder.RealAmount));
                                resultCode = OmisePaymentResultType.Success;
                            }
                            else
                            {
                                //支付成功,更新数据库失败
                                Logger.Error("Omise_Log", string.Format("支付成功,更新数据库失败  PaymentOrderID={0},outOrderId={1},RealAmount={2}", paymentOrder.PaymentOrderID, paymentOrder.outOrderId, paymentOrder.RealAmount));
                                resultCode = OmisePaymentResultType.UpdateDataError;
                            }
                        }
                    }
                    else
                    {
                        resultCode = OmisePaymentResultType.Fail;
                        Logger.Error("Omise_Log", string.Format("支付失败 PaymentOrderID:{0}", requestPaymentOrder.PaymentOrderID));
                    }

                    //var result = c.ChargeService.Capture(charge.Id);
                    //Client.ChargeService.CreateCharge(chargeInfo);
                    //charge.Id //是否是交易ID(这个字段可能性大一些)
                    //charge.TransactionId  是否是交易ID
                    // charge.LiveMode是否是生产环境
                    //charge.FailureCode错误代码
                    //charge.FailureMessage错误信息
                    //var result = c.ChargeService.GetCharge("chrg_test_4xso2s8ivdej29pqnhz");
                }
                catch (Exception ex)
                {
                    resultCode = OmisePaymentResultType.Fail;
                    Logger.Error("Omise_Log", string.Format("支付异常 Error:{0} PaymentOrderID:{1}", ex.Message, requestPaymentOrder.PaymentOrderID));
                }
            }
            ResultModel resultModel = new ResultModel()
            {
                Status = (int)resultCode,
                Data   = requestPaymentOrder
            };

            resultModel.Messages.Add(EnumDescription.GetFieldText(resultCode));
            return(resultModel);
        }
示例#5
0
        public void TestCreateChargeWithCardToken()
        {
            var charge = new ChargeCreateInfo();

            charge.Amount      = 10000;//100 THB,=> 10000 Satangs
            charge.Currency    = "THB";
            charge.Description = "Test charge";
            charge.ReturnUri   = "http://*****:*****@"{
					    'object': 'charge',
					    'id': '123',
					    'livemode': false,
					    'location': '/charges/123',
					    'amount': 10000,
					    'currency': 'thb',
					    'description': 'test charge',
					    'capture': true,
					    'authorized': false,
					    'captured': false,
					    'transaction': null,
					    'return_uri': 'http://www.lvh.me:3000/?payment-result=success',
					    'reference': '123',
					    'authorize_uri': 'http://api.lvh.me:3000/payments/123/authorize',
					    'card': {
					        'object': 'card',
					        'id': '123',
					        'livemode': false,
					        'country': '',
					        'city': null,
					        'postal_code': null,
					        'financing': '',
					        'last_digits': '4242',
					        'brand': 'Visa',
					        'expiration_month': 9,
					        'expiration_year': 2017,
					        'fingerprint': '123',
					        'name': 'test card',
					        'created': '2014-10-02T07:27:30Z'
					    },
					    'customer': null,
					    'ip': null,
					    'created': '2014-10-02T07:30:29Z'
					}"                    );
            var result = client.ChargeService.CreateCharge(charge);

            Assert.AreEqual("123", result.Id);
            Assert.AreEqual(10000, result.Amount);
            Assert.AreEqual("thb", result.Currency);
            Assert.AreEqual(new DateTime(2014, 10, 2, 7, 30, 29), result.CreatedAt);
            Assert.IsNotNull(result.Card);
            Assert.AreEqual("4242", result.Card.LastDigits);
            Assert.AreEqual("test card", result.Card.Name);
            Assert.AreEqual(9, result.Card.ExpirationMonth);
            Assert.AreEqual(2017, result.Card.ExpirationYear);
            Assert.AreEqual("123", result.Card.Fingerprint);
            Assert.IsNull(result.Card.Location);
            Assert.IsNullOrEmpty(result.Card.Country);
            Assert.IsNullOrEmpty(result.Card.City);
            Assert.IsNullOrEmpty(result.Card.PostalCode);
            Assert.IsNullOrEmpty(result.Card.Financing);
            Assert.AreEqual(Brand.Visa, result.Card.Brand);
            Assert.AreEqual(new DateTime(2014, 10, 2, 7, 27, 30), result.Card.CreatedAt);
            Assert.False(result.Card.LiveMode);
            Assert.AreEqual("/charges/123", result.Location);
        }