Showing posts with label PayWay. Show all posts
Showing posts with label PayWay. Show all posts

Sunday, July 16, 2017

PayWay Payment GateWay | Hosted Payment Page Setup | PayWay Hosted Payment | Hosted Payment

It's easy to integrate PayWay hosted payment system. First we need an account. If you have not any PayWay account yet, go to https://www.payway.com.au/core/LoginView and create an account first.

Now visit to https://www.payway.com.au/net/HostedPaymentPageSetupView and follow below screen:


Next step is to collect PayWay biller code:



Next step is to setup some notification configuration:



Below screen for notification panel, the marked url will be notified once a payment made via POST method:



I used "http://luckyorange.net/payway-hosted/callback.php?ThisPartWould0=&SecurityCheck=" as "Browser Return URL".

Where SecurityCheck field will be filled up when return to our working server.

Now from below page we will collect decryption key and security username and password. You have to add your server ip address here.



Now we will setup customer reference field as below:



Next we will setup minimum and maximum payment amount.



Next we will setup surcharge configuration:



And now finally we will do implementation.

First we will create a payment token and then redirect to PayWay payment page, below is a PHP script:


<?php
include_once "CurlExecutor.php";

$token_url = "https://www.payway.com.au/RequestToken";
$payment_url = "https://www.payway.com.au/MakePayment";
$redirect_uri = "http://luckyorange.net/payway-hosted/callback.php?ThisPartWould0=&SecurityCheck=";

define("BILLER_CODE", "10...6");
define("USERNAME", "T1...");
define("PASSWORD", "N........");

$post_data = "biller_code=" . BILLER_CODE;
$post_data = $post_data . "&username=" . USERNAME;
$post_data = $post_data . "&password=" . PASSWORD;

/* CUSTOMER INFORMATION */
$post_data = $post_data . "&information_fields=Name,InvoiceNO,Address";
$post_data = $post_data . "&Name=" . urlencode("Pritom Kumar");
$post_data = $post_data . "&InvoiceNO=" . strtoupper(substr(md5(time()), 0, 10));
$post_data = $post_data . "&Address=" . urlencode("Some Address");

/* HIDDEN FIELDS */
$post_data = $post_data . "&hidden_fields=SecurityCheck";
$post_data = $post_data . "&SecurityCheck=" . urlencode("SECURE TEXT");

/* PRODUCT DETAILS */
$post_data = $post_data . "&Shampoo=1,0.10";
$post_data = $post_data . "&Soap=2,0.20";

/* REQUEST HEADERS */
$headers[] = "Content-type: application/x-www-form-urlencoded";

$response = CurlExecutor::execute($token_url, "POST", $post_data, null, $headers);
if ($response["code"] == 200) {
    $token = substr($response["response"], 6);
    header("Refresh:0; url=$payment_url?biller_code=" . BILLER_CODE . "&token=" . $token);
}
CurlExecutor::prettyPrint($response);


Which will redirect to PayWay payment page as below screenshot:



After successful payment PayWay will make a redirection to our server (redirect url we provided) as below format:

http://luckyorange.net/payway-hosted/callback.php?EncryptedParameters=...&Signature=...

We will decrypt "EncryptedParameters" using AES 128 algorithm with PKCS7 padding. Key is "Encryption Key" on "Security Information" page.


<?php
function decrypt($key, $to_decrypt)
{
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = substr($to_decrypt, 0, $iv_size);
    $to_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, substr($to_decrypt, $iv_size), MCRYPT_MODE_CBC, $iv);
    $pad = ord($to_decrypt[strlen($to_decrypt) - 1]);
    return substr($to_decrypt, 0, -$pad);
}

$key = base64_decode("NI+YM.............ojAIQ==");
$params = decrypt($key, base64_decode($_GET["EncryptedParameters"]));
echo "<pre>";
print_r(explode("&", $params));
echo "</pre>";

And would be like below:


Array
(
    [0] => SecurityCheck=SECURE+TEXT
    [1] => payment_reference=E4DF550696
    [2] => payment_amount=0.50
    [3] => payment_date=20170717
    [4] => payment_time=17+Jul+2017+01%3A33%3A53
    [5] => payment_number=1979758473
    [6] => bank_reference=1979758473
    [7] => remote_ip=103.59.179.132
    [8] => card_type=VISA
    [9] => response_code=08
    [10] => summary_code=0
    [11] => response_text=Honour+with+identification
    [12] => payment_status=approved
)


And the payment in PayWay below screenshot:



So it's all now. Pure implementation. Step by step description. Simple coding. At last you can download PayWay hosted payment documentation from link next PayWay Hosted Payment Guide.

Saturday, June 10, 2017

PayWay Rest API: Create Refund From PayWay Payment

You can visit PayWay rest api documentation page:
https://www.payway.com.au/docs/rest.html#refund-a-payment

PayWay Rest API: Check if Rest API credentials are valid

PayWay Rest API: Create Customer Create Token


<?php
include_once "CurlExecutor.php";

define("BASE_URL", "https://api.payway.com.au/rest/v1");
define("MERCHANT_ID", "TEST");
define("PRIVATE_KEY", "T10487_SEC_...");
define("PUBLIC_KEY", "T10487_PUB_...");

createRefundOfPayment();

function createRefundOfPayment() {
    $headers[] = "Authorization: Basic " . base64_encode(PRIVATE_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "parentTransactionId" => "1956258559",
        "transactionType" => "refund",
        "principalAmount" => "23.45",
        "orderNumber" => "RefundIdentification"
    );

    $result = CurlExecutor::execute(BASE_URL . "/transactions", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    CurlExecutor::prettyPrint($result);
}
?>


And output is below:


Array
(
    [code] => 201
    [response] => stdClass Object
        (
            [transactionId] => 1958000143
            [receiptNumber] => 1958000143
            [status] => approved
            [responseCode] => 08
            [responseText] => Honour with identification
            [transactionType] => refund
            [customerNumber] => 5
            [customerName] => Hexa Lucio
            [customerEmail] => hexa@bitmascot.com
            [orderNumber] => RefundIdentification
            [currency] => aud
            [principalAmount] => -23.45
            [surchargeAmount] => 0
            [paymentAmount] => -23.45
            [paymentMethod] => creditCard
            [creditCard] => stdClass Object
                (
                    [cardNumber] => 456471...004
                    [expiryDateMonth] => 02
                    [expiryDateYear] => 19
                    [cardScheme] => visa
                    [cardholderName] => Hexa Lucio Tony
                )

            [merchant] => stdClass Object
                (
                    [merchantId] => TEST
                    [merchantName] => Test Merchant
                    [links] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [rel] => self
                                    [href] => https://api.payway.com.au/rest/v1/merchants/TEST
                                )

                        )

                )

            [transactionDateTime] => 10 Jun 2017 13:24 AEST
            [settlementDate] => 10 Jun 2017
            [parentTransaction] => stdClass Object
                (
                    [transactionId] => 1956258559
                    [receiptNumber] => 1956258559
                    [status] => approved
                    [transactionType] => payment
                    [customerNumber] => 5
                    [orderNumber] => INV-GGO-ABA-460
                    [currency] => aud
                    [paymentAmount] => 99.25
                    [settlementDate] => 08 Jun 2017
                )

            [isVoidable] => 1
            [isRefundable] => 
            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => self
                            [href] => https://api.payway.com.au/rest/v1/transactions/1958000143
                        )

                    [1] => stdClass Object
                        (
                            [rel] => void
                            [href] => https://api.payway.com.au/transactions/1958000143/void
                        )

                    [2] => stdClass Object
                        (
                            [rel] => parent
                            [href] => https://api.payway.com.au/rest/v1/transactions/1956258559
                        )

                )

        )

)





PayWay Rest API: Create Payment Using Stored Credit Card Token

It's easy to store credit card in PayWay and create payment using that that credit card token.

PayWay Rest API: Check if Rest API credentials are valid

PayWay Rest API: Create Customer Create Token

Below is a PHP script to create payment using stored credit card token:


<?php
include_once "CurlExecutor.php";

define("BASE_URL", "https://api.payway.com.au/rest/v1");
define("MERCHANT_ID", "TEST");
define("PRIVATE_KEY", "T10487_SEC_...");
define("PUBLIC_KEY", "T10487_PUB_...");

createTokenPayment();

function createTokenPayment() {
    $headers[] = "Authorization: Basic " . base64_encode(PRIVATE_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "customerNumber" => 7,
        "merchantId" => MERCHANT_ID,
        "transactionType" => "payment",
        "principalAmount" => "23.45",
        "currency" => "aud",
        "orderNumber" => "Sale-Identification"
    );

    $result = CurlExecutor::execute(BASE_URL . "/transactions", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    CurlExecutor::prettyPrint($result);
}
?>

Output as below:


Array
(
    [code] => 201
    [response] => stdClass Object
        (
            [transactionId] => 1957977083
            [receiptNumber] => 1957977083
            [status] => approved
            [responseCode] => 08
            [responseText] => Honour with identification
            [transactionType] => payment
            [customerNumber] => 7
            [customerName] => Pritom
            [customerEmail] => pritomkucse@gmail.com
            [orderNumber] => Sale-Identification
            [currency] => aud
            [principalAmount] => 23.45
            [surchargeAmount] => 0
            [paymentAmount] => 23.45
            [paymentMethod] => creditCard
            [creditCard] => stdClass Object
                (
                    [cardNumber] => 516320...008
                    [expiryDateMonth] => 08
                    [expiryDateYear] => 20
                    [cardScheme] => mastercard
                    [cardholderName] => My Mastercard
                )

            [merchant] => stdClass Object
                (
                    [merchantId] => TEST
                    [merchantName] => Test Merchant
                    [links] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [rel] => self
                                    [href] => https://api.payway.com.au/rest/v1/merchants/TEST
                                )

                        )

                )

            [transactionDateTime] => 10 Jun 2017 13:09 AEST
            [settlementDate] => 10 Jun 2017
            [isVoidable] => 1
            [isRefundable] => 
            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => self
                            [href] => https://api.payway.com.au/rest/v1/transactions/1957977083
                        )

                    [1] => stdClass Object
                        (
                            [rel] => void
                            [href] => https://api.payway.com.au/transactions/1957977083/void
                        )

                )

        )

)








PayWay Rest API: Create Customer Create Token

You can create token in PayWay portal using credit card details to use further that token to create payment.

PayWay Rest API: Check if Rest API credentials are valid

If you are interested more on PayWay Rest API you can visit the below link:

https://www.payway.com.au/docs/rest.html

Below is a PHP script to create token using credit card etails. Be aware that below code snippet used both private and public key in different section.



<?php
include_once "CurlExecutor.php";

define("BASE_URL", "https://api.payway.com.au/rest/v1");
define("MERCHANT_ID", "TEST");
define("PRIVATE_KEY", "T10487_SEC_...");
define("PUBLIC_KEY", "T10487_PUB_...");

createCustomer(getSingleUseToken());

function createCustomer($token) {
    $headers[] = "Authorization: Basic " . base64_encode(PRIVATE_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "singleUseTokenId" => $token,
        "merchantId" => MERCHANT_ID,
        "customerName" => "Pritom",
        "emailAddress" => "pritomkucse@gmail.com"
    );

    $result = CurlExecutor::execute(BASE_URL . "/customers", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    CurlExecutor::prettyPrint($result);
}

function getSingleUseToken() {
    $headers[] = "Authorization: Basic " . base64_encode(PUBLIC_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "paymentMethod" => "creditCard",
        "cardNumber" => "5163200000000008",
        "cardholderName" => "My Mastercard",
        "cvn" => "070",
        "expiryDateMonth" => "08",
        "expiryDateYear" => "20",
    );

    $result = CurlExecutor::execute(BASE_URL . "/single-use-tokens", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    if ($result["code"] == 200) {
        return $result["response"]->singleUseTokenId;
    }
    CurlExecutor::prettyPrint($result);
    die("Error");
}
?>

Output is below:


Array
(
    [code] => 201
    [response] => stdClass Object
        (
            [customerNumber] => 7
            [paymentSetup] => stdClass Object
                (
                    [paymentMethod] => creditCard
                    [stopped] => 
                    [creditCard] => stdClass Object
                        (
                            [cardNumber] => 516320...008
                            [expiryDateMonth] => 08
                            [expiryDateYear] => 20
                            [cardScheme] => mastercard
                            [cardholderName] => My Mastercard
                            [surchargePercentage] => 0
                        )

                    [merchant] => stdClass Object
                        (
                            [merchantId] => TEST
                            [merchantName] => Test Merchant
                            [links] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [rel] => self
                                            [href] => https://api.payway.com.au/rest/v1/merchants/TEST
                                        )

                                )

                        )

                )

            [contact] => stdClass Object
                (
                    [customerName] => Pritom
                    [emailAddress] => pritomkucse@gmail.com
                    [sendEmailReceipts] => 
                    [phoneNumber] => 
                    [address] => stdClass Object
                        (
                            [street1] => 
                            [street2] => 
                            [cityName] => 
                            [state] => 
                            [postalCode] => 
                        )

                )

            [customFields] => stdClass Object
                (
                )

            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => self
                            [href] => https://api.payway.com.au/rest/v1/customers/7
                        )

                    [1] => stdClass Object
                        (
                            [rel] => help
                            [href] => https://www.payway.com.au/docs/rest.html#customers
                        )

                    [2] => stdClass Object
                        (
                            [rel] => contact
                            [href] => https://api.payway.com.au/rest/v1/customers/7/contact
                        )

                    [3] => stdClass Object
                        (
                            [rel] => custom-fields
                            [href] => https://api.payway.com.au/rest/v1/customers/7/custom-fields
                        )

                    [4] => stdClass Object
                        (
                            [rel] => payment-setup
                            [href] => https://api.payway.com.au/rest/v1/customers/7/payment-setup
                        )

                    [5] => stdClass Object
                        (
                            [rel] => schedule
                            [href] => https://api.payway.com.au/rest/v1/customers/7/schedule
                        )

                    [6] => stdClass Object
                        (
                            [rel] => virtual-account
                            [href] => https://api.payway.com.au/rest/v1/customers/7/virtual-account
                        )

                    [7] => stdClass Object
                        (
                            [rel] => search-customer-transactions
                            [href] => https://api.payway.com.au/rest/v1/transactions/search-customer?customerNumber=7
                        )

                )

        )

)





PayWay Rest API: Create Credit Card Payment

If you are not already check connection to PayWay Rest API, you need to do that validation. Follow the below link to test API connection, there you can learn about public and private key.


If you are interested more on PayWay Rest API you can visit the below link:

https://www.payway.com.au/docs/rest.html

Below is a PHP script to create credit card payment. Be aware that below code snippet used both private and public key in different section.


<?php
include_once "CurlExecutor.php";

define("BASE_URL", "https://api.payway.com.au/rest/v1");
define("MERCHANT_ID", "TEST");
define("PRIVATE_KEY", "T10487_SEC_...");
define("PUBLIC_KEY", "T10487_PUB_...");

createPayment(getSingleUseToken());

function createPayment($token) {
    $headers[] = "Authorization: Basic " . base64_encode(PRIVATE_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "singleUseTokenId" => $token,
        "customerNumber" => "Customer-ID",
        "transactionType" => "payment",
        "principalAmount" => "12.34",
        "currency" => "aud",
        "orderNumber" => "Sale-Identification",
        "merchantId" => MERCHANT_ID
    );

    $result = CurlExecutor::execute(BASE_URL . "/transactions", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    CurlExecutor::prettyPrint($result);
}

function getSingleUseToken() {
    $headers[] = "Authorization: Basic " . base64_encode(PUBLIC_KEY . ":");
    $headers[] = "Content-Type: application/x-www-form-urlencoded";

    $post = array(
        "paymentMethod" => "creditCard",
        "cardNumber" => "5163200000000008",
        "cardholderName" => "My Mastercard",
        "cvn" => "070",
        "expiryDateMonth" => "08",
        "expiryDateYear" => "20",
    );

    $result = CurlExecutor::execute(BASE_URL . "/single-use-tokens", "POST", $post, null, $headers);
    $result["response"] = json_decode($result["response"]);
    if ($result["code"] == 200) {
        return $result["response"]->singleUseTokenId;
    }
    CurlExecutor::prettyPrint($result);
    die("Error");
}
?>



Below is output of valid payment:


Array
(
    [code] => 201
    [response] => stdClass Object
        (
            [transactionId] => 1957973936
            [receiptNumber] => 1957973936
            [status] => approved
            [responseCode] => 08
            [responseText] => Honour with identification
            [transactionType] => payment
            [customerNumber] => CUSTOMER-ID
            [customerName] => My Mastercard
            [orderNumber] => Sale-Identification
            [currency] => aud
            [principalAmount] => 12.34
            [surchargeAmount] => 0
            [paymentAmount] => 12.34
            [paymentMethod] => creditCard
            [creditCard] => stdClass Object
                (
                    [cardNumber] => 516320...008
                    [expiryDateMonth] => 08
                    [expiryDateYear] => 20
                    [cardScheme] => mastercard
                    [cardholderName] => My Mastercard
                )

            [merchant] => stdClass Object
                (
                    [merchantId] => TEST
                    [merchantName] => Test Merchant
                    [links] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [rel] => self
                                    [href] => https://api.payway.com.au/rest/v1/merchants/TEST
                                )

                        )

                )

            [transactionDateTime] => 10 Jun 2017 11:57 AEST
            [settlementDate] => 10 Jun 2017
            [isVoidable] => 1
            [isRefundable] => 
            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => self
                            [href] => https://api.payway.com.au/rest/v1/transactions/1957973936
                        )

                    [1] => stdClass Object
                        (
                            [rel] => void
                            [href] => https://api.payway.com.au/transactions/1957973936/void
                        )

                )

        )

)

And you can see that in PayWay portal there is a payment created:


Error response from PayWay Rest API is well defined. So you can capture error response easily.


PayWay Rest API: Check if Rest API credentials are valid

PayWay Rest API is easy to implement. We need three information from PayWay end to create payment, refund payment, void payment, store credit card for further use and many other things.

So we need:
1. Publishable public key
2. Secret private key



3. and Merchant ID



Now you have all 3 information to communicate with PayWay.

Below is a PHP script to test credentials using rest API:


<?php
include_once "CurlExecutor.php";

define("BASE_URL", "https://api.payway.com.au/rest/v1");
define("MERCHANT_ID", "TEST");
define("PRIVATE_KEY", "T10487_SEC_...");
define("PUBLIC_KEY", "T10487_PUB_...");

$headers[] = "Authorization: Basic " . base64_encode(PUBLIC_KEY . ":");
$result = CurlExecutor::execute(BASE_URL, "GET", null, null, $headers);
$result["response"] = json_decode($result["response"]);
CurlExecutor::prettyPrint($result);
?>


If your credentials are fine below output will back from PayWay end:


Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [clientNumber] => T52943
            [clientName] => Pritom
            [keyName] => T10487_PUB...bzq
            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => single-use-tokens
                            [href] => https://api.payway.com.au/rest/v1/single-use-tokens
                        )

                    [1] => stdClass Object
                        (
                            [rel] => surcharges
                            [href] => https://api.payway.com.au/rest/v1/surcharges
                        )

                    [2] => stdClass Object
                        (
                            [rel] => help
                            [href] => https://www.payway.com.au/docs/rest.html#resources
                        )

                )

        )

)
If you have error in your public key below output will be generated:


Array
(
    [code] => 401
    [response] => stdClass Object
        (
            [message] => The "Authorization" header contained an unknown API key. You must send your API key as the basic authentication username.
            [links] => Array
                (
                    [0] => stdClass Object
                        (
                            [rel] => help
                            [href] => https://www.payway.com.au/docs/rest.html#authentication
                        )

                )

        )

)