in src/Modules/SimplCommerce.Module.PaymentPaypalExpress/Areas/PaymentPaypalExpress/Controllers/PaypalExpressController.cs [56:115]
public async Task<ActionResult> CreatePayment()
{
var hostingDomain = Request.Host.Value;
var accessToken = await GetAccessToken();
var currentUser = await _workContext.GetCurrentUser();
var cart = await _cartService.GetActiveCartDetails(currentUser.Id);
if(cart == null)
{
return NotFound();
}
var regionInfo = new RegionInfo(_currencyService.CurrencyCulture.LCID);
var experienceProfileId = await CreateExperienceProfile(accessToken);
var httpClient = _httpClientFactory.CreateClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var paypalAcceptedNumericFormatCulture = CultureInfo.CreateSpecificCulture("en-US");
var paymentCreateRequest = new PaymentCreateRequest
{
experience_profile_id = experienceProfileId,
intent = "sale",
payer = new Payer
{
payment_method = "paypal"
},
transactions = new Transaction[]
{
new Transaction {
amount = new Amount
{
total = (cart.OrderTotal + CalculatePaymentFee(cart.OrderTotal)).ToString("N2", paypalAcceptedNumericFormatCulture),
currency = regionInfo.ISOCurrencySymbol,
details = new Details
{
handling_fee = CalculatePaymentFee(cart.OrderTotal).ToString("N2", paypalAcceptedNumericFormatCulture),
subtotal = cart.SubTotalWithDiscountWithoutTax.ToString("N2", paypalAcceptedNumericFormatCulture),
tax = cart.TaxAmount?.ToString("N2", paypalAcceptedNumericFormatCulture) ?? "0",
shipping = cart.ShippingAmount?.ToString("N2", paypalAcceptedNumericFormatCulture) ?? "0"
}
}
}
},
redirect_urls = new Redirect_Urls
{
cancel_url = $"http://{hostingDomain}/PaypalExpress/Cancel", //Haven't seen it being used anywhere
return_url = $"http://{hostingDomain}/PaypalExpress/Success", //Haven't seen it being used anywhere
}
};
var response = await httpClient.PostJsonAsync($"https://api{_setting.Value.EnvironmentUrlPart}.paypal.com/v1/payments/payment", paymentCreateRequest);
var responseBody = await response.Content.ReadAsStringAsync();
dynamic payment = JObject.Parse(responseBody);
if (response.IsSuccessStatusCode)
{
string paymentId = payment.id;
return Ok(new { PaymentId = paymentId });
}
return BadRequest(responseBody);
}