
<?php

require_once(__DIR__ . '/php-sdk/vendor/autoload.php');

// Configuration
$spaceId = 70098;
$userId = 131904;
$secret = '4UUpTYGDFrZOhAHHzIHDsau55A9NbB4piZHbUDrDRyw=';

// Setup API client
$client = new \PostFinanceCheckout\Sdk\ApiClient($userId, $secret);

// Create transaction
$lineItem = new \PostFinanceCheckout\Sdk\Model\LineItemCreate();

$lineItem->setName('reservationssystem');
$lineItem->setUniqueId($tpl['arr']['custom']);
$lineItem->setSku('reservations-system');
$lineItem->setQuantity(1);
$lineItem->setAmountIncludingTax($tpl['arr']['amount']);
$lineItem->setType(\PostFinanceCheckout\Sdk\Model\LineItemType::PRODUCT);

// Customer Billing Address
$billingAddress = new \PostFinanceCheckout\Sdk\Model\AddressCreate();

$billingAddress->setEmailAddress('test@example.com');
$billingAddress->setFamilyName('Customer');
$billingAddress->setGivenName('Good');
$billingAddress->setOrganizationName('Test GmbH');

$transactionPayload = new \PostFinanceCheckout\Sdk\Model\TransactionCreate();

$transactionPayload->setCurrency($tpl['arr']['currency_code']);
$transactionPayload->setLineItems(array($lineItem));
$transactionPayload->setBillingAddress($billingAddress);
$transactionPayload->setShippingAddress($billingAddress);
$transactionPayload->setAutoConfirmationEnabled(true);

 $transactionPayload->setSuccessUrl($tpl['arr']['notify_url']);
//->setSuccessUrl('https://www.phpjabbers.com');

$transactionPayload->setFailedUrl($tpl['arr']['cancel_url']);

$transactionPayload->setLanguage('en-US');

// $transactionPayload->setAllowedPaymentMethodConfigurations([334328]);

$transaction = $client->getTransactionService()->create($spaceId, $transactionPayload);

$PostFinanceId = pjBookingModel::factory()->where('uuid', $tpl['arr']['custom'])->limit(1)->modifyAll(array('txn_id' => $transaction->getId()));

//  $this->set('postfinanceid', $transaction); 

// Create Payment Page URL:
$redirectionUrl = $client->getTransactionPaymentPageService()->paymentPageUrl($spaceId, $transaction->getId());

// Set transaction ID in a cookie
setcookie('postfinance_transaction_id', $transaction->getId(), time() + 3600, '/'); // Cookie valid for 1 hour0

// header('Location: ' . $redirectionUrl);

?>

<form id="postfinanceForm" name="pjOnlinePaymentForm" action="<?php echo $redirectionUrl; ?>" method="post">
   
</form>

<script>

    localStorage.setItem('postfinance_transaction_id', '<?php echo $transaction->getId(); ?>');

</script>

