To create a PayPal Business Account, you do not have to own a business or have a business bank account. Having said that, you should put in place practices that separate your personal financial transactions from the transactions from your e-commerce website. Therefore, I suggest using a separate personal bank account to link to your PayPal 'business' account.
- Navigate to www.PayPal.com.
-
Click Sign Up, even if you already have a personal PayPal account. Remember never mix business with pleasure.
- Select the Business Account and click Next.
-
Complete the short questionnaire.
- Select "On my website" for the question "I want to mainly accept payments:".
-
Select an answer to the question "May annual volume is:".
-
Provide an email address and click Continue.
This can be any valid email address, but you may want to use 'info@your e-commerce.com'.
-
Provide a password and click Continue.
A confirmation email will be sent to the address provided.
Update your spreadsheet.
15 | Username | info@... |
16 | Password | |
-
Complete the business contact details page and click Agree and Create Account.
They will require your contact name, a business name, telephone number and business address.
-
Select a business type.
Select from a list of Individual, Sole owner, Partnership, Private company, Public company, Not-for-profit organisation, Government entity, Trust – Investment and Family.
-
Add personal details.
This includes your name, date of birth and address.
-
Click Submit.
You have now set up your PayPal Business Account. You should be presented with a dashboard display.
- Create an ASP.NET Core 3.x MVC application using Visual Studio.
- Go to NuGet package manager and add the following packages:
-
PayPalCheckoutSdk
, package version 1.0.3. I used the latest version at the time of writing.
PayPalCheckoutSdk
is merely a class library. There is no logic contained within the library. The classes are decorated with attributes to aid the serialisation into JSON.
-
PayPalHttp
v1.0.0.
-
Microsoft.AspNetCore.Mvc.NewtonsoftJson
. With the release of ASP.NET Core 3.0, Microsoft broke their implementation of JSON serialisation. Search for "ASP.NET Core: Blank Json {} Return After Upgrading to 3.0" to find out what to add to Startup.ConfigureServices
or choose an option from the next step.
-
Update Startup.ConfigureServices
to call AddNewtonsoftJson
.
services.AddMvc()
.AddNewtonsoftJson();
OR:
services.AddMvc()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
- Create a folder called PayPal in your ASP.NET Core project.
- Create a class
PayPalClient
in the folder, with the code below.
Remember to use the sandbox and live client Ids and secrets to populate the highlighted string content.
using System;
using PayPalCheckoutSdk.Core;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
namespace PayPal
{
public class PayPalClient
{
public static string SandboxClientId { get; set; } =
"<alert>{PayPal SANDBOX Client Id}</alert>";
public static string SandboxClientSecret { get; set; } =
"<alert>{PayPal SANDBOX Client Secret}</alert>";
public static string LiveClientId { get; set; } =
"<alert>{PayPal LIVE Client Id}</alert>";
public static string LiveClientSecret { get; set; } =
"<alert>{PayPal LIVE Client Secret}</alert>";
public static PayPalEnvironment Environment()
{
#if DEBUG
return new SandboxEnvironment(<alert>SandboxClientId</alert>,
<alert>SandboxClientSecret</alert>);
#else
return new LiveEnvironment(<alert>LiveClientId</alert>,
<alert>LiveClientSecret</alert>);
#endif
}
public static PayPalCheckoutSdk.Core.PayPalHttpClient Client()
{
return new PayPalHttpClient(Environment());
}
public static PayPalCheckoutSdk.Core.PayPalHttpClient Client(string refreshToken)
{
return new PayPalHttpClient(Environment(), refreshToken);
}
public static String ObjectToJSONString(Object serializableObject)
{
MemoryStream memoryStream = new MemoryStream();
var writer = JsonReaderWriterFactory.CreateJsonWriter(memoryStream,
Encoding.UTF8,
true,
true,
" ");
var ser = new DataContractJsonSerializer(serializableObject.GetType(),
new DataContractJsonSerializerSettings
{
UseSimpleDictionaryFormat = true
});
ser.WriteObject(writer,
serializableObject);
memoryStream.Position = 0;
StreamReader sr = new StreamReader(memoryStream);
return sr.ReadToEnd();
}
}
}
- Create a class
SmartButtonHttpResponse
in the folder, with code.
using System.Net;
using System.Net.Http.Headers;
namespace PayPal
{
public class SmartButtonHttpResponse
{
readonly PayPalCheckoutSdk.Orders.Order _result;
public SmartButtonHttpResponse(PayPalHttp.HttpResponse httpResponse)
{
Headers = httpResponse.Headers;
StatusCode = httpResponse.StatusCode;
_result = httpResponse.Result<PayPalCheckoutSdk.Orders.Order>();
}
public HttpHeaders Headers { get; }
public HttpStatusCode StatusCode { get; }
public PayPalCheckoutSdk.Orders.Order Result()
{
return _result;
}
public string orderID { get; set; }
}
}
-
Create a class OrderBuilder
in the folder, with code.
using PayPalCheckoutSdk.Orders;
using System.Collections.Generic;
namespace PayPal
{
public static class OrderBuilder
{
public static OrderRequest Build()
{
OrderRequest orderRequest = new OrderRequest();
<alert>
return orderRequest;
}
}
}
-
Create a controller class in the Controllers folder called CheckoutController
. Add the following code:
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using PayPalCheckoutSdk.Orders;
namespace Test.Controllers
{
public class CheckoutController : Controller
{
public IActionResult Index()
{
#if DEBUG
ViewBag.ClientId =
<alert>PayPal.PayPalClient.SandboxClientId</alert>;
#else
ViewBag.ClientId =
<alert>PayPal.PayPalClient.LiveClientId</alert>;
#endif
ViewBag.CurrencyCode = "GBP";
ViewBag.CurrencySign = "£";
return View();
}
[Route("api/paypal/checkout/order/create")]
public async Task<PayPal.SmartButtonHttpResponse> Create()
{
var request = new PayPalCheckoutSdk.Orders.OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(PayPal.OrderBuilder.Build());
var response = await PayPal.PayPalClient.Client().Execute(request);
var result = response.Result<PayPalCheckoutSdk.Orders.Order>();
var payPalHttpResponse = new PayPal.SmartButtonHttpResponse(response)
{
orderID = result.Id
};
return payPalHttpResponse;
}
[Route("api/paypal/checkout/order/approved/{orderId}")]
public IActionResult Approved(string orderId)
{
return Ok();
}
[Route("api/paypal/checkout/order/complete/{orderId}")]
public IActionResult Complete(string orderId)
{
return Ok();
}
[Route("api/paypal/checkout/order/cancel/{orderId}")]
public IActionResult Cancel(string orderId)
{
return Ok();
}
[Route("api/paypal/checkout/order/error/{orderId}/{error}")]
public IActionResult Error(string orderId,
string error)
{
return NoContent();
}
}
}
-
Create a Checkout
folder in the Views folder and add a view called index.cshtml.
Add the following code to create the PayPal smart button to the view.
<!--
<div id="paypal-button-container"></div>
<!--
<script src="https://www.paypal.com/sdk/js?client-id=@ViewBag.ClientId&
currency=@ViewBag.CurrencyCode"></script>
<script>
var orderId;
paypal.Buttons({
createOrder: function (data, actions) {
orderId = data.orderID;
return fetch('/api/paypal/checkout/order/create/', {
method: 'post'
}).then(function (res) {
return res.json();
}).then(function (data) {
return data.orderID;
});
},
onApprove: function (data, actions) {
return fetch('/api/paypal/checkout/order/approved/' + data.orderID, {
method: 'post'
}).then(function (res) {
return actions.order.capture();
}).then(function (details) {
window.location.replace('/api/paypal/checkout/order/complete/' +
data.orderID + '/@ViewBag.CurrencyCode');
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onCancel: function (data, actions) {
httpGet('/api/paypal/checkout/order/cancel/' + data.orderID);
},
onError: function (err) {
httpGet('/api/paypal/checkout/order/error/' + orderId + '/' +
encodeURIComponent(err));
}
}).render('#paypal-button-container');
</script>
Apart from the URLs, this code is the same for all solutions.
-
Add the following JavaScript function:
function httpGet(url) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}
-
Create a folder called Values within the PayPal folder.
-
Add a class called CheckoutPaymentIntent.cs, within the Values folder.
Add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PayPal.Values
{
public static class CheckoutPaymentIntent
{
public static string CAPTURE { get; private set; } = "CAPTURE";
public static string AUTHORIZE { get; private set; } = "AUTHORIZE";
}
}
-
Add a class called CurrencyCode.cs, within the Values folder.
Add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PayPal.Values
{
public static class CurrencyCode
{
public static string GBP { get; private set; } = "GBP";
public static string USD { get; private set; } = "USD";
public static string EUR { get; private set; } = "EUR";
}
}
Add additional currencies, as required.
-
Add a class called LandingPage.cs, within the Values folder.
Add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PayPal.Values
{
public class LandingPage
{
public static string LOGIN { get; private set; } = "LOGIN";
public static string BILLING { get; private set; } = "BILLING";
public static string NO_PREFERENCE { get; private set; } = "NO_PREFERENCE";
}
}
-
Add a class called ShippingPreference.cs, within the Values folder.
Add the following code:
namespace PayPal.Values
{
public static class ShippingPreference
{
public static string GET_FROM_FILE { get; private set; } = "GET_FROM_FILE";
public static string NO_SHIPPING { get; private set; } = "NO_SHIPPING";
public static string SET_PROVIDED_ADDRESS { get; private set; } =
"SET_PROVIDED_ADDRESS";
}
}
-
Add a class called UserAction.cs, within the Values folder.
Add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PayPal.Values
{
public static class UserAction
{
public static string CONTINUE { get; private set; } = "CONTINUE";
public static string PAY_NOW { get; private set; } = "PAY_NOW";
}
}
-
Create a folder called Item within the PayPal\Values folder.
-
Add a class called Category.cs, within the Values folder.
Add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PayPal.Values.Item
{
public static class Category
{
public static string DIGITAL_GOODS { get; private set; } = "DIGITAL_GOODS";
public static string PHYSICAL_GOODS { get; private set; } = "PHYSICAL_GOODS";
}
}
- The final step is to write code to handle what happens when the following code is called:
- api/paypal/checkout/order/create
- api/paypal/checkout/order/approved/{orderId}
- api/paypal/checkout/order/complete/{orderId}
- api/paypal/checkout/order/cancel/{orderId}
- api/paypal/checkout/order/error/{orderId}/{error}
- Good luck!