From f9e4c48db48e106de1bea407163ab1302a32ce9e Mon Sep 17 00:00:00 2001 From: ao Date: Wed, 4 Mar 2015 09:45:03 +0000 Subject: [PATCH] v1.4 --- LICENSE | 0 examples/3ds_redirect.php | 8 +++- examples/cancel_authorised_form.php | 17 +++++++ examples/cancel_authorised_order.php | 33 ++++++++++++++ examples/capture_authorised_form.php | 21 +++++++++ examples/capture_authorised_order.php | 36 +++++++++++++++ examples/create_order.php | 12 +++-- examples/get_stored_cards.php | 6 ++- examples/header.php | 65 ++++++++++++++------------- examples/index.php | 7 ++- examples/partial_refund.php | 0 examples/partial_refund_order.php | 12 +++-- examples/refund.php | 0 examples/refund_order.php | 6 ++- examples/stored_cards.php | 0 lib/JSON.php | 0 lib/worldpay.php | 44 ++++++++++++++++-- readme.md | 2 +- 18 files changed, 218 insertions(+), 51 deletions(-) mode change 100755 => 100644 LICENSE mode change 100755 => 100644 examples/3ds_redirect.php create mode 100644 examples/cancel_authorised_form.php create mode 100644 examples/cancel_authorised_order.php create mode 100644 examples/capture_authorised_form.php create mode 100644 examples/capture_authorised_order.php mode change 100755 => 100644 examples/create_order.php mode change 100755 => 100644 examples/get_stored_cards.php mode change 100755 => 100644 examples/header.php mode change 100755 => 100644 examples/index.php mode change 100755 => 100644 examples/partial_refund.php mode change 100755 => 100644 examples/partial_refund_order.php mode change 100755 => 100644 examples/refund.php mode change 100755 => 100644 examples/refund_order.php mode change 100755 => 100644 examples/stored_cards.php mode change 100755 => 100644 lib/JSON.php mode change 100755 => 100644 lib/worldpay.php mode change 100755 => 100644 readme.md diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/examples/3ds_redirect.php b/examples/3ds_redirect.php old mode 100755 new mode 100644 index 0dce10d..1440b29 --- a/examples/3ds_redirect.php +++ b/examples/3ds_redirect.php @@ -2,7 +2,7 @@ disableSSLCheck(true); +include('header.php'); + try { $response = $worldpay->authorise3DSOrder($_SESSION['orderCode'], $_POST['PaRes']); @@ -21,11 +23,13 @@ } else { echo 'There was a problem authorising 3DS order
'; } -} catch (WorldpayException $e) { // PHP 5.2 - Change to Exception, only $e->getMessage() is available +} catch (WorldpayException $e) { // PHP 5.3+ // Worldpay has thrown an exception echo 'Error code: ' . $e->getCustomCode() . '
HTTP status code:' . $e->getHttpStatusCode() . '
Error description: ' . $e->getDescription() . '
Error message: ' . $e->getMessage() . '
' . 'PaRes: ' . print_r($_POST, true) . '
'; +} catch (Exception $e) { // PHP 5.2 + echo 'Error message: '. $e->getMessage(); } diff --git a/examples/cancel_authorised_form.php b/examples/cancel_authorised_form.php new file mode 100644 index 0000000..48312f5 --- /dev/null +++ b/examples/cancel_authorised_form.php @@ -0,0 +1,17 @@ + +

PHP Library Cancel Authorised Order Example

+
+
+
+ + +
+ +
+ + + + + diff --git a/examples/cancel_authorised_order.php b/examples/cancel_authorised_order.php new file mode 100644 index 0000000..d0a0f6b --- /dev/null +++ b/examples/cancel_authorised_order.php @@ -0,0 +1,33 @@ + +disableSSLCheck(true); + +$worldpayOrderCode = $_POST['orderCode']; + +include("header.php"); + +// Try catch +try { + // Cancel the authorised order using the Worldpay order code + $worldpay->cancelAuthorisedOrder($worldpayOrderCode); + echo 'Authorised order '.$worldpayOrderCode.' + has been cancelled'; +} catch (WorldpayException $e) { // PHP 5.3+ + // Worldpay has thrown an exception + echo 'Error code: ' . $e->getCustomCode() . '
+ HTTP status code:' . $e->getHttpStatusCode() . '
+ Error description: ' . $e->getDescription() . '
+ Error message: ' . $e->getMessage(); +} catch (Exception $e) { // PHP 5.2 + echo 'Error message: '. $e->getMessage(); +} diff --git a/examples/capture_authorised_form.php b/examples/capture_authorised_form.php new file mode 100644 index 0000000..97a0fb7 --- /dev/null +++ b/examples/capture_authorised_form.php @@ -0,0 +1,21 @@ + +

PHP Library Capture Authorised Order Example

+
+
+
+ + +
+
+ + +
+ +
+ + + diff --git a/examples/capture_authorised_order.php b/examples/capture_authorised_order.php new file mode 100644 index 0000000..afc4318 --- /dev/null +++ b/examples/capture_authorised_order.php @@ -0,0 +1,36 @@ + +disableSSLCheck(true); + +$worldpayOrderCode = $_POST['orderCode']; +$amount = $_POST['amount']; + +include("header.php"); + +// Try catch +try { + // Capture the authorised order using the Worldpay order code + $worldpay->captureAuthorisedOrder($worldpayOrderCode, $amount*100); + $response = 'Authorised order '.$worldpayOrderCode.' has been captured for '; + $response .= (!empty($amount)) ? ''. $amount .'' : 'the full amount'; + echo $response; + +} catch (WorldpayException $e) { // PHP 5.3+ + // Worldpay has thrown an exception + echo 'Error code: ' . $e->getCustomCode() . '
+ HTTP status code:' . $e->getHttpStatusCode() . '
+ Error description: ' . $e->getDescription() . '
+ Error message: ' . $e->getMessage(); +} catch (Exception $e) { // PHP 5.2 + echo 'Error message: '. $e->getMessage(); +} diff --git a/examples/create_order.php b/examples/create_order.php old mode 100755 new mode 100644 index 09c90ce..47f9c21 --- a/examples/create_order.php +++ b/examples/create_order.php @@ -1,7 +1,7 @@ $_POST['description'], // Order description of your choice 'amount' => $amount*100, // Amount in pence 'is3DSOrder' => $_3ds, // 3DS + 'authoriseOnly' => $authoriseOnly, 'currencyCode' => $_POST['currency'], // Currency code 'name' => ($_3ds) ? '3D' : $name, // Customer name 'billingAddress' => $billing_address, // Billing address array @@ -44,8 +46,8 @@ ), 'customerOrderCode' => 'A123' // Order code of your choice )); - - if ($response['paymentStatus'] === 'SUCCESS') { + + if ($response['paymentStatus'] === 'SUCCESS' || $response['paymentStatus'] === 'AUTHORIZED') { // Create order was successful! $worldpayOrderCode = $response['orderCode']; echo '

Order Code: ' . $worldpayOrderCode . '

'; @@ -73,10 +75,12 @@ echo '

' . $response['paymentStatus'] . '

'; throw new WorldpayException(print_r($response, true)); } -} catch (WorldpayException $e) { // PHP 5.2 - Change to Exception, only $e->getMessage() is available +} catch (WorldpayException $e) { // PHP 5.3+ // Worldpay has thrown an exception echo 'Error code: ' . $e->getCustomCode() . '
HTTP status code:' . $e->getHttpStatusCode() . '
Error description: ' . $e->getDescription() . '
Error message: ' . $e->getMessage(); +} catch (Exception $e) { // PHP 5.2 + echo 'Error message: '. $e->getMessage(); } diff --git a/examples/get_stored_cards.php b/examples/get_stored_cards.php old mode 100755 new mode 100644 index 89e7ce8..2535f6d --- a/examples/get_stored_cards.php +++ b/examples/get_stored_cards.php @@ -1,7 +1,7 @@ Masked Card Number: ' . $cardDetails['maskedCardNumber'] . '

'; echo '
' . print_r($cardDetails, true). '
'; -} catch (WorldpayException $e) { // PHP 5.2 - Change to Exception, only $e->getMessage() is available +} catch (WorldpayException $e) { // PHP 5.3+ echo 'Error code: ' . $e->getCustomCode() . '
HTTP status code:' . $e->getHttpStatusCode() . '
Error description: ' . $e->getDescription() . '
Error message: ' . $e->getMessage(); +} catch (Exception $e) { // PHP 5.2 + echo 'Error message: '. $e->getMessage(); } diff --git a/examples/header.php b/examples/header.php old mode 100755 new mode 100644 index 4d9ba92..274d238 --- a/examples/header.php +++ b/examples/header.php @@ -2,7 +2,7 @@ PHP Library - + + .payment-errors { + font-size: 20px; + font-weight: bold; + text-align: center; + color: red; + padding: 20px; + margin-bottom: 20px; + } + .token { + padding-top:20px; + } + #top-nav { + list-style: none; + text-align: center; + } + #top-nav li { + display:inline-block; + } + #top-nav li a { + text-decoration: none; + color:blue; + padding-left: 10px; + } +
- + diff --git a/examples/index.php b/examples/index.php old mode 100755 new mode 100644 index 5bdb7d0..26d9e26 --- a/examples/index.php +++ b/examples/index.php @@ -1,6 +1,6 @@ - +

PHP Library Create Order Example

@@ -132,6 +132,11 @@
+
+ + +
+ diff --git a/examples/partial_refund.php b/examples/partial_refund.php old mode 100755 new mode 100644 diff --git a/examples/partial_refund_order.php b/examples/partial_refund_order.php old mode 100755 new mode 100644 index 2649d06..9ebe990 --- a/examples/partial_refund_order.php +++ b/examples/partial_refund_order.php @@ -1,7 +1,7 @@ refundOrder($worldpayOrderCode, $amount*100); - echo 'Order '.$worldpayOrderCode.' - has been refunded for '. $amount .''; -} catch (WorldpayException $e) { // PHP 5.2 - Change to Exception, only $e->getMessage() is available + $response = 'Order '.$worldpayOrderCode.' has been refunded for '; + $response .= (!empty($amount)) ? ''. $amount .'' : 'the full amount'; + echo $response; + +} catch (WorldpayException $e) { // PHP 5.3+ // Worldpay has thrown an exception echo 'Error code: ' . $e->getCustomCode() . '
HTTP status code:' . $e->getHttpStatusCode() . '
Error description: ' . $e->getDescription() . '
Error message: ' . $e->getMessage(); +} catch (Exception $e) { // PHP 5.2 + echo 'Error message: '. $e->getMessage(); } diff --git a/examples/refund.php b/examples/refund.php old mode 100755 new mode 100644 diff --git a/examples/refund_order.php b/examples/refund_order.php old mode 100755 new mode 100644 index b3d189e..ddb3da8 --- a/examples/refund_order.php +++ b/examples/refund_order.php @@ -1,7 +1,7 @@ refundOrder($worldpayOrderCode); echo 'Order '.$worldpayOrderCode.' has been refunded!'; -} catch (WorldpayException $e) { // PHP 5.2 - Change to Exception, only $e->getMessage() is available +} catch (WorldpayException $e) { // PHP 5.3+ // Worldpay has thrown an exception echo 'Error code: ' . $e->getCustomCode() . '
HTTP status code:' . $e->getHttpStatusCode() . '
Error description: ' . $e->getDescription() . '
Error message: ' . $e->getMessage(); +} catch (Exception $e) { // PHP 5.2 + echo 'Error message: '. $e->getMessage(); } diff --git a/examples/stored_cards.php b/examples/stored_cards.php old mode 100755 new mode 100644 diff --git a/lib/JSON.php b/lib/JSON.php old mode 100755 new mode 100644 diff --git a/lib/worldpay.php b/lib/worldpay.php old mode 100755 new mode 100644 index 8918b0e..c8e019e --- a/lib/worldpay.php +++ b/lib/worldpay.php @@ -1,7 +1,7 @@ array( 'ordercode' => 'No order code entered' ), + 'capture' => array( + 'ordercode' => 'No order code entered' + ), 'json' => 'JSON could not be decoded', 'key' => 'Please enter your service key', 'sslerror' => 'Worldpay SSL certificate could not be validated' @@ -171,7 +174,7 @@ private function sendRequest($action, $json = false, $expectResponse = false, $m } $clientUserAgent = 'os.name=' . php_uname('s') . ';os.version=' . php_uname('r') . ';os.arch=' . - $arch . ';lang.version='. phpversion() . ';lib.version=1.3;' . + $arch . ';lang.version='. phpversion() . ';lib.version=;' . 'api.version=v1;lang=php;owner=worldpay'; curl_setopt( @@ -267,6 +270,7 @@ public function createOrder($order = array()) 'customerIdentifiers' => null, 'billingAddress' => null, 'is3DSOrder' => false, + 'authoriseOnly' => false, 'redirectURL' => false ); @@ -280,6 +284,7 @@ public function createOrder($order = array()) "currencyCode" => $order['currencyCode'], "name" => $order['name'], "orderType" => $order['orderType'], + "authorizeOnly" => ($order['authoriseOnly']) ? true : false, "billingAddress" => $order['billingAddress'], "customerOrderCode" => $order['customerOrderCode'], "customerIdentifiers" => $order['customerIdentifiers'] @@ -322,6 +327,39 @@ public function authorise3DSOrder($orderCode, $responseCode) return $this->sendRequest('orders/' . $orderCode, $json, true, 'PUT'); } + /** + * Capture Authorized Worldpay Order + * @param string $orderCode + * @param string $amount + * */ + public function captureAuthorisedOrder($orderCode = false, $amount = null) + { + if (empty($orderCode) || !is_string($orderCode)) { + self::onError('ip', self::$errors['capture']['ordercode']); + } + + if (!empty($amount) && is_int($amount)) { + $json = json_encode(array('captureAmount'=>"{$amount}")); + } else { + $json = false; + } + + $this->sendRequest('orders/' . $orderCode . '/capture', $json, !!$json); + } + + /** + * Cancel Authorized Worldpay Order + * @param string $orderCode + * */ + public function cancelAuthorisedOrder($orderCode = false) + { + if (empty($orderCode) || !is_string($orderCode)) { + self::onError('ip', self::$errors['capture']['ordercode']); + } + + $this->sendRequest('orders/' . $orderCode, false, false, 'DELETE'); + } + /** * Refund Worldpay order * @param bool $orderCode @@ -339,7 +377,7 @@ public function refundOrder($orderCode = false, $amount = null) $json = false; } - $this->sendRequest('orders/' . $orderCode . '/refund', $json, !!$json); + $this->sendRequest('orders/' . $orderCode . '/refund', $json, false); } /** diff --git a/readme.md b/readme.md old mode 100755 new mode 100644 index 6b4839e..65023e1 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# Worldpay PHP Library 1.3 +# Worldpay PHP Library #### Documentation https://online.worldpay.com/docs