Skip to content

Commit

Permalink
sample - Add exception
Browse files Browse the repository at this point in the history
  • Loading branch information
yidas committed Oct 7, 2019
1 parent db7881f commit a9d997f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
27 changes: 13 additions & 14 deletions sample/confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,25 @@
if ($order['transactionId'] != $transactionId) {
die("<script>alert('TransactionId doesn\'t match');location.href='./index.php';</script>");
}
// var_dump($order);exit;

// Online Confirm API
$response = $linePay->confirm($order['transactionId'], [
'amount' => (integer) $order['params']['amount'],
'currency' => $order['params']['currency'],
]);
try {

$response = $linePay->confirm($order['transactionId'], [
'amount' => (integer) $order['params']['amount'],
'currency' => $order['params']['currency'],
]);

} catch (\yidas\linePay\exception\ConnectException $e) {

// Implement recheck process
die("Confirm API timeout! A recheck mechanism should be implemented.");
}

// Save error info if confirm fails
if (!$response->isSuccessful()) {
$_SESSION['linePayOrder']['confirmCode'] = $response['returnCode'];
$_SESSION['linePayOrder']['confirmMessage'] = $response['returnMessage'];
}

// Use Details API to confirm the transaction (Details API verification is stable then Confirm API)
$response = $linePay->details([
'transactionId' => [$order['transactionId']],
]);

// Check the transaction
if (!isset($response["info"]) || $response["info"][0]['transactionId'] != $transactionId) {
$_SESSION['linePayOrder']['isSuccessful'] = false;
die("<script>alert('Refund Failed\\nErrorCode: {$_SESSION['linePayOrder']['confirmCode']}\\nErrorMessage: {$_SESSION['linePayOrder']['confirmMessage']}');location.href='{$successUrl}';</script>");
}
Expand Down
41 changes: 25 additions & 16 deletions sample/refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,31 @@
die("<script>alert('TransactionId doesn\'t match');location.href='./index.php';</script>");
}

// Online Refund API
$refundParams = ($_GET['amount']!="") ? ['refundAmount' => (integer) $_GET['amount']] : null;
$response = $linePay->refund($order['transactionId'], $refundParams);

// Save error info if confirm fails
if (!$response->isSuccessful()) {
die("<script>alert('Refund Failed\\nErrorCode: {$response['returnCode']}\\nErrorMessage: {$response['returnMessage']}');location.href='{$successUrl}';</script>");
}

// Use Details API to confirm the transaction (Details API verification is stable then Confirm API)
$response = $linePay->details([
'transactionId' => [$order['transactionId']],
]);
// Check the transaction
if (!isset($response["info"][0]['refundList']) || $response["info"][0]['transactionId'] != $transactionId) {
die("<script>alert('Refund Failed');location.href='{$successUrl}';</script>");
// API
try {

// Online Refund API
$refundParams = ($_GET['amount']!="") ? ['refundAmount' => (integer) $_GET['amount']] : null;
$response = $linePay->refund($order['transactionId'], $refundParams);

// Save error info if confirm fails
if (!$response->isSuccessful()) {
die("<script>alert('Refund Failed\\nErrorCode: {$response['returnCode']}\\nErrorMessage: {$response['returnMessage']}');location.href='{$successUrl}';</script>");
}

// Use Details API to confirm the transaction and get refund detail info
$response = $linePay->details([
'transactionId' => [$order['transactionId']],
]);
// Check the transaction
if (!isset($response["info"][0]['refundList']) || $response["info"][0]['transactionId'] != $transactionId) {
die("<script>alert('Refund Failed');location.href='{$successUrl}';</script>");
}

} catch (\yidas\linePay\exception\ConnectException $e) {

// Implement recheck process
die("Refund/Details API timeout! A recheck mechanism should be implemented.");
}

// Code for saving the successful order into your application database...
Expand Down

0 comments on commit a9d997f

Please sign in to comment.