Exemple #1
0
        private async Task <PaypalAccessTokenResponse> GetPaypalAccessToken()
        {
            var clientID = _configuration.GetValue <string>("Paypal:ClientID");
            var secret   = _configuration.GetValue <string>("Paypal:Secret");
            PaypalAccessTokenResponse token = new PaypalAccessTokenResponse();

            var request = new HttpRequestMessage();
            Dictionary <string, string> getTokenAuthData = new Dictionary <string, string>();

            getTokenAuthData.Add("Authorization", $"Basic {EncryptionHelper.GetBasicAuthorizationString(clientID, secret)}");
            getTokenAuthData.Add("Accept", $"application/json");
            getTokenAuthData.Add("Accept-Language", $"en_US");

            FormUrlEncodedContent content = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("grant_type", "client_credentials")
            });
            var uri = new Uri("https://api.sandbox.paypal.com/v1/oauth2/token");
            HttpRequestMessage httpRequestMessage = new HttpRequestMessage();

            foreach (KeyValuePair <string, string> keyValuePair in getTokenAuthData ?? new Dictionary <string, string>())
            {
                httpRequestMessage.Headers.Add(keyValuePair.Key, keyValuePair.Value);
            }

            httpRequestMessage.Content    = content;
            httpRequestMessage.Method     = HttpMethod.Post;
            httpRequestMessage.RequestUri = uri;
            HttpClient httpClient = new HttpClient();

            var response = await httpClient.SendAsync(httpRequestMessage);

            string jsonString = await response.Content.ReadAsStringAsync();

            token = JsonConvert.DeserializeObject <PaypalAccessTokenResponse>(jsonString);

            return(token);
        }
Exemple #2
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);
        }