Showing posts with label payment. Show all posts
Showing posts with label payment. Show all posts

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.


Saturday, May 27, 2017

Stripe Payment API: Create Charge Or Payment

You need a Token (Saved credit card instance) to create a Payment which is known as Charge in Stripe API. Creating Charge in Stripe is equal to creating a Payment. 

Payment API documentation:
https://stripe.com/docs/api#charges

<?php
function createCharge()
{
    $token = CreateToken::create();

    if ($token["code"] != 200) {
        StripeCharge::prettyPrint($token["response"]);
        exit;
    }

    $params = array(
        "amount" => "200",
        "currency" => "aud",
        "source" => $token["response"]->id,
        "description" => "Some description against charge/payment"
    );
    $create = StripeCharge::create($params);
    StripeCharge::prettyPrint($create);
}
createCharge();

class StripeCharge {
    private static $key = "sk_test_...";

    static function create($params)
    {
        $url = "https://api.stripe.com/v1/charges";

        $headers[] = "Authorization: Bearer " . self::$key;
        $headers[] = "Content-Type: application/x-www-form-urlencoded";

        return makeCurlCall($url, "POST", null, $params, $headers);
    }

    static function prettyPrint($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }
}

Output below:



Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => ch_1ANxxAFIwfarG3vBDqR8ROkg
            [object] => charge
            [amount] => 200
            [amount_refunded] => 0
            [application] =>
            [application_fee] =>
            [balance_transaction] => txn_1ANxxAFIwfarG3vB3wrLUKgn
            [captured] => 1
            [created] => 1495864748
            [currency] => aud
            [customer] =>
            [description] => Some description against charge/payment
            [destination] =>
            [dispute] =>
            [failure_code] =>
            [failure_message] =>
            [fraud_details] => stdClass Object
                (
                )

            [invoice] =>
            [livemode] =>
            [metadata] => stdClass Object
                (
                )

            [on_behalf_of] =>
            [order] =>
            [outcome] => stdClass Object
                (
                    [network_status] => approved_by_network
                    [reason] =>
                    [risk_level] => normal
                    [seller_message] => Payment complete.
                    [type] => authorized
                )

            [paid] => 1
            [receipt_email] =>
            [receipt_number] =>
            [refunded] =>
            [refunds] => stdClass Object
                (
                    [object] => list
                    [data] => Array
                        (
                        )

                    [has_more] =>
                    [total_count] => 0
                    [url] => /v1/charges/ch_1ANxxAFIwfarG3vBDqR8ROkg/refunds
                )

            [review] =>
            [shipping] =>
            [source] => stdClass Object
                (
                    [id] => card_1ANxx8FIwfarG3vBa0fmLGhT
                    [object] => card
                    [address_city] =>
                    [address_country] =>
                    [address_line1] =>
                    [address_line1_check] =>
                    [address_line2] =>
                    [address_state] =>
                    [address_zip] =>
                    [address_zip_check] =>
                    [brand] => Visa
                    [country] => US
                    [customer] =>
                    [cvc_check] => pass
                    [dynamic_last4] =>
                    [exp_month] => 12
                    [exp_year] => 2019
                    [fingerprint] => CjZNbbCtG5QSnuIS
                    [funding] => credit
                    [last4] => 4242
                    [metadata] => stdClass Object
                        (
                        )

                    [name] => Card Name
                    [tokenization_method] =>
                )

            [source_transfer] =>
            [statement_descriptor] =>
            [status] => succeeded
            [transfer_group] =>
        )

)

Wednesday, August 14, 2013

Php create/edit eWay token for further payment

<?php
function 
create_token_eway()
{
    
$testUrl "https://www.eway.com.au/gateway/ManagedPaymentService/test/managedCreditCardPayment.asmx";
    
$liveUrl "https://www.eway.com.au/gateway/ManagedPaymentService/managedCreditCardPayment.asmx";
  
    
$eWaySOAPActionURL    "https://www.eway.com.au/gateway/managedpayment";
    
$eWayCustomerId       "87654321";
    
/* test account */
    
$eWayCustomerEmail    "test@eway.com.au";
    
/* test email */
    
$eWayCustomerPassword "test123";
    
/* test password */
  
    
$updateCustomer false;
    
$customTag      "CreateCustomer";
    
/* For update customer: "UpdateCustomer". */
    /**
     * If you want to update existing customer do the following -
     */
    
if ($updateCustomer) {
        
$updateCustomerEWayId "9876543211000";
        
/* Already saved customer id. */
        
$customTag            "UpdateCustomer";
        
$updateCustomerEWayId "<managedCustomerID>"  

              $updateCustomerEWayId 
              "</managedCustomerID>";
    }
  
    
$directXML "<?xml version=\"1.0\" encoding=\"utf-8\"?>
        <soap12:Envelope 

            xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
            xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
            xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">
            <soap12:Header>
            <eWAYHeader xmlns=\"" 
$eWaySOAPActionURL "\">
                <eWAYCustomerID>" 
$eWayCustomerId "</eWAYCustomerID>
                <Username>" 
$eWayCustomerEmail "</Username>
                <Password>" 
$eWayCustomerPassword "</Password>
            </eWAYHeader>
        </soap12:Header>
          <soap12:Body>
            <" 
$customTag " xmlns=\"" $eWaySOAPActionURL "\">
                " 
$updateCustomerEWayId "
                <Title>Mr.</Title>
                <FirstName>Pritom</FirstName>
                <LastName>Kumar Mondal</LastName>
                <Address></Address>
                <Suburb></Suburb>
                <State></State>
                <Company>Khulna University</Company>
                <PostCode></PostCode>
                <Country>au</Country>
                <Email>pritomkucse@gmail.com</Email>
                <Fax></Fax>
                <Phone></Phone>
                <Mobile></Mobile>
                <CustomerRef>CSE-060238</CustomerRef>
                <JobDesc></JobDesc>
                <Comments></Comments>
                <URL></URL>
                <CCNumber>4444333322221111</CCNumber>
                <CCNameOnCard>Pritom K Mondal</CCNameOnCard>
                <CCExpiryMonth>12</CCExpiryMonth>
                <CCExpiryYear>15</CCExpiryYear>
            </" 
$customTag ">
        </soap12:Body>
        </soap12:Envelope>"
;
  
    
$result makeCurlCall(

    $testUrl/* CURL URL */ 
    "POST"/* CURL CALL METHOD */  
    array(
        
/* CURL HEADERS */
        
"Content-Type: text/xml; charset=utf-8",
        
"Accept: text/xml",
        
"Pragma: no-cache",
        
"SOAPAction: " $eWaySOAPActionURL "/" $customTag,
        
"Content_length: " strlen(trim($directXML))
    ), 

    null/* CURL GET PARAMETERS */  
    $directXML /* CURL POST PARAMETERS AS XML */ );
  
    if (
$result != null && isset($result["response"])) {
        echo 
$result["response"]; /* Result printed below */
    }
    die(
"");
}
 


/* makeCurlCall */ 
function makeCurlCall($url$method "GET"$headers null$gets null$posts null)
{
    
$ch curl_init();
    if (
$gets != null) {
        
$url .= "?" . (http_build_query($gets));
    }
    
curl_setopt($chCURLOPT_URL$url);
    
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  
    if (
$posts != null) {
        
curl_setopt($chCURLOPT_POSTFIELDS$posts);
    }
    if (
$method == "POST") {
        
curl_setopt($chCURLOPT_POSTtrue);
    } else if (
$method == "PUT") {
        
curl_setopt($chCURLOPT_CUSTOMREQUEST"PUT");
    } else if (
$method == "HEAD") {
        
curl_setopt($chCURLOPT_NOBODYtrue);
    }
    if (
$headers != null && is_array($headers)) {
        
curl_setopt($chCURLOPT_HTTPHEADER$headers);
    }
    
$response curl_exec($ch);
    
$code     curl_getinfo($chCURLINFO_HTTP_CODE);
  
    
curl_close($ch);
    return array(
        
"code" => $code,
        
"response" => $response
    
);
}
 

create_token_eway();?>

Output

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <CreateCustomerResponse  
         xmlns="https://www.eway.com.au/gateway/managedpayment">
         <CreateCustomerResult>9876543211000</CreateCustomerResult>
      </CreateCustomerResponse>
   </soap:Body>
</soap:Envelope>

Saturday, June 29, 2013

eWay transaction using php api direct payment

/* Pay using eWay */
function pay_using_eway()
{
    $testUrl = "https://www.eway.com.au/gateway_cvn/xmltest/testpage.asp";
    $liveUrl = "https://www.eway.com.au/gateway_cvn/xmlpayment.asp";
    $eWaySOAPActionURL = "https://www.eway.com.au/gateway/managedpayment";
    $eWayCustomerId = "87654321"; /* test account */
    $eWayTotalAmount = 100; /* 1$ = 100 cent */
    $directXML = "<ewaygateway>".
        "<ewayCustomerID>".$eWayCustomerId."</ewayCustomerID>".
        "<ewayTotalAmount>".$eWayTotalAmount."</ewayTotalAmount>".
        "<ewayCustomerFirstName></ewayCustomerFirstName>".
        "<ewayCustomerLastName></ewayCustomerLastName>".
        "<ewayCustomerEmail></ewayCustomerEmail>".
        "<ewayCustomerAddress></ewayCustomerAddress>".
        "<ewayCustomerPostcode></ewayCustomerPostcode>".
        "<ewayCustomerInvoiceDescription></ewayCustomerInvoiceDescription>".
        "<ewayCustomerInvoiceRef> Invoice Reference </ewayCustomerInvoiceRef>".
        "<ewayCardHoldersName>Card Holder Name</ewayCardHoldersName>".
        "<ewayCardNumber>4444333322221111</ewayCardNumber>".
        "<ewayCardExpiryMonth>01</ewayCardExpiryMonth>".
        "<ewayCardExpiryYear>2015</ewayCardExpiryYear>".
        "<ewayCVN>123</ewayCVN>".
        "<ewayTrxnNumber></ewayTrxnNumber>".
        "<ewayOption1></ewayOption1>".
        "<ewayOption2></ewayOption2>".
        "<ewayOption3></ewayOption3>".
    "</ewaygateway>";

    $result = makeCurlCall(
        $testUrl, /* CURL URL */
        "POST", /* CURL CALL METHOD */
        array( /* CURL HEADERS */
            "Content-Type: text/xml; charset=utf-8",
            "Accept: text/xml",
            "Pragma: no-cache",
            "SOAPAction: ".$eWaySOAPActionURL,
            "Content_length: ".strlen(trim($directXML))
        ),
        null, /* CURL GET PARAMETERS */
        $directXML /* CURL POST PARAMETERS AS XML */
    );

    if($result != null && isset($result["response"])) {
        $response = new SimpleXMLElement($result["response"]);
        $response = simpleXMLToArray($response);
        print_r2($response);
    }
    die("");
}

/* makeCurlCall */
function makeCurlCall($url, $method = "GET", $headers = null, $gets = null, $posts = null) {
    $ch = curl_init();
    if($gets != null)
    {
        $url.="?".(http_build_query($gets));
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    if($posts != null)
    {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
    }
    if($method == "POST") {
        curl_setopt($ch, CURLOPT_POST, true);
    } else if($method == "PUT") {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    } else if($method == "HEAD") {
        curl_setopt($ch, CURLOPT_NOBODY, true);
    }
    if($headers != null && is_array($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    $response = curl_exec($ch);
    $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

    curl_close($ch);
    return array(
        "code" => $code,
        "response" => $response
    );
}

/* Response */ 
Array
(
    [ewayTrxnStatus] => Array
        (
            [__cdata] => True
        )

    [ewayTrxnNumber] => Array
        (
            [__cdata] => 20466
        )

    [ewayTrxnReference] => Array
        (
            [__cdata] => 
        )

    [ewayTrxnOption1] => Array
        (
            [__cdata] => 
        )

    [ewayTrxnOption2] => Array
        (
            [__cdata] => 
        )

    [ewayTrxnOption3] => Array
        (
            [__cdata] => 
        )

    [ewayAuthCode] => Array
        (
            [__cdata] => 123456
        )

    [ewayReturnAmount] => Array
        (
            [__cdata] => 100
        )

    [ewayTrxnError] => Array
        (
            [__cdata] => 00,Transaction Approved(Test CVN Gateway)
        )

)
 
http://www.eway.com.au/developers/api/direct-payments