Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TokenV2 Payments Service #36

Open
wants to merge 24 commits into
base: payments_v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

/**
* Include composer class autoloader
*/
require_once dirname(__FILE__) . '/../vendor/autoload.php';


use PayU\Alu\Billing;
use PayU\Alu\Card;
use PayU\Alu\Client;
Expand All @@ -14,7 +14,6 @@
use PayU\Alu\Product;
use PayU\Alu\Request;
use PayU\Alu\User;
use PayU\Alu\Exceptions\ConnectionException;
use PayU\Alu\Exceptions\ClientException;
use PayU\PaymentsApi\PaymentsV4\PaymentsV4;

Expand All @@ -25,13 +24,12 @@
* Secret Key - Your PayU Secret Key
* Platform - RO | RU | UA | TR | HU
*/
//todo modify merchantCode back to MERCHANT_CODE
$cfg = new MerchantConfig('PAYU_2', 'SECRET_KEY', 'RO');

$cfg = new MerchantConfig('MERCHANT_CODE', 'SECRET_KEY', 'RO');
/**
* Create user with params:
*
* User IP - User's IP address
* Card Number Input Time - Time it took the user to enter credit card number in seconds
* User Time - Time of user computer - optional
*
*/
Expand All @@ -48,15 +46,8 @@
* Full params available in the documentation
*/

/**
* todo remove $merchantOrderRef when pushing to master
* also date format has been modified
*/

$merchantOrderRef = strval(rand(1000, 9999));
$order->withBackRef('http://path/to/your/returnUrlScript')
->withOrderRef($merchantOrderRef)
//->withOrderRef('MerchantOrderRef')
->withOrderRef('MerchantOrderRef')
->withCurrency('RON')
->withOrderDate(gmdate('Y-m-d\TH:i:sP'))
->withOrderTimeout(1000)
Expand Down Expand Up @@ -124,7 +115,6 @@
->withPhoneNumber('40123456789')
->withIdentityCardNumber('111222');


/**
* Create new delivery address
*
Expand Down Expand Up @@ -156,7 +146,18 @@
* Credit Card CVV (Security Code)
* Credit Card Owner
*/
$card = new Card('4111111111111111', '01', '2026', '123', 'Card Owner Name');
$card = new Card(
'4111111111111111',
'01',
date("Y", strtotime("+1 year")),
123,
'Card Owner Name'
);

/**
* tokenize card for further token payments
*/
$card->enableTokenCreation();

/**
* Create new Request with params:
Expand All @@ -168,7 +169,6 @@
* User object
*/
$request = new Request($cfg, $order, $billing, $delivery, $user, PaymentsV4::API_VERSION_V4);
//$request = new Request($cfg, $order, $billing, $delivery, $user);

/**
* Add the Credit Card to the Request
Expand Down Expand Up @@ -202,12 +202,17 @@
die();
}

echo $response->getCode()
. ' ' . $response->getStatus()
. ' ' . $response->getReturnCode()
. ' ' . $response->getReturnMessage();
} catch (ConnectionException $exception) {
echo $exception->getMessage();
echo $response->getStatus() . ' ' . $response->getReturnCode() . ' ' . $response->getReturnMessage() . "\n";

if ($response->getTokenResponseData() === null) {
echo 'Could not make token request because authorization failed.' . "\n";
die();
}

echo('Token response data: ');
echo $response->getTokenResponseData()->getTokenCode() . ' ' .
$response->getTokenResponseData()->getTokenMessage() .
' Token:' . $response->getTokenHash();
} catch (ClientException $exception) {
echo $exception->getErrorMessage();
}
249 changes: 249 additions & 0 deletions examplesV4/tokenCreationAndPaymentExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?php

/**
* Include composer class autoloader
*/
require_once dirname(__FILE__) . '/../vendor/autoload.php';

use PayU\Alu\Billing;
use PayU\Alu\Card;
use PayU\Alu\CardToken;
use PayU\Alu\Client;
use PayU\Alu\Delivery;
use PayU\Alu\MerchantConfig;
use PayU\Alu\Order;
use PayU\Alu\Product;
use PayU\Alu\Request;
use PayU\Alu\User;
use PayU\Alu\Exceptions\ClientException;
use PayU\PaymentsApi\PaymentsV4\PaymentsV4;

/**
* Create configuration with params:
*
* Merchant Code - Your PayU Merchant Code
* Secret Key - Your PayU Secret Key
* Platform - RO | RU | UA | TR | HU
*/
$cfg = new MerchantConfig('MERCHANT_CODE', 'SECRET_KEY', 'RO');
/**
* Create user with params:
*
* User IP - User's IP address
* Card Number Input Time - Time it took the user to enter credit card number in seconds
* User Time - Time of user computer - optional
*
*/
$user = new User('127.0.0.1');

/**
* Create new order
*/
$order = new Order();

/**
* Setup the order params
*
* Full params available in the documentation
*/

$order->withBackRef('http://path/to/your/returnUrlScript')
->withOrderRef('MerchantOrderRef')
->withCurrency('RON')
->withOrderDate(gmdate('Y-m-d\TH:i:sP'))
->withOrderTimeout(1000)
->withPayMethod('CCVISAMC');

/**
* Create new product
*/
$product = new Product();

/**
* Setup the product params
*
* Full params available in the documentation
*/
$product->withCode('PCODE01')
->withName('PNAME01')
->withPrice(100.0)
->withVAT(24.0)
->withQuantity(1);

/**
* Add the product to the order
*/
$order->addProduct($product);

/**
* Create another product
*/
$product = new Product();

/**
* Setup the product params
*
* Full params available in the documentation
*/
$product->withCode('PCODE02')
->withName('PNAME02')
->withPrice(200.0)
->withVAT(24.0)
->withQuantity(1);

/**
* Add the second product to the same order
*/
$order->addProduct($product);

/**
* Create new billing address
*/
$billing = new Billing();

/**
* Setup the billing address params
*
* Full params available in the documentation
*/
$billing->withAddressLine1('Address1')
->withAddressLine2('Address2')
->withCity('City')
->withCountryCode('RO')
->withEmail('john.doe@mail.com')
->withFirstName('FirstName')
->withLastName('LastName')
->withPhoneNumber('40123456789')
->withIdentityCardNumber('111222');

/**
* Create new delivery address
*
* If you want to have the same delivery as billing, skip these two steps
* and pass the Billing $billing object to the request twice
*/
$delivery = new Delivery();

/**
* Setup the delivery address params
*
* Full params available in the documentation
*/
$delivery->withAddressLine1('Address1')
->withAddressLine2('Address2')
->withCity('City')
->withCountryCode('RO')
->withEmail('john.doe@mail.com')
->withFirstName('FirstName')
->withLastName('LastName')
->withPhoneNumber('40123456789');

/**
* Create new Card with params:
*
* Credit Card Number
* Credit Card Expiration Month
* Credit Card Expiration Year
* Credit Card CVV (Security Code)
* Credit Card Owner
*/
$card = new Card(
'4111111111111111',
'01',
date("Y", strtotime("+1 year")),
123,
'Card Owner Name'
);

/**
* tokenize card for further token payments
*/
$card->enableTokenCreation();

/**
* Create new Request with params:
*
* Config object
* Order object
* Billing object
* Delivery (or Billing object again, if you want to have the delivery address the same as the billing address)
* User object
*/
$request = new Request($cfg, $order, $billing, $delivery, $user, PaymentsV4::API_VERSION_V4);
/**
* Add the Credit Card to the Request
*/
$request->setCard($card);

/**
* Create new API Client, passing the Config object as parameter
*/
$client = new Client($cfg);

/**
* Will throw different Exceptions on errors
*/
try {
/**
* Sends the Request to ALU and returns a Response
*
* See documentation for Response params
*/
$initialPaymentResponse = $client->pay($request);

/**
* In case of 3DS enrolled cards, PayU will return the URL_3DS that contains a unique url for each
* transaction. The merchant must redirect the browser to this url to allow user to authenticate.
* After the authentication process ends the user will be redirected to BACK_REF url
* with payment result in a HTTP POST request
*/
if ($initialPaymentResponse->isThreeDs()) {
header("Location:" . $initialPaymentResponse->getThreeDsUrl());
die();
}

echo $initialPaymentResponse->getStatus() . ' ' .
$initialPaymentResponse->getReturnCode() . ' ' .
$initialPaymentResponse->getReturnMessage() .
"\n";

if ($initialPaymentResponse->getTokenResponseData() === null) {
echo 'Could not make token request because authorization failed.' . "\n";
die();
}

echo 'Token response data: ';
echo $initialPaymentResponse->getTokenResponseData()->getTokenCode() . ' ' .
$initialPaymentResponse->getTokenResponseData()->getTokenMessage() .
' Token:' . $initialPaymentResponse->getTokenHash() .
"\n";

if ($initialPaymentResponse->getTokenResponseData()->getTokenCode() == 0 &&
$initialPaymentResponse->getTokenHash() !== ''
) {
$cardToken = new CardToken($initialPaymentResponse->getTokenHash());

// Create a new request using the token received
$requestWithToken = new Request(
$cfg,
$order,
$billing,
$delivery,
$user,
PaymentsV4::API_VERSION_V4
);
$requestWithToken->getOrder()->withOrderRef('TokenOrderRef');
$requestWithToken->setCardToken($cardToken);

$tokenPaymentResponse = $client->pay($requestWithToken);

echo $tokenPaymentResponse->getStatus() . ' ' .
$tokenPaymentResponse->getReturnCode() . ' ' .
$tokenPaymentResponse->getReturnMessage() .
"\n";
} else {
echo "No token received";
}
} catch (ClientException $exception) {
echo $exception->getErrorMessage();
}
Loading