//[RequiredLogin]
        public async Task<JsonResult> UpointTelkomselVoucher(UPointTelkomselVoucherViewModel model)
        {
            var api = GoPlayApi.Instance;
            if (!ModelState.IsValid)
            {
                return Json(new { errors = Errors(ModelState) });
            }

            var result = api.UpointAPI(new APIUpointParamModel()
            {
                enumAction = EUpointAction.GetTicketTelkomsel,
            }).Result;
            if (!result.HasData || result.Data.code != "201")
                return FailResult("hrn", !result.HasData ? ErrorCodes.ServerError.ToErrorMessage() : result.Data.info);

            model.hrn = model.hrn.Replace(" ", "");
            var transaction = GenerateCoinTransaction();
            var transactionUUID = Guid.Parse(transaction.order_id);
            transaction.telkom_order_id = result.Data.ticket;
            transaction.payment_method = Helper.GetDescription(PaymentMethod.UPoint_TelkomselVoucher);
            transaction.description = "HRN: " + model.hrn;
            transaction.status = Helper.GetDescription(TransactionStatus.Telkomsel_voucher_pending);
            transaction = api.CreateCoinTransaction(transaction).Data;
            if (transaction == null)
                return FailResult("hrn", ErrorCodes.ServerError.ToErrorMessage());
            string err = null;
            var upointParam = new APIUpointParamModel(transaction, "GToken")
                {
                    enumAction = EUpointAction.TelkomselVoucher,
                    amount = -1,
                    hrn = model.hrn,
                    ticket = result.Data.ticket
                };
            for (int i = 0; i < 5; i++)
            {
                result = api.UpointAPI(upointParam).Result;
                if (!result.HasData)
                {
                    err = ErrorCodes.ServerError.ToErrorCode();
                    break;
                }
                if (result.Data.code == "200")
                {
                    err = null;
                    break;
                }
                err = result.Data.info;
                Thread.Sleep(5000);//5s
                //# For result code of 100 - Voucher burning is still on progress
                //# Retry 5 times max
            }
            if (err != null)
            {
                UpdateTransactionFail(transaction, err);
                return FailResult("hrn", err);
            }
            if (result.Data.trx_id != transactionUUID.ToString("N"))
            {
                var errorMessage = "UPoint incorrect transaction order id: " + result.Data.trx_id;
                UpdateTransactionFail(transaction, errorMessage);

                return FailResult("hrn", errorMessage);
            }
            var voucherValue = result.Data.nominal.HasValue ? result.Data.nominal.Value : 0;
            var gtokenPackage = api.GetGTokenPackage(voucherValue, "IDR").Data;
            if (gtokenPackage == null)
            {
                var errorMessage = "UPoint incorrect transaction amount: " + voucherValue;
                UpdateTransactionFail(transaction, errorMessage);

                return FailResult("hrn", errorMessage);
            }
            var user = api.GetUserById(transaction.customer_account_id).Data;// require login first, user cannot null
            bool debug = false;
            bool.TryParse(ConfigurationManager.AppSettings["UPOINT_DEBUG"], out debug);
            decimal? totalAmount = 0;
            decimal? gtokenAmount = 0;
            if (!debug)
            {
                gtokenAmount = gtokenPackage.GetPlayToken(user);
                totalAmount = gtokenPackage.price;
            }
            transaction.gtoken_package_id = gtokenPackage.id;
            transaction.description += "; GToken Package: " + gtokenPackage.name;
            if (user.HasDiscount())
            {
                transaction.description += "; Extra 10% on GoPlay Token amount.";
                user.is_discount_permanent = true;
                api.UpdateCustomerAccount(user.id, true);
                if (transaction.amount.HasValue)
                    transaction.amount = transaction.amount.Value / decimal.Parse("1.1");
            }
            //# Add view invoice permission for user
            //identity = Identity(transaction.customerAccount.id)
            //identity.provides.add(ItemNeed('action', 'view_invoice', transaction.order_id))

            transaction.status = Helper.GetDescription(TransactionStatus.Success);
            api.UpdateCoinTransaction(transaction.id, transaction.status, transaction.description, transaction.telkom_order_id);
            var invoiceTemplate = new InvoiceViewModel()
            {
                transaction = transaction,
                payer = user,
                package = gtokenPackage,
                topupCard = transaction.topup_card_id.HasValue ? api.GetTopUpCard(transaction.topup_card_id.Value).Data : null
            };
            if (!(await EmailHelper.SendMailInvoice(invoiceTemplate)))
            {
                var errorMessage = "This guy purchased GToken with an improper email " + user.id + " " + user.email;
                return FailResult("hrn", errorMessage);
            }
            return Json(new { correct = true });
        }
        //[RequiredLogin]
        public JsonResult UpointTelkomselVoucher(UPointStandardVoucherViewModel model, string voucherType)
        {
            voucherType = voucherType.ToLower();
            if (voucherType != Helper.GetDescription(EVoucherType.Spin) && voucherType != Helper.GetDescription(EVoucherType.Telkom))
                return FailResult("formError", ErrorCodes.INVALID_VOUCHER_TYPE.ToErrorMessage());

            model.hrn = model.hrn.Replace(" ", "");
            model.vsn = model.vsn.Replace(" ", "");

            var api = GoPlayApi.Instance;
            var transaction = GenerateCoinTransaction();
            var transactionUUID = Guid.Parse(transaction.order_id);

            transaction.payment_method = voucherType == Helper.GetDescription(EVoucherType.Telkom)
                                           ? Helper.GetDescription(PaymentMethod.UPoint_Telkom)
                                           : Helper.GetDescription(PaymentMethod.UPoint_SPIN);

            transaction.description = "HRN: " + model.hrn + "; VSN: " + model.vsn;

            transaction.status = voucherType == Helper.GetDescription(EVoucherType.Telkom)
                                           ? Helper.GetDescription(TransactionStatus.Telkom_voucher_pending)
                                           : Helper.GetDescription(TransactionStatus.Spin_voucher_pending);

            transaction = api.CreateCoinTransaction(transaction).Data;
            if (transaction == null)
                return FailResult("hrn", ErrorCodes.ServerError.ToErrorMessage());
            var upointParam = new APIUpointParamModel(transaction, transactionUUID.ToString("N"))
            {
                enumAction = EUpointAction.StandardVoucher,
                hrn = model.hrn,
                vsn = model.vsn
            };
            if (!string.IsNullOrEmpty(model.phoneNumber))
                upointParam.phone_number = model.phoneNumber;
            var result = api.UpointAPI(upointParam).Result;
            if (!result.HasData)
                return FailResult("hrn", ErrorCodes.ServerError.ToErrorMessage());
            if (!string.IsNullOrEmpty(result.Data.error_code))
            {
                UpdateTransactionFail(transaction, result.Data.error_info);
                return FailResult("hrn", result.Data.error_info);
            }
            if (result.Data.trx_id != transactionUUID.ToString("N"))
            {
                var errorMessage = "UPoint incorrect transaction order id: " + result.Data.trx_id;
                UpdateTransactionFail(transaction, errorMessage);

                return FailResult("hrn", errorMessage);
            }
            var nominal = result.Data.nominal.HasValue ? result.Data.nominal.Value : 0;
            var voucherValue = nominal > result.Data.amount ? nominal : result.Data.amount;

            var gtokenPackage = api.GetGTokenPackage(voucherValue, "IDR").Data;
            if (gtokenPackage == null)
            {
                var errorMessage = "UPoint incorrect transaction amount: " + voucherValue;
                UpdateTransactionFail(transaction, errorMessage);

                return FailResult("hrn", errorMessage);
            }
            var user = api.GetUserById(transaction.customer_account_id).Data;// require login first, user cannot null
            bool debug = false;
            bool.TryParse(ConfigurationManager.AppSettings["UPOINT_DEBUG"], out debug);
            decimal? totalAmount = 0;
            decimal? gtokenAmount = 0;
            if (!debug)
            {
                gtokenAmount = gtokenPackage.GetPlayToken(user);
                totalAmount = gtokenPackage.price;
            }
            transaction.gtoken_package_id = gtokenPackage.id;
            transaction.description += "; GToken Package: " + gtokenPackage.name;
            if (user.HasDiscount())
            {
                transaction.description += "; Extra 10% on GoPlay Token amount.";
                user.is_discount_permanent = true;
                if (transaction.amount.HasValue)
                    transaction.amount = transaction.amount.Value / decimal.Parse("1.1");
                api.UpdateCustomerAccount(user.id, true);
            }
            transaction.telkom_order_id = result.Data.t_id;
            transaction.status = Helper.GetDescription(TransactionStatus.Success);
            api.UpdateCoinTransaction(transaction.id, transaction.status, transaction.description, transaction.telkom_order_id);

            return Json(new { success = true });
        }
Example #3
0
        public async Task<Result<APIUpointModel>> UpointAPI(APIUpointParamModel model)
        {
            using (var client = new HttpClient())
            {
                List<KeyValuePair<string, string>> ValueCollection = new List<KeyValuePair<string, string>>();
                string action = string.Empty;
                if (model.enumAction == EUpointAction.GetTicketTelkomsel)
                {
                    ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_SECRET_TOKEN, model.secret_token));
                    action = ConfigurationManager.AppSettings["UPOINT_TELKOMSEL_VOUCHER_TICKET_ENDPOINT"];
                }
                else
                {
                    ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_SECRET_TOKEN, model.secret_token));
                    ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_TRX_ID, model.trx_id));
                    ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_ITEM, model.item));
                    if (model.amount > -1)
                        ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_AMOUNT, model.amount.ToString()));

                    switch (model.enumAction)
                    {
                        case EUpointAction.BalanceDeduction:
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_PHONE_NUMBER, model.phone_number));
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_CALLBACK_URL, model.callback_url));
                            action = ConfigurationManager.AppSettings["UPOINT_BALANCE_DEDUCTION_ENDPOINT"];
                            break;
                        case EUpointAction.Speedy:
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_PHONE_NUMBER, model.phone_number));
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_SPEEDY_NUMBER, model.speedy_number));
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_IP, model.ip));
                            action = ConfigurationManager.AppSettings["UPOINT_SPEEDY_ENDPOINT"];
                            break;
                        case EUpointAction.TMoney:
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_PHONE_NUMBER, model.phone_number));
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_CALLBACK_URL, model.callback_url));
                            action = ConfigurationManager.AppSettings["UPOINT_TMONEY_ENDPOINT"];
                            break;
                        case EUpointAction.TelkomselVoucher:
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_HRN, model.hrn));
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_TICKET, model.ticket));
                            action = ConfigurationManager.AppSettings["UPOINT_TELKOMSEL_VOUCHER_SUBMIT_ENDPOINT"];
                            break;
                        case EUpointAction.StandardVoucher:
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_HRN, model.hrn));
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_VSN, model.vsn));
                            ValueCollection.Add(new KeyValuePair<string, string>(ConstantValues.S_PHONE_NUMBER, model.phone_number));
                            action = ConfigurationManager.AppSettings["UPOINT_STANDARD_VOUCHER_ENDPOINT"];
                            break;
                        default:
                            break;
                    }
                }
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var formContent = new FormUrlEncodedContent(ValueCollection);
                HttpResponseMessage response = client.PostAsync(action, formContent).Result;

                if (response.IsSuccessStatusCode)
                {
                    var result = JsonConvert.DeserializeObject<APIUpointModel>(await response.Content.ReadAsStringAsync());
                    return Result<APIUpointModel>.Make(result);
                }
                return Result<APIUpointModel>.Null(ErrorCodes.ServerError);
            }
        }