- Quick guide -
Create a payment
Accepting payment in your application.
Which programming language do you prefer?
| PHP
| Python
|
Use arrow keys or space bar to navigate in the presentation.
Authentication
Verify your identity when communicating with the API by providing a secret key in all your requests.
To submit your credentials, include your key in all pages that use the API:
<?php
\Payplug\Payplug::init(array(
'secretKey' => 'sk_live_YOUR_PRIVATE_KEY',
'apiVersion' => 'THE_API_VERSION',
));
payplug.set_secret_key('sk_live_YOUR_PRIVATE_KEY')
API keys start with “sk_”.
They are available in My account, then API Credentials in the PayPlug portal.
Access LIVE and TEST modes using the same endpoint. To switch between modes, simply provide the related secret key to the mode you wish to access.
Take a look at the changelog to know which api version you should use.
The payment creation process
- Create the payment; a link to a payment page will be generated.
- Redirect the customer to the payment page.
- After entering card details, your customer is automatically redirected to the return page on your site.
- PayPlug sends you a confirmation via IPN (Instant Payment Notification).
Creating a payment
The following code shows an example of a payment request that automatically redirects your client to the payment page:
<?php
$email = 'john.watson@example.net';
$amount = 33;
$customer_id = '42710';
$payment = \Payplug\Payment::create(array(
'amount' => $amount * 100,
'currency' => 'EUR',
'billing' => array(
'title' => 'mr',
'first_name' => 'John',
'last_name' => 'Watson',
'email' => 'john.watson@example.net',
'address1' => '221B Baker Street',
'postcode' => 'NW16XE',
'city' => 'London',
'country' => 'GB',
'language' => 'en'
),
'shipping' => array(
'title' => 'mr',
'first_name' => 'John',
'last_name' => 'Watson',
'email' => 'john.watson@example.net',
'address1' => '221B Baker Street',
'postcode' => 'NW16XE',
'city' => 'London',
'country' => 'GB',
'language' => 'en',
'delivery_type' => 'BILLING'
),
'hosted_payment' => array(
'return_url' => 'https://example.net/return?id='.$customer_id,
'cancel_url' => 'https://example.net/cancel?id='.$customer_id
),
'notification_url' => 'https://example.net/notifications?id='.$customer_id,
'metadata' => array(
'customer_id' => $customer_id
)
));
$payment_url = $payment->hosted_payment->payment_url;
$payment_id = $payment->id;
header('Location:' . $payment_url);
payment_data = {
'amount': 3300,
'currency': 'EUR',
'customer': {
'email': 'john.watson@example.net'
},
'hosted_payment': {
'return_url': 'https://example.net/return?id=42710',
'cancel_url': 'https://example.net/cancel?id=42710',
},
'notification_url': 'https://example.net/notifications?id=42710',
'metadata': {
'customer_id': 42710,
},
}
payment = payplug.Payment.create(**payment_data)
payment_id = str(payment.id)
IMPORTANT: Please note that all amounts must be expressed in centimes as positive whole numbers (1€ = 100 centimes).
Confirmation
Option 1: Instant Payment Notification (IPN)
When creating a payment, you can specify a notification URL for use with IPN: notification_url
. Whether the payment is successful or is fails, a POST query containing an object corresponding to the payment will be sent to your server.
<?php
$input = file_get_contents('php://input');
try {
$resource = \Payplug\Notification::treat($input);
if ($resource instanceof \Payplug\Resource\Payment
&& $resource->is_paid) {
$payment_id = $resource->id;
$payment_state = $resource->is_paid;
$payment_date = $resource->hosted_payment->paid_at;
$payment_amount = $resource->amount;
$payment_data = $resource->metadata[customer_id];
}
}
catch (\Payplug\Exception\PayplugException $exception) {
echo htmlentities($exception);
}
request_body = request.body
try:
resource = payplug.notifications.treat(request_body)
except payplug.exceptions.PayplugError:
pass
else:
if resource.object == 'payment' and resource.is_paid:
payment_id = str(resource.id)
payment_state = str(resource.is_paid)
payment_date = datetime.datetime.fromtimestamp(resource.hosted_payment.paid_at).strftime('%d/%m/%Y %H:%M:%S')
payment_amount = str(resource.amount)
payment_data = resource.metadata['customer_id']
pass
The notification URL must be publicly accessible via the internet. It will not work if you are operating offline or if the page is protected with a firewall or proxy.
Confirmation
If you prefer not to use IPN, you may use the following method to extract payment details:
Steps one to three are the same as in the first diagram. In step four, make an API call using the payment ID that was generated when the payment was created:
<?php
$payment = \Payplug\Payment::retrieve($payment_id);
payment = payplug.Payment.retrieve(payment_id)
Metadata allow you to include additional information when processing payments or refunds.
{
"metadata": {
"transaction_id": "tsct_201506023456",
"customer_id": 58790,
"product_id": "ts_blk_00234",
"shipping": "The delivery was delayed"
}
}
You can add up to ten keys. Names cannot exceed twenty (20) characters, and stored content cannot exceed five hundred (500) characters.