Last active
March 25, 2022 20:42
-
-
Save Ciantic/f7e1087d3a8a742d7da66e92d9b7771c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors', 1); | |
error_reporting(E_ALL); | |
use Paytrail\SDK\Client; | |
use Paytrail\SDK\Exception\HmacException; | |
use Paytrail\SDK\Exception\ValidationException; | |
use Paytrail\SDK\Model\Address; | |
use Paytrail\SDK\Model\CallbackUrl; | |
use Paytrail\SDK\Model\Customer; | |
use Paytrail\SDK\Model\Item; | |
use Paytrail\SDK\Request\PaymentRequest; | |
require 'vendor/autoload.php'; | |
$client = new Client( | |
375917, | |
'SAIPPUAKAUPPIAS', | |
'php-sdk-test' | |
); | |
// Callback to validate payment (used only when returning from Paytrail) | |
if (isset($_GET["signature"])) { | |
try { | |
$client->validateHmac($_GET, '', $_GET["signature"]); | |
} catch (HmacException $e) { | |
die("Signature validation failed"); | |
} | |
if ($_GET["checkout-status"] === "ok") { | |
die("Payment succeeded"); | |
} | |
if ($_GET["checkout-status"] === "fail") { | |
die("Payment failed"); | |
} | |
exit; | |
} | |
// Create payment -------------------------------------------- | |
$payment = new PaymentRequest(); | |
$payment->setStamp(hash('sha256', time())); | |
$payment->setAmount(100 * 100); | |
// Items need not to be set, amount is enough! | |
$payment->setItems([ | |
(new Item())->setVatPercentage(24) | |
->setDescription("Tuote") | |
->setUnitPrice(100 * 100) | |
->setUnits(1) | |
->setProductCode("tuotekoodi") | |
]); | |
$payment->setReference('your order reference'); | |
$payment->setCurrency('EUR'); | |
$payment->setLanguage("FI"); | |
$customer = new Customer(); | |
$customer->setEmail("matti@example.com") | |
->setFirstName("Matti") | |
->setLastName("Meikäläinen") | |
->setPhone("05012341234"); | |
$payment->setCustomer($customer); | |
$address = new Address(); | |
$address->setStreetAddress("Testikatu 123") | |
->setPostalCode("40600") | |
->setCity("Jyväskylä") | |
->setCounty("") | |
->setCountry("FI"); | |
$payment->setInvoicingAddress($address); | |
$payment->setDeliveryAddress($address); | |
// Callback url in this example is this same URL. | |
$callback_url = "https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; | |
$callback = new CallbackUrl(); | |
$callback->setSuccess($callback_url); | |
$callback->setCancel($callback_url); | |
$payment->setRedirectUrls($callback); | |
$payment->setCallbackUrls($callback); | |
try { | |
$paymentResponse = $client->createPayment($payment); | |
header("Location: {$paymentResponse->getHref()}"); | |
exit; | |
} catch (HmacException $e) { | |
$error = $e->getMessage(); | |
} catch (ValidationException $e) { | |
$error = $e->getMessage(); | |
} catch (Exception $e) { | |
$error = $e->getMessage(); | |
} | |
if (!empty($error)) { | |
die($error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment