PHP code example of jsj051 / omnipay-mycard

1. Go to this page and download the library: Download jsj051/omnipay-mycard library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jsj051 / omnipay-mycard example snippets


// Initialize
$config = [
    'appId'  => 'MyCard_ServiceId',
    'appKey' => 'MyCard_Key'
];
$gateway = Omnipay::create('MyCard');
$gateway->initialize($config);

// Send purchase request
$response = $gateway->purchase(
    [
        'amount'        => '1.00',
        'currency'      => 'TWD',
        'description'   => 'product description',
        'transactionId' => mt_rand(100000, 999999),
    ]
)->send();

// Process response
if ($response->isRedirect()) {
    // doing something here
    // $token = $response->getToken();
    // $data = $response->getData();
    // $transactionReference = $response->getTransactionReference();
    $response->redirect();
}
elseif ($response->isSuccessful()) {
    // doing something here
    print_r($response);
}
else {
    echo $response->getMessage();
}


// Notify
$config = [
    'appId'  => 'MyCard_ServiceId',
    'appKey' => 'MyCard_Key'
];
$gateway = Omnipay::create('MyCard');
$gateway->initialize($config);
try {
    $response = $gateway->acceptNotification()->send();

    // set token (which saved when send a purchase @see Usage For Purchase)
    // $transactionId = $response->getTransactionId();
    $response->setToken('MyCard_AuthCode');
    // confirm
    $response->confirm();
    if ($response->isSuccessful()) {
        // doing something here
        // save $response->getData()['confirmData'] for further compare
        // $data = $response->getData();
    }
} catch (\Exception $e) {
    // failed logs
}

$gateway = Omnipay::create('MyCard');
$gateway->initialize($config);
$response = $gateway->fetchTransaction(['token' => 'MyCard_AuthCode'])->send();
// further functions below
$response->isSuccessful();
$response->getTransactionId();
$response->getAmount();
$response->getCurrency();
$response->getCardNumber();     // card number
$response->getMessage();        // message response from MyCard query api
$response->getData();           // output RAW data

$compare = $gateway->compareTransaction();

// Get Params, Exp: ["card"=>"MC123456"] or ["startTime"=>1500000000,"endTime"=>1560000000];
$params = $compare->getParams();

// Get data from database with the $params above
$data = [
    [
        'type'                 => 'INGAME',         // INGAME, COSTPOINT Or Something Else
        'transactionId'        => '12345678',       // My Transaction Id
        'transactionReference' => 'MC973924',       // MyCard Transaction Id
        'card'                 => 'card number',    // Card Number Or Something Else
        'amount'               => '50.00',          // Amount
        'currency'             => 'TWD',            // Currency
        'account'              => 'user123',        // User Id
        'time'                 => 1500000000,       // Timestamp
    ],
    // ... more
];

// Output data
$compare->setData($data)->send();