Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
developer:sample_codes:php_xml_subscriptions [2017/11/01 14:33]
tleite
developer:sample_codes:php_xml_subscriptions [2017/11/07 10:55] (current)
Line 1: Line 1:
 +====== PHP XML Subscriptions ======
  
 +The sample code below requires the **{{:​developer:​sample_codes:​gateway_xml_php_api.zip|PHP XML API}}**.\\
 +
 +**Settings file (%gatewaySampleFileName_account.inc):​**
 +
 +<file php %gatewaySampleFileName_account.inc>​
 +
 +<?php
 + 
 +# These values are used to identify and validate the account that you are using. They are mandatory.
 +$gateway = '';​ #​ This is the %Gateway payments gateway that you should use, assigned to the site by %Gateway.
 +$terminalId = '';​ #​ This is the Terminal ID assigned to the site by %Gateway.
 +$currency = '';​ #​ This is the 3 digit ISO currency code for the above Terminal ID.
 +$secret = '';​ #​ This shared secret is used when generating the hash validation strings. ​
 + # It must be set exactly as it is in the %Gateway %SelfCare system.
 +$testAccount = true;
 + 
 +# These are used only in the case where the response hash is incorrect, which should
 +# never happen in the live environment unless someone is attempting fraud.
 +$adminEmail = '';​
 +$adminPhone = '';​
 + 
 +?>
 +
 +</​file>​
 +----
 +\\
 +
 +**Subscription registration:​**
 +
 +<file php %gatewaySampleFileName_subscription_registration.php>​
 +
 +<?php
 +
 +require('​%gatewaySampleFileName_account.inc'​);​
 +require('​gateway_tps_xml.php'​);​
 + 
 +# These values are specific to the cardholder.
 +$subscriptionMerchantRef = '';​ #​ Unique merchant identifier for the subscription. Length is limited to 48 chars.
 +$storedSubscriptionMerchantRef = '';​ #​ Merchant reference for the Stored Subscription under which this Subscription is to be created.
 +$secureCardMerchantRef = '';​ #​ Merchant reference for the SecureCard entry that you want to use to set up the subscription.
 +$subscriptionStartDate = '';​ #​ Date on which the subscription should start (setup payment is processed immediately,​ and does not obey this). Format: DD-MM-YYYY.
 +
 +# These are all optiona fields
 +$endDate = '';​ #​ (optional) set an end date for the subscription. ​ Format: DD-MM-YYYY.
 +$eDCCDecision = '';​ #​ (optional) if eDCC was offered and accepted, you should set this to '​Y'​.
 +
 +$recurringAmount = '';​ #​
 +$initialAmount = '';​ #​
 +$periodType = '';​
 +# Set up the stored subscription addition object
 +$subreg = new XmlSubscriptionRegRequest($subscriptionMerchantRef,​$terminalId,​$storedSubscriptionMerchantRef,​$secureCardMerchantRef,​$subscriptionStartDate);​
 + if($name != ""​ || $description != ""​ || $periodType != ""​ || $length != ""​ || $type != ""​ || $onUpdate != ""​ || $onDelete != ""​) $subreg->​SetNewStoredSubscriptionValues($name,​ $description,​ $periodType,​ $length, $currency, $recurringAmount,​ $initialAmount,​ $type, $onUpdate, $onDelete);
 + else if($recurringAmount != ""​ || $initialAmount != ""​) $subreg->​SetSubscriptionAmounts($recurringAmount,​ $initialAmount);​
 + if($endDate != ""​) $subreg->​SetEndDate($endDate);​
 + if($eDCCDecision != ""​) $subreg->​EDCCDecision($eDCCDecision);​
 + 
 +$response = $subreg->​ProcessRequestToGateway($secret,​$testMode,​$gateway);​
 + 
 +if($response->​IsError())echo 'AN ERROR OCCURED, Subscription not created. Error details: ' . $response->​ErrorString();​
 +else {
 + $expectedResponseHash = md5($terminalId.$response->​MerchantReference().$response->​DateTime().$secret);​
 + $merchantReference =$response->​MerchantReference();​
 + if($expectedResponseHash != $response->​Hash()) {
 + echo '​SUBSCRIPTION REGISTRATION FAILED: INVALID RESPONSE HASH. Please contact <a href="​mailto:'​ . $adminEmail . '">'​ . $adminEmail . '</​a>​ or call ' . $adminPhone . ' to clarify if your card will be billed.';​
 + if(isset($merchantReference))echo '​Please quote %Gateway Terminal ID: ' . $terminalId . ', and Subscription Merchant Reference: ' . $response->​MerchantReference() . ' when mailling or calling.';​
 + } else echo "​Subscription successfully setup and setup payment processed succesfully.";​
 +}
 + 
 +?>
 +
 +</​file>​
 +----
 +\\
 +
 +**Subscription update:**
 +
 +<file php %gatewaySampleFileName_subscription_update.php>​
 +
 +<?php
 +
 +require('​%gatewaySampleFileName_account.inc'​);​
 +require('​gateway_tps_xml.php'​);​
 +
 +# These values are specific to the cardholder.
 +$subscriptionMerchantRef = '';​ #​ Merchant Reference of the subscription to be updated
 +$storedSubscriptionMerchantRef = '';​ #​ Merchant reference for the Stored Subscription under which this Subscription is to be created.
 +$secureCardMerchantRef = '';​ #​ Merchant reference for the SecureCard entry that you want to use to set up the subscription.
 +$subscriptionStartDate = '';​ #​ Date on which the subscription should start (setup payment is processed immediately,​ and does not obey this).
 +$terminalId = '';​
 +$recurringAmount = '';​
 +# Set up the stored subscription update object
 +$subupd = new XmlSubscriptionUpdRequest($subscriptionMerchantRef,​$terminalId,​$secureCardMerchantRef);​
 + if($name != ""​) $subupd->​SetSubName($name);​
 + if($description != ""​) $subupd->​SetDescription($description);​
 + if($periodType != ""​) $subupd->​SetPeriodType($periodType);​
 + if($length != ""​) $subupd->​SetLength($length);​
 + if($recurringAmount != ""​) $subupd->​SetRecurringAmount($recurringAmount);​
 + if($type != ""​) $subupd->​SetSubType($type);​
 + if($startDate != ""​) $subupd->​SetStartDate($startDate);​
 + if($endDate != ""​) $subupd->​SetEndDate($endDate);​
 + if($eDCCDecision != ""​) $subupd->​EDCCDecision($eDCCDecision);​
 + 
 +$response = $subupd->​ProcessRequestToGateway($secret,​$testMode,​$gateway);​
 + 
 +if($response->​IsError()) {
 +
 + echo 'AN ERROR OCCURED, Subscription not updated. Error details: ' . $response->​ErrorString();​
 +
 +}
 +else {
 + $expectedResponseHash = md5($terminalId.$response->​MerchantReference().$response->​DateTime().$secret);​
 + if($expectedResponseHash != $response->​Hash()) {
 + echo '​SUBSCRIPTION UPDATE FAILED: INVALID RESPONSE HASH. Please contact <a href="​mailto:'​ . $adminEmail . '">'​ . $adminEmail . '</​a>​ or call ' . $adminPhone . ' to clarify if your card will be billed.';​
 + $merchantRef = $response->​MerchantReference();​
 + if(isset($merchantRef)) {
 + echo '​Please quote %Gateway Terminal ID: ' . $terminalId . ', and Subscription Merchant Reference: ' . $response->​MerchantReference() . ' when mailling or calling.';​
 + }
 + } else echo "​Subscription successfully updated.";​
 +
 +}
 + 
 +?>
 +
 +</​file>​
 +----
 +\\
 +
 +**Subscription deletion:**
 +
 +<file php %gatewaySampleFileName_subscription_delete.php>​
 +
 +<?php
 +
 +require('​%gatewaySampleFileName_account.inc'​);​
 +require('​gateway_tps_xml.php'​);​
 +
 +# These values are specific to the cardholder.
 +$subscriptionMerchantRef = '';​ #​ Merchant Reference of the subscription to be deleted
 +
 +# Set up the stored subscription update object
 +$subdel = new XmlSubscriptionDelRequest($subscriptionMerchantRef,​$terminalId);​
 +$response = $subdel->​ProcessRequestToGateway($secret,​$testMode,​$gateway);​
 + 
 +if($response->​IsError())
 +{
 + echo 'AN ERROR OCCURED, Subscription not deleted. Error details: ' . $response->​ErrorString();​
 +}
 +else {
 + $expectedResponseHash = md5($terminalId.$response->​MerchantReference().$response->​DateTime().$secret);​
 + if($expectedResponseHash != $response->​Hash()) {
 + $merchantRef = $response->​MerchantReference();​
 + echo '​SUBSCRIPTION DELETION FAILED: INVALID RESPONSE HASH. Please contact <a href="​mailto:'​ . $adminEmail . '">'​ . $adminEmail . '</​a>​ or call ' . $adminPhone . ' to clarify if your card will be billed.';​
 + if(isset($merchantRef)) {
 + echo '​Please quote %Gateway Terminal ID: ' . $terminalId . ', and Subscription Merchant Reference: ' . $merchantRef . ' when mailling or calling.';​
 + }
 +
 + else {
 + echo "​Subscription successfully deleted.";​
 + }
 +}
 + 
 +?>
 +
 +</​file>​
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International