Safepay logo
Docs
Safepay logo
Launch Safepay

Save Card

Learn how to integrate Safepay embedded into your backend application


A set of components allowing easy integration of Safepay Button and Safepay Checkout into your website, powered by zoid

1. Install Bindings

Install bindings via Composer. If you do not wish to use Composer, you can download the latest release of our PHP sdk.

Composer

Install bindings through Composer:

$ composer require getsafepay/sfpy-php

2. Use Bindings

Use binding manually or through Composer. In case of manual use, the bindings require the following extensions in order to work properly: 1) curl, although you can use your own non-cURL client if you prefer, 2) json, 3) mbstring (Multibyte String):

Composer

Manual

Use bindings through Composer:

$ require_once 'vendor/autoload.php';

3. Get Public and Secret Key

To instantiate the tracker, the utilization of your public key is imperative, as detailed in the subsequent discussion. For authorization purposes, the inclusion of your secret key is mandatory. Retrieve both keys from the Developers section under API on your dashboard. Ensure precision in accessing the appropriate sandbox or production dashboards aligned with your operational environment.

4. Create a Safepay client

Create a Safepay client by passing your config. To use the SDK in a sandbox environment set the base_url to https://sandbox.api.getsafepay.com

5. Initiate a tracker

The Tracker serves as a singular identifier for individual payments, enabling the comprehensive tracking of the complete journey undergone by a specific payment. To activate the instrument element in your API requests, ensure that the mode in the request body is set to `mode:instrument`. It's crucial to note that distinct modes are applicable to each element, and these variations will be elaborated upon in their respective sections.

6. Create customer

A Customer Token is vital for enrollment. Verify no duplicate entries before creating a new customer. Set is_guest = false for permanent card storage, or is_guest = true for no storage. This config ensures card details are kept for 24 hours, auto-deleting after a successful payment or within the specified timeframe for incomplete payments.

7. Generate address token

Including the address is crucial for successful transactions, impacting payment success. Furnishing all necessary details streamlines checkout by minimizing address fields. Enhance customer experience by thorough submission, reducing the need for manual address entry. Ensure comprehensive information is provided in the specified section.

8. Generate TBT

Employ your secret key to generate a Time-Based Token (TBT). The TBT is a prerequisite for authenticating the URL during the loading process of our embedded checkout.

9. Generate Checkout URL

Following parameters are required generate URL to render the Safepay instrument element

10. Webhooks

To enable Safepay integrations, register webhook endpoints for real-time event data transmission. Webhooks deliver HTTPS-secured JSON payloads, including Event objects, for monitoring asynchronous occurrences like payment confirmation or charge disputes. Establish server endpoints for Safepay webhooks, perform actions based on received event types, and send a 200 response code for successful receipt confirmation. Failure to provide a 200 response code prompts Safepay to resend the webhook.

11. Handle the event

Note Safepay requires the raw body of the request to perform signature verification. If you’re using a framework, make sure it doesn’t manipulate the raw body. Any manipulation to the raw body of the request causes the verification to fail.

Ending Step

Since we're huge fans of React at Safepay, we've integrated this library into our products like Quick Links through the following React integration that ships out of the box with this library

Ending Step

Since we're huge fans of React at Safepay, we've integrated this library into our products like Quick Links through the following React integration that ships out of the box with this library

Feedback

What do you think of our integration document? Let us know here support@getsafepay.com

$safepay = new SafepaySafepayClient([
'api_key' => 'BQokikJOvBiI2HlWgH4olfQ2',
'api_base' => 'https://sandbox.api.getsafepay.com'
]);
try {
// You need to generate a tracker with mode 'instrument'
// to tell Safepay that you wish to set up a tracker to
// tokenize a customer's card
$session = $safepay->order->setup([
"merchant_api_key" => "sec_8dcac601-4b70-442d-b198-03aadd28f12b",
"intent" => "CYBERSOURCE",
"mode" => "instrument",
"currency" => "PKR"
]);
// You need to either create a customer or retreive the customer
// from your backend so you have access to the customer ID
$customer = $safepay->customer->create([
//required
"first_name" => "Hassan",
"last_name" => "Zaidi",
"email" => "hzaidi@getsafepay.com",
"phone_number" => "+923331234567",
"country" => "PK"
//optional
"is_guest" => true
]);
// You can optionally create an address object if you have
// access to the customer's billing details
$address = $safepay->address->create([
// required
"street1" => "3A-2 7th South Street",
"city" => "Karachi",
"country" => "PK",
// optional
"postal_code" => "75500",
"province" => "Sindh"
]);
// You need to create a Time Based Authentication token
$tbt = $safepay->passport->create();
// Finally, you can create the Checkout URL
$checkoutURL = SafepayCheckout::constructURL([
"environment" => "production", // one of "development", "sandbox" or "production"
"tracker" => $session->tracker->token,
"user_id" => $customer->token,
"tbt" => $tbt,
"address" => $address->token,
"source" => "mobile" // important for rendering in a WebView
]);
echo($checkoutURL);
return $checkoutURL;
} catch(UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
Previous
IOS SDK
Next
Shopify