Beispiel #1
0
        private async Task <bool> UpdatePaypalRecordSuccess(ApiResponse response, PaypalRecordDto paypalRecordDto)
        {
            var          data         = _myDbContext.PaypalRecord.Where(s => s.paypalOrderId == paypalRecordDto.paypalRecordPayload.paypalOrderId);
            PaypalRecord paypalRecord = data.FirstOrDefault();

            try
            {
                if (paypalRecord != null)
                {
                    paypalRecord.apiTransactionNumber = paypalRecordDto.paypalRecordPayload.apiTransactionNumber;
                    paypalRecord.apiTransactionId     = paypalRecordDto.paypalRecordPayload.apiTransactionId;
                    _myDbContext.Entry(paypalRecord).CurrentValues.SetValues(paypalRecord);
                    _myDbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                var aaa = ex.ToString();
            }

            if (paypalRecord == null)
            {
                paypalRecordDto.resultMessage = $"Failed to update paypal payment record. {suggestedAction}";
                response.meta.error.Add("message", paypalRecordDto.resultMessage);
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        private bool AddPaypalRecordSuccess(ApiResponse response, PaypalRecordDto paypalRecordDto)
        {
            PaypalRecord paypalRecord = _addPaypalRecordMapper.AddPaypalRecord(paypalRecordDto.paypalRecordPayload);

            var data = _myDbContext.PaypalRecord.Where(s => s.paypalRecordId == paypalRecord.paypalRecordId);

            try
            {
                if (data.Count() > 0)
                {
                    _myDbContext.Entry(data).CurrentValues.SetValues(paypalRecord);
                    _myDbContext.SaveChanges();
                }
                else
                {
                    _myDbContext.Add(paypalRecord);
                    _myDbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                var aa = ex.ToString();
            }
            if (paypalRecord == null)
            {
                paypalRecordDto.resultMessage = $"Failed to add paypal payment record. {suggestedAction}";
                response.meta.error.Add("message", paypalRecordDto.resultMessage);
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        private bool AddApiTransactionSuccess(ApiResponse response, PaypalRecordDto paypalRecordDto)
        {
            if (!paypalRecordDto.resultMessage.IsNullOrEmpty())
            {
                response.meta.error.Add("message", $"{paypalRecordDto.resultMessage} {suggestedAction}");
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        private bool PaypalPaymentCurrencyIsUsd(ApiResponse response, PaypalRecordDto paypalRecordDto)
        {
            if (!paypalRecordDto.paypalRecordPayload.currencyCode.Equals(expectedCurrencyCode))
            {
                response.meta.error.Add("message",
                                        $"Payment captured is not in currency: {expectedCurrencyCode.ToUpper()}. " +
                                        $"Top up failed. {suggestedAction}");
                return(false);
            }

            return(true);
        }
Beispiel #5
0
        public async Task <PaypalRecordDto> TopUpApiBalance(string userSessionapiKey,
                                                            PaypalRecordDto paypalRecordDto)
        {
            AddApiTransactionRequest addApiTransactionRequest = new AddApiTransactionRequest
            {
                amount       = paypalRecordDto.paypalRecordPayload.amount,
                currencyCode = paypalRecordDto.paypalRecordPayload.currencyCode,
                userApiKey   = userSessionapiKey,
                notes        = $"Paypal Order ID: {paypalRecordDto.paypalRecordPayload.paypalOrderId}; " +
                               $"Paypal Capture ID: {paypalRecordDto.paypalRecordPayload.paypalCaptureId}"
            };

            return(await AddTopUpApiTransactionViaAdminApi(paypalRecordDto, addApiTransactionRequest));
        }
Beispiel #6
0
        private bool CapturePaypalPaymentSuccess(ApiResponse response, PaypalRecordDto paypalRecordDto)
        {
            if (!paypalRecordDto.resultMessage.IsNullOrEmpty())
            {
                response.meta.error.Add("message", paypalRecordDto.resultMessage);
                return(false);
            }

            if (!PaypalPaymentCurrencyIsUsd(response, paypalRecordDto))
            {
                return(false);
            }

            return(true);
        }
Beispiel #7
0
        private async Task <PaypalRecordDto> AddTopUpApiTransactionViaAdminApi(PaypalRecordDto paypalRecordDto,
                                                                               AddApiTransactionRequest addApiTransactionRequest)
        {
            AddApiTransactionResponse addApiTransactionResponse = new AddApiTransactionResponse();

            try
            {
                addApiTransactionRequest.transactionType = topUpTransactionType;
                var api = this._myDbContext.Apis.Where(s => s.apiKey.ToString() == addApiTransactionRequest.userApiKey).FirstOrDefault();
                addApiTransactionRequest.apiId    = api.apiId;
                addApiTransactionRequest.createBy = api.apiId.ToString();
                ApiTransaction request = _addApiTransactionMapper.CreateApiTransaction(addApiTransactionRequest);

                string query =
                    "call P_AddApiTransaction(@apiId,@headId,@currencyCode,@transactionType,@amount,@notes,@createBy)";
                MySqlParameter pApiId           = new MySqlParameter("@apiId", request.apiId);
                MySqlParameter pHeadId          = new MySqlParameter("@headId", request.headId);
                MySqlParameter pCurrencyCode    = new MySqlParameter("@currencyCode", request.currencyCode);
                MySqlParameter pTransactionType = new MySqlParameter("@transactionType", request.transactionType);
                MySqlParameter pAmount          = new MySqlParameter("@amount", request.amount);
                MySqlParameter pNotes           = new MySqlParameter("@notes", request.notes);
                MySqlParameter pCreateBy        = new MySqlParameter("@createBy", request.createBy);

                var data = this._myDbContext.ApiTransactions.FromSqlRaw(query, pApiId, pHeadId, pCurrencyCode,
                                                                        pTransactionType,
                                                                        pAmount, pNotes, pCreateBy).AsNoTracking().AsEnumerable().ToList().FirstOrDefault();

                addApiTransactionResponse.apiTransactionId  = data.apiTransactionId;
                addApiTransactionResponse.transactionNumber = data.transactionNumber;
            }
            catch (Exception ex)
            {
                var aa = ex.ToString();
                addApiTransactionResponse = JsonConvert.DeserializeObject <AddApiTransactionResponse>(ex.Message) ??
                                            ErrorAddApiTransactionResponse();
                paypalRecordDto.resultMessage = addApiTransactionResponse.meta.error["message"];
            }

            if (paypalRecordDto.resultMessage.IsNullOrEmpty())
            {
                paypalRecordDto.paypalRecordPayload.apiTransactionNumber = addApiTransactionResponse.transactionNumber;
                paypalRecordDto.paypalRecordPayload.apiTransactionId     = addApiTransactionResponse.apiTransactionId;
            }

            return(paypalRecordDto);
        }
Beispiel #8
0
        public async Task <ActionResult <object> > paypal(PaypalOrderParamterModel par)
        {
            _myDbContext.Add(par);
            _myDbContext.SaveChanges();

            //取TOKEN
            var token = await GetPaypalAccessToken();

            PaypalRecordPayload paypalTransaction = new PaypalRecordPayload {
                paypalOrderId = par.orderID
            };

            dto = new PaypalRecordDto {
                paypalRecordPayload = paypalTransaction
            };
            PaypalAccessTokenResponse paypalAccessTokenResponse = new PaypalAccessTokenResponse();

            paypalAccessTokenResponse     = token;
            dto.paypalAccessTokenResponse = paypalAccessTokenResponse;
            //取ORDER資訊
            PaypalCapturePaymentResponse paypalCapturePaymentResponse = new PaypalCapturePaymentResponse();

            paypalCapturePaymentResponse = await CapturePaymentForOrder(par.orderID, token.accessToken);

            dto.paypalCapturePaymentResponse = paypalCapturePaymentResponse;
            if (paypalCapturePaymentResponse.name.IsNullOrEmpty())
            {
                GetCaptureResult(paypalCapturePaymentResponse);
            }

            ApiResponse response = new ApiResponse()
            {
                meta = new ApiResponseMeta()
            };

            if (CapturePaypalPaymentSuccess(response, dto))
            {
                //給公司代號
                dto.paypalRecordPayload.companyId = 1;
                //把資料存入PaypalRecord
                if (AddPaypalRecordSuccess(response, dto))
                {
                    try
                    {
                        var             apikey = _myDbContext.Apis.FirstOrDefault();
                        PaypalRecordDto dto2   = await TopUpApiBalance(apikey.apiKey.ToString(), dto);

                        if (AddApiTransactionSuccess(response, dto2))
                        {
                            if (await UpdatePaypalRecordSuccess(response, dto2))
                            {
                                return(dto);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        var aa = ex.ToString();
                    }
                }
            }

            return(dto);
        }