Saturday, June 10, 2017

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.


3 comments:

  1. I have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. dlrcollectionagency.com

    ReplyDelete
  2. Quite a beautiful website. I built mine and i was looking for some design ideas and your website gave me some. Did you develop the website alone? Slice invitation code

    ReplyDelete
  3. You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! Cool roblox usernames

    ReplyDelete