Esempio n. 1
0
        private void btnOnlinePayment_Click(object sender,
                                            EventArgs e)
        {
            string result = MomoHelper.SendPayOnline(txtPartnerCode_Web.Text,
                                                     txtAccessKey_Web.Text,
                                                     txtSecretKey_Web.Text,
                                                     txtOrderInfo_Web.Text,
                                                     txtReturnUrl_Web.Text,
                                                     txtNotifyUrl_Web.Text,
                                                     txtAmount_Web.Text,
                                                     Guid.NewGuid()
                                                     .ToString(),
                                                     true);

            WebPaymentResult rawResult = JsonConvert.DeserializeObject <WebPaymentResult>(result);

            if (rawResult != null)
            {
                txtResult_Web.Text    = rawResult.RawResponse;
                txtQrCodeUrl_Web.Text = rawResult.QrCodeImg;
                txtPayUrl_Web.Text    = rawResult.PayUrl;

                if (!string.IsNullOrEmpty(rawResult.PayUrl))
                {
                    Process.Start(rawResult.PayUrl);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Thanh toan momo online tich hop tren website
        /// </summary>
        /// <param name="partnerCode">Ma doi tac do momo cung cap</param>
        /// <param name="accessKey">Access key do momo cung cap</param>
        /// <param name="secretKey">Secret key do momo cung cap</param>
        /// <param name="orderInfo">Thong tin don hang</param>
        /// <param name="returnUrl">Url tra ve khi thanh toan thanh cong</param>
        /// <param name="notifyUrl">Url post de nhan thong tin thanh toan thanh cong</param>
        /// <param name="amount">So tien can thanh toan</param>
        /// <param name="orderId">Order id</param>
        /// <param name="test">Test=true la moi truong test, test=false la moi truong production. Mac dinh la false</param>
        /// <returns></returns>
        public static string SendPayOnline(string partnerCode,
                                           string accessKey,
                                           string secretKey,
                                           string orderInfo,
                                           string returnUrl,
                                           string notifyUrl,
                                           string amount,
                                           string orderId,
                                           bool test = false)
        {
            var result = new WebPaymentResult
            {
                RawResponse = string.Empty,
                QrCodeUrl   = string.Empty,
                PayUrl      = string.Empty
            };

            try
            {
                string requestType = "captureMoMoWallet";
                string requestId   = Guid.NewGuid()
                                     .ToString();

                Dictionary <string, string> obj = new Dictionary <string, string>
                {
                    ["partnerCode"] = partnerCode,
                    ["accessKey"]   = accessKey,
                    ["requestId"]   = requestId,
                    ["amount"]      = amount,
                    ["orderId"]     = orderId,
                    ["orderInfo"]   = orderInfo,
                    ["returnUrl"]   = returnUrl,
                    ["notifyUrl"]   = notifyUrl,
                    ["extraData"]   = ""
                };

                var responseFromMomo = TransactionOnline(obj,
                                                         secretKey,
                                                         test,
                                                         requestType);

                result.RawResponse = responseFromMomo;

                if (string.Equals(responseFromMomo,
                                  "The remote server returned an error: (404) Not Found."))
                {
                    result.QrCodeUrl = "Sai thông tin cấu hình endpoint";
                }
                else
                {
                    var response = JsonConvert.DeserializeObject <WebPaymentResponse>(responseFromMomo);

                    if (response != null)
                    {
                        Logger.Debug("Return from MoMo: " + responseFromMomo);

                        if (!string.IsNullOrEmpty(response.QrCodeUrl))
                        {
                            result.QrCodeUrl = response.QrCodeUrl;
                            result.PayUrl    = response.PayUrl;

                            //get qr code image
                            try
                            {
                                WebClient client = new WebClient();
                                client.Headers.Add("user-agent",
                                                   "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
                                string htmlCode    = client.DownloadString(result.PayUrl);
                                string matchString = Regex.Match(htmlCode,
                                                                 "<img class=\"qr-code\".+?src=[\"'](.+?)[\"'].+?>",
                                                                 RegexOptions.IgnoreCase)
                                                     .Groups[1]
                                                     .Value;
                                result.QrCodeImg = matchString;
                            }
                            catch (Exception exception)
                            {
                                Logger.Debug(exception);
                            }
                        }
                        else
                        {
                            result.QrCodeUrl = response.LocalMessage;
                        }
                    }
                    else
                    {
                        var errorResponse = JsonConvert.DeserializeObject <ErrorWebPaymentResponse>(responseFromMomo);
                        if (errorResponse != null)
                        {
                            result.QrCodeUrl = errorResponse.LocalMessage;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Debug(exception);
            }

            return(JsonConvert.SerializeObject(result,
                                               Formatting.None));
        }