Beispiel #1
0
        public void CreateChargeTest()
        {
            var   pingpp = new Pingpp(KEY);
            Error error;
            var   charge = pingpp.CreateCharge(new ChargeCreateParam()
            {
                OrderNo = Guid.NewGuid().ToString().Replace("-", ""),
                App     = new Dictionary <string, string>()
                {
                    { "id", APPID }
                },
                Channel  = "alipay",
                Amount   = 10000000,
                ClientIp = "127.0.0.1",
                Currency = "cny",
                Subject  = "测试 " + DateTime.Now.ToString(),
                Body     = "测试",
                Metadata = new Dictionary <string, string>()
                {
                    { "test", "test metadata" }
                },
            }, out error);

            Assert.IsNotNull(charge);
            Assert.IsNull(error);
        }
Beispiel #2
0
 public void ListChargeTest()
 {
     var pingpp = new Pingpp(KEY);
     Error error;
     var charges = pingpp.ListCharge(new ChargeListParam(), out error);
     Assert.IsNotNull(charges);
     Assert.IsNull(error);
 }
Beispiel #3
0
        public void ListChargeTest()
        {
            var   pingpp = new Pingpp(KEY);
            Error error;
            var   charges = pingpp.ListCharge(new ChargeListParam(), out error);

            Assert.IsNotNull(charges);
            Assert.IsNull(error);
        }
Beispiel #4
0
 public void ListRefundTest()
 {
     var pingpp = new Pingpp(KEY);
     Error error;
     var refunds = pingpp.ListRefund(new RefundListParam()
     {
         Id = "ch_O888e1ynnTS4anPejL8KurDC", // 退款的支付账单 ID
     }, out error);
     Assert.IsNotNull(refunds);
     Assert.IsNull(error);
 }
Beispiel #5
0
 public void RetrieveChargeTest()
 {
     var pingpp = new Pingpp(KEY);
     Error error;
     var charge = pingpp.RetrieveCharge(new ChargeRetrieveParam()
     {
         Id = "ch_Hy1i189SG408HiHmb1L4qXH4"
     }, out error);
     Assert.IsNotNull(charge);
     Assert.IsNull(error);
 }
Beispiel #6
0
        public void RetrieveChargeTest()
        {
            var   pingpp = new Pingpp(KEY);
            Error error;
            var   charge = pingpp.RetrieveCharge(new ChargeRetrieveParam()
            {
                Id = "ch_Hy1i189SG408HiHmb1L4qXH4"
            }, out error);

            Assert.IsNotNull(charge);
            Assert.IsNull(error);
        }
Beispiel #7
0
 public void RetrieveRefundTest()
 {
     var pingpp = new Pingpp(KEY);
     Error error;
     var refund = pingpp.RetrieveRefund(new RefundRetrieveParam()
     {
         Id = "re_iXjf9GLuPKWTm58an5vfn1e5", // 退款 ID
         Charge = "ch_O888e1ynnTS4anPejL8KurDC" // 退款的支付账单 ID
     }, out error);
     Assert.IsNotNull(refund);
     Assert.IsNull(error);
 }
Beispiel #8
0
        public void ListRefundTest()
        {
            var   pingpp = new Pingpp(KEY);
            Error error;
            var   refunds = pingpp.ListRefund(new RefundListParam()
            {
                Id = "ch_O888e1ynnTS4anPejL8KurDC", // 退款的支付账单 ID
            }, out error);

            Assert.IsNotNull(refunds);
            Assert.IsNull(error);
        }
Beispiel #9
0
 public void CreateRefundTest()
 {
     var pingpp = new Pingpp(KEY);
     Error error;
     var refund = pingpp.CreateRefund(new RefundCreateParam()
     {
         Id = "ch_O888e1ynnTS4anPejL8KurDC", // 退款的支付账单 ID
         Amount = 1,
         Description = "没啥可说的",
     }, out error);
     Assert.IsNotNull(refund);
     Assert.IsNull(error);
 }
Beispiel #10
0
        public void RetrieveRefundTest()
        {
            var   pingpp = new Pingpp(KEY);
            Error error;
            var   refund = pingpp.RetrieveRefund(new RefundRetrieveParam()
            {
                Id     = "re_iXjf9GLuPKWTm58an5vfn1e5", // 退款 ID
                Charge = "ch_O888e1ynnTS4anPejL8KurDC"  // 退款的支付账单 ID
            }, out error);

            Assert.IsNotNull(refund);
            Assert.IsNull(error);
        }
Beispiel #11
0
        public void CreateRefundTest()
        {
            var   pingpp = new Pingpp(KEY);
            Error error;
            var   refund = pingpp.CreateRefund(new RefundCreateParam()
            {
                Id          = "ch_O888e1ynnTS4anPejL8KurDC", // 退款的支付账单 ID
                Amount      = 1,
                Description = "没啥可说的",
            }, out error);

            Assert.IsNotNull(refund);
            Assert.IsNull(error);
        }
Beispiel #12
0
 public void CreateChargeTest()
 {
     var pingpp = new Pingpp(KEY);
     Error error;
     var charge = pingpp.CreateCharge(new ChargeCreateParam()
     {
         OrderNo = Guid.NewGuid().ToString().Replace("-",""),
         App = new Dictionary<string, string>() { { "id", APPID } },
         Channel = "alipay",
         Amount = 10000000,
         ClientIp = "127.0.0.1",
         Currency = "cny",
         Subject = "测试 " + DateTime.Now.ToString(),
         Body = "测试",
         Metadata = new Dictionary<string, string>() { { "test", "test metadata" } },
     }, out error);
     Assert.IsNotNull(charge);
     Assert.IsNull(error);
 }
        public IHttpActionResult Charge([FromBody] ChargeInfo info)
        {
            Logger.Info($"AppServer请求读取Data\nAppId={info.AppId}\nAccountId={info.AccountId}");

            try
            {
                Pingpp.SetApiKey("sk_test_zXnD8KKOyfn1vDuj9SG8ibfT");

                var    appId          = "app_H4yDu5COi1O4SWvz";
                var    r              = new Random();
                string orderNoPostfix = r.Next(0, 1000000).ToString("D6");
                string orderNo        = $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}_{orderNoPostfix}";
                var    amount         = info.Amount;
                var    channel        = "alipay";
                var    currency       = "cny";

                var param = new Dictionary <string, object>
                {
                    { "livemode", false },
                    { "order_no", orderNo },
                    { "amount", amount },
                    { "channel", channel },
                    { "currency", currency },
                    { "subject", info.Subject },
                    { "body", info.Body },
                    { "description", info.Description },
                    { "client_ip", info.ClientIp },
                    { "app", new Dictionary <string, string> {
                          { "id", appId }
                      } }
                };

                var charge = pingpp.Models.Charge.create(param);

                return(CreateSuccessResult(charge));
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "创建Charge失败");
                throw new UCenterException(UCenterErrorCode.PaymentCreateChargeFailed, ex.Message);
            }
        }
Beispiel #14
0
        /// <summary>
        /// 获取支付凭据
        /// </summary>
        /// <param name="transactionId">交易Id</param>
        /// <param name="paymentChannel">支付渠道</param>
        /// <param name="userAgent">客户端的UserAgent</param>
        /// <param name="ipAddress">客户端的Ip地址</param>
        /// <param name="charge">返回的支付凭据</param>
        /// <param name="realResultUrl">返回的结果Url</param
        /// <param name="waitResultUrl">等待返回的结果Url</param>>
        public virtual void GetCharge(
            Guid transactionId, string paymentChannel, string userAgent, string ipAddress,
            out Charge charge, out string realResultUrl, out string waitResultUrl)
        {
            var transactionManager = Application.Ioc.Resolve <PaymentTransactionManager>();
            PaymentTransaction transaction;
            PaymentApi         api;
            ApiData            apiData;

            using (UnitOfWork.Scope()) {
                // 获取交易和支付接口
                transaction = transactionManager.Get(transactionId);
                api         = transaction == null ? null : transaction.Api;
                // 检查交易和接口是否存在
                if (transaction == null)
                {
                    throw new BadRequestException(new T("Payment transaction not found"));
                }
                else if (api == null)
                {
                    throw new BadRequestException(new T("Payment api not exist"));
                }
                // 检查当前登录用户是否可以支付
                var result = transaction.Check(c => c.IsPayerLoggedIn);
                if (!result.First)
                {
                    throw new BadRequestException(result.Second);
                }
                result = transaction.Check(c => c.IsPayable);
                if (!result.First)
                {
                    throw new BadRequestException(result.Second);
                }
                apiData = transaction.Api.ExtraData.GetOrDefault <ApiData>("ApiData") ?? new ApiData();
            }
            // 检查货币,虽然Ping++有提供货币参数但是支付渠道里面都只能支付人民币
            if (transaction.CurrencyType != "CNY")
            {
                throw new BadRequestException(new T("Ping++ only support cny payment"));
            }
            // 获取Ping++的支付凭据
            // 注意这里非线程安全,如果添加多个Ping++接口可能会出问题
            Pingpp.SetApiKey(apiData.TradeSecretKey);
            Pingpp.SetPrivateKey(apiData.PartnerRsaPrivateKey);
            var configManager   = Application.Ioc.Resolve <GenericConfigManager>();
            var websiteSettings = configManager.GetData <WebsiteSettings>();
            var extra           = new Dictionary <string, object>();

            realResultUrl = PaymentUtils.GetReturnOrNotifyUrl(
                apiData.ReturnDomain,
                transactionManager.GetResultUrl(transactionId));
            waitResultUrl = PaymentUtils.GetReturnOrNotifyUrl(
                apiData.ReturnDomain,
                GetWaitResultUrl(transactionId));
            if (paymentChannel == "alipay_wap" ||
                paymentChannel == "alipay_pc_direct")
            {
                // 支付宝支付
                extra["success_url"] = waitResultUrl;
            }
            else if (paymentChannel == "bfb_wap")
            {
                // 百度钱包支付
                extra["result_url"] = waitResultUrl;
                extra["bfb_login"]  = apiData.BfbRequireLogin;
            }
            else if (paymentChannel == "upacp_wap" ||
                     paymentChannel == "upacp_pc")
            {
                // 银联支付
                extra["result_url"] = waitResultUrl;
            }
            else if (paymentChannel == "wx" ||
                     paymentChannel == "wx_pub" ||
                     paymentChannel == "wx_pub_qr" ||
                     paymentChannel == "wx_wap")
            {
                // 微信支付
                if (apiData.WeChatNoCredit)
                {
                    extra["limit_pay"] = "no_credit";
                }
                if (paymentChannel == "wx_pub")
                {
                    extra["open_id"] = apiData.WeChatOpenId;
                }
                else if (paymentChannel == "wx_pub_qr")
                {
                    extra["product_id"] = transaction.ExtraData
                                          .GetOrDefault <string>("WeChatProductId") ?? "0";
                }
                else if (paymentChannel == "wx_wap")
                {
                    extra["result_url"] = waitResultUrl;
                }
            }
            else if (paymentChannel == "jdpay_wap")
            {
                // 京东支付
                extra["success_url"] = waitResultUrl;
                extra["fail_url"]    = realResultUrl;
            }
            else if (paymentChannel == "fqlpay_wap")
            {
                // 分期乐支付
                extra["c_merch_id"] = apiData.FqlChildMerchantId;
                extra["return_url"] = waitResultUrl;
            }
            else if (paymentChannel == "qgbc_wap")
            {
                // 量化派支付
                extra["phone"] = transaction.ExtraData
                                 .GetOrDefault <string>("QgbcBuyerMobile") ?? "0";
                extra["return_url"] = waitResultUrl;
            }
            else if (paymentChannel == "qpay")
            {
                // QQ钱包
                var isIOS = userAgent.Contains("iphone") || userAgent.Contains("ipad");
                extra["device"] = isIOS ? "ios" : "android";
            }
            var chargeParams = new Dictionary <string, object>
            {
                { "order_no", transaction.Serial },
                { "amount", checked ((int)(transaction.Amount * 100)) },
                { "channel", paymentChannel },
                { "currency", "cny" },
                { "subject", $"{new T(websiteSettings.WebsiteName)} {transaction.Serial}" },
                { "body", transaction.Description },
                { "client_ip", ipAddress },
                { "app", new Dictionary <string, string> {
                      { "id", apiData.PingppAppId }
                  } },
                { "extra", extra }
            };

            charge = Charge.Create(chargeParams);
        }
Beispiel #15
0
        public async Task <IHttpActionResult> CreateCharge([FromBody] ChargeInfo info, CancellationToken token)
        {
            CustomTrace.TraceInformation($"AppServer.CreateCharge\nAppId={info.AppId}\nAccountId={info.AccountId}");

            try
            {
                var account = await this.Database.Accounts.GetSingleAsync(info.AccountId, token);

                var app = await this.Database.Apps.GetSingleAsync(info.AppId, token);

                var orderEntity = new OrderEntity
                {
                    Id          = Guid.NewGuid().ToString(),
                    AppId       = info.AppId,
                    AppName     = app == null ? null : app.Name,
                    AccountId   = info.AccountId,
                    AccountName = account == null ? null : account.AccountName,
                    State       = OrderState.Created,
                    CreatedTime = DateTime.UtcNow
                };

                await this.Database.Orders.InsertAsync(orderEntity, token);

                // TODO: Replace with live key
                Pingpp.SetApiKey("sk_test_zXnD8KKOyfn1vDuj9SG8ibfT");

                // TODO: Fix hard code path
                var certFile = this.GetCertFilePath("rsa_private_key.pem");
                Pingpp.SetPrivateKeyPath(certFile);

                var    appId          = "app_H4yDu5COi1O4SWvz";
                var    r              = new Random();
                string orderNoPostfix = r.Next(0, 1000000).ToString("D6");
                string orderNo        = $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}_{orderNoPostfix}";
                var    amount         = info.Amount;

                // var channel = "alipay";
                // var currency = "cny";
                // 交易请求参数,这里只列出必填参数,可选参数请参考 https://pingxx.com/document/api#api-c-new
                var chargeParams = new Dictionary <string, object>
                {
                    { "order_no", new Random().Next(1, 999999999) },
                    { "amount", amount },
                    { "channel", "wx" },
                    { "currency", "cny" },
                    { "subject", info.Subject },
                    { "body", info.Body },
                    { "client_ip", "127.0.0.1" },
                    { "app", new Dictionary <string, string> {
                          { "id", appId }
                      } }
                };

                var charge = Charge.Create(chargeParams);

                return(this.CreateSuccessResult(charge));
            }
            catch (Exception ex)
            {
                CustomTrace.TraceError(ex, "Failed to create charge");
                throw new UCenterException(UCenterErrorCode.PaymentCreateChargeFailed, ex.Message);
            }
        }