Skip to content

Commit b8bc80d

Browse files
author
akankari
committed
Move the Paypal sample codes to function
1 parent c0f7e0f commit b8bc80d

8 files changed

Lines changed: 394 additions & 333 deletions

PaypalExpressCheckout/authorization-and-capture-continue.php

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,62 @@
44
use net\authorize\api\controller as AnetController;
55
define("AUTHORIZENET_LOG_FILE", "phplog");
66

7-
// Put the values of the Payer ID and Ref. Transaction ID generated in Authorization and Capture
8-
$payerID="6ZSCSYG33VP8Q";
9-
$refTransId="2241708986";
10-
11-
// Common setup for API credentials (with PayPal compatible merchant credentials)
12-
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
13-
$merchantAuthentication->setName("5KP3u95bQpv");
14-
$merchantAuthentication->setTransactionKey("4Ktq966gC55GAX7S");
15-
16-
$payPalType=new AnetAPI\PayPalType();
17-
$payPalType->setPayerID($payerID);
18-
19-
$paymentOne = new AnetAPI\PaymentType();
20-
$paymentOne->setPayPal($payPalType);
21-
22-
// Create an authorize and capture continue transaction
23-
$transactionRequestType = new AnetAPI\TransactionRequestType();
24-
$transactionRequestType->setTransactionType( "authCaptureContinueTransaction");
25-
$transactionRequestType->setPayment($paymentOne);
26-
$transactionRequestType->setRefTransId($refTransId);
27-
28-
$request = new AnetAPI\CreateTransactionRequest();
29-
$request->setMerchantAuthentication($merchantAuthentication);
30-
$request->setTransactionRequest( $transactionRequestType);
31-
$controller = new AnetController\CreateTransactionController($request);
32-
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
33-
34-
if ($response != null)
35-
{
36-
$tresponse = $response->getTransactionResponse();
37-
if (($tresponse != null))
38-
{
39-
echo "Transaction Response...\n";
40-
echo "Received response code: ".$tresponse->getResponseCode()."\n";
41-
//Valid response codes: 1=Approved, 2=Declined, 3=Error, 5=Need Payer Consent
42-
echo "Secure acceptance URL: ".$tresponse->getSecureAcceptance()->getSecureAcceptanceUrl()."\n";
43-
echo "Transaction ID: ".$tresponse->getTransId()."\n";
44-
}
45-
else
46-
echo "NULL transactionResponse Error\n";
47-
$messages=$response->getMessages();
48-
if (($messages != null))
7+
function payPalAuthorizeCaptureContinue($refTransId, $payerID) {
8+
9+
// Common setup for API credentials (with PayPal compatible merchant credentials)
10+
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
11+
$merchantAuthentication->setName("5KP3u95bQpv");
12+
$merchantAuthentication->setTransactionKey("4Ktq966gC55GAX7S");
13+
14+
$payPalType=new AnetAPI\PayPalType();
15+
$payPalType->setPayerID($payerID);
16+
17+
$paymentOne = new AnetAPI\PaymentType();
18+
$paymentOne->setPayPal($payPalType);
19+
20+
// Create an authorize and capture continue transaction
21+
$transactionRequestType = new AnetAPI\TransactionRequestType();
22+
$transactionRequestType->setTransactionType( "authCaptureContinueTransaction");
23+
$transactionRequestType->setPayment($paymentOne);
24+
$transactionRequestType->setRefTransId($refTransId);
25+
26+
$request = new AnetAPI\CreateTransactionRequest();
27+
$request->setMerchantAuthentication($merchantAuthentication);
28+
$request->setTransactionRequest( $transactionRequestType);
29+
$controller = new AnetController\CreateTransactionController($request);
30+
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
31+
32+
if ($response != null)
4933
{
50-
echo "Messages...\n";
51-
echo "Result code: ".$messages->getResultCode()."\n";
52-
$message0=$messages->getMessage()[0];
53-
if($message0!=null)
54-
echo "Message: ".$message0->getCode().", ".$message0->getText()."\n";
34+
$tresponse = $response->getTransactionResponse();
35+
if (($tresponse != null))
36+
{
37+
echo "Transaction Response...\n";
38+
echo "Received response code: ".$tresponse->getResponseCode()."\n";
39+
//Valid response codes: 1=Approved, 2=Declined, 3=Error, 5=Need Payer Consent
40+
echo "Secure acceptance URL: ".$tresponse->getSecureAcceptance()->getSecureAcceptanceUrl()."\n";
41+
echo "Transaction ID: ".$tresponse->getTransId()."\n";
42+
}
43+
else
44+
echo "NULL transactionResponse Error\n";
45+
$messages=$response->getMessages();
46+
if (($messages != null))
47+
{
48+
echo "Messages...\n";
49+
echo "Result code: ".$messages->getResultCode()."\n";
50+
$message0=$messages->getMessage()[0];
51+
if($message0!=null)
52+
echo "Message: ".$message0->getCode().", ".$message0->getText()."\n";
53+
}
54+
else
55+
echo "NULL messages Error\n";
5556
}
5657
else
57-
echo "NULL messages Error\n";
58+
echo "NULL response Error\n";
59+
60+
return $response;
5861
}
59-
else
60-
echo "NULL response Error\n";
62+
63+
if(!defined(DONT_RUN_SAMPLES))
64+
payPalAuthorizeCaptureContinue("2241708986","6ZSCSYG33VP8Q");
6165
?>

PaypalExpressCheckout/authorization-and-capture.php

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,51 @@
44
use net\authorize\api\controller as AnetController;
55
define("AUTHORIZENET_LOG_FILE", "phplog");
66

7-
// Common setup for API credentials (with PayPal compatible merchant credentials)
8-
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
9-
$merchantAuthentication->setName("5KP3u95bQpv");
10-
$merchantAuthentication->setTransactionKey("4Ktq966gC55GAX7S");
11-
12-
$payPalType=new AnetAPI\PayPalType();
13-
$payPalType->setCancelUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
14-
$payPalType->setSuccessUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
15-
16-
$paymentOne = new AnetAPI\PaymentType();
17-
$paymentOne->setPayPal($payPalType);
18-
19-
// Create an authorize and capture transaction
20-
$transactionRequestType = new AnetAPI\TransactionRequestType();
21-
$transactionRequestType->setTransactionType( "authCaptureTransaction");
22-
$transactionRequestType->setPayment($paymentOne);
23-
$transactionRequestType->setAmount(151);
24-
25-
$request = new AnetAPI\CreateTransactionRequest();
26-
$request->setMerchantAuthentication($merchantAuthentication);
27-
$request->setTransactionRequest( $transactionRequestType);
28-
$controller = new AnetController\CreateTransactionController($request);
29-
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
30-
31-
if ($response != null)
32-
{
33-
$tresponse = $response->getTransactionResponse();
34-
if (($tresponse != null))
7+
function payPalAuthorizeCapture($amount) {
8+
9+
// Common setup for API credentials (with PayPal compatible merchant credentials)
10+
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
11+
$merchantAuthentication->setName("5KP3u95bQpv");
12+
$merchantAuthentication->setTransactionKey("4Ktq966gC55GAX7S");
13+
14+
$payPalType=new AnetAPI\PayPalType();
15+
$payPalType->setCancelUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
16+
$payPalType->setSuccessUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
17+
18+
$paymentOne = new AnetAPI\PaymentType();
19+
$paymentOne->setPayPal($payPalType);
20+
21+
// Create an authorize and capture transaction
22+
$transactionRequestType = new AnetAPI\TransactionRequestType();
23+
$transactionRequestType->setTransactionType( "authCaptureTransaction");
24+
$transactionRequestType->setPayment($paymentOne);
25+
$transactionRequestType->setAmount($amount);
26+
27+
$request = new AnetAPI\CreateTransactionRequest();
28+
$request->setMerchantAuthentication($merchantAuthentication);
29+
$request->setTransactionRequest( $transactionRequestType);
30+
$controller = new AnetController\CreateTransactionController($request);
31+
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
32+
33+
if ($response != null)
3534
{
36-
echo "Received response code: ".$tresponse->getResponseCode()."\n";
37-
//Valid response codes: 1=Approved, 2=Declined, 3=Error, 5=Need Payer Consent
38-
echo "Secure acceptance URL: ".$tresponse->getSecureAcceptance()->getSecureAcceptanceUrl()."\n";
39-
echo "Transaction ID: ".$tresponse->getTransId()."\n";
35+
$tresponse = $response->getTransactionResponse();
36+
if (($tresponse != null))
37+
{
38+
echo "Received response code: ".$tresponse->getResponseCode()."\n";
39+
//Valid response codes: 1=Approved, 2=Declined, 3=Error, 5=Need Payer Consent
40+
echo "Secure acceptance URL: ".$tresponse->getSecureAcceptance()->getSecureAcceptanceUrl()."\n";
41+
echo "Transaction ID: ".$tresponse->getTransId()."\n";
42+
}
43+
else
44+
echo "NULL transactionResponse Error\n";
4045
}
4146
else
42-
echo "NULL transactionResponse Error\n";
47+
echo "NULL response Error\n";
48+
49+
return $response;
4350
}
44-
else
45-
echo "NULL response Error\n";
51+
52+
if(!defined(DONT_RUN_SAMPLES))
53+
payPalAuthorizeCapture(12.12);
4654
?>

PaypalExpressCheckout/authorization-only-continued.php

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,65 @@
66

77
define("AUTHORIZENET_LOG_FILE", "phplog");
88

9-
echo "PayPal Authorize Only Continue Transaction\n";
10-
11-
// Common setup for API credentials
12-
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
13-
$merchantAuthentication->setName("5KP3u95bQpv");
14-
$merchantAuthentication->setTransactionKey("4Ktq966gC55GAX7S");
15-
$refId = 'ref' . time();
16-
17-
$paypal_type = new AnetAPI\PayPalType();
18-
$paypal_type->setPayerID("JJLRRB29QC7RU");
19-
$paypal_type->setSuccessUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
20-
$paypal_type->setCancelUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
21-
22-
$payment_type = new AnetAPI\PaymentType();
23-
$payment_type->setPayPal($paypal_type);
9+
function payPalAuthorizeOnlyContinue($transactionId, $payerId) {
10+
echo "PayPal Authorize Only Continue Transaction\n";
11+
12+
// Common setup for API credentials
13+
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
14+
$merchantAuthentication->setName("5KP3u95bQpv");
15+
$merchantAuthentication->setTransactionKey("4Ktq966gC55GAX7S");
16+
$refId = 'ref' . time();
2417

25-
//create a transaction
26-
$transactionRequestType = new AnetAPI\TransactionRequestType();
27-
$transactionRequestType->setTransactionType( "authOnlyContinueTransaction");
28-
$transactionRequestType->setRefTransId("2241711631");
29-
$transactionRequestType->setAmount(125.34);
30-
$transactionRequestType->setPayment($payment_type);
18+
$paypal_type = new AnetAPI\PayPalType();
19+
$paypal_type->setPayerID($payerId);
20+
$paypal_type->setSuccessUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
21+
$paypal_type->setCancelUrl("http://www.merchanteCommerceSite.com/Success/TC25262");
22+
23+
$payment_type = new AnetAPI\PaymentType();
24+
$payment_type->setPayPal($paypal_type);
3125

32-
$request = new AnetAPI\CreateTransactionRequest();
33-
$request->setMerchantAuthentication($merchantAuthentication);
34-
$request->setRefId( $refId);
35-
$request->setTransactionRequest( $transactionRequestType);
26+
//create a transaction
27+
$transactionRequestType = new AnetAPI\TransactionRequestType();
28+
$transactionRequestType->setTransactionType( "authOnlyContinueTransaction");
29+
$transactionRequestType->setRefTransId($transactionId);
30+
$transactionRequestType->setAmount(125.34);
31+
$transactionRequestType->setPayment($payment_type);
3632

37-
$controller = new AnetController\CreateTransactionController($request);
33+
$request = new AnetAPI\CreateTransactionRequest();
34+
$request->setMerchantAuthentication($merchantAuthentication);
35+
$request->setRefId( $refId);
36+
$request->setTransactionRequest( $transactionRequestType);
3837

39-
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
38+
$controller = new AnetController\CreateTransactionController($request);
4039

41-
if ($response != null)
42-
{
43-
$tresponse = $response->getTransactionResponse();
44-
45-
if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )
40+
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
41+
42+
if ($response != null)
4643
{
47-
//echo " RESULT CODE : " . $response->getResultCode() . "\n";
48-
echo " TRANS ID : " . $tresponse->getTransId() . "\n";
49-
echo "Payer ID : " . $tresponse->getSecureAcceptance()->getPayerID();
44+
$tresponse = $response->getTransactionResponse();
45+
46+
if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )
47+
{
48+
//echo " RESULT CODE : " . $response->getResultCode() . "\n";
49+
echo " TRANS ID : " . $tresponse->getTransId() . "\n";
50+
echo "Payer ID : " . $tresponse->getSecureAcceptance()->getPayerID();
51+
}
52+
else
53+
{
54+
//print_r($tresponse);
55+
echo "ERROR : " . $tresponse->getResponseCode() . "\n";
56+
}
57+
5058
}
5159
else
5260
{
53-
//print_r($tresponse);
54-
echo "ERROR : " . $tresponse->getResponseCode() . "\n";
61+
echo "No response returned";
5562
}
56-
57-
}
58-
else
59-
{
60-
echo "No response returned";
63+
64+
return $response;
6165
}
66+
67+
if(!defined(DONT_RUN_SAMPLES))
68+
payPalAuthorizeOnlyContinue("2241711631", "JJLRRB29QC7RU");
6269

6370
?>

0 commit comments

Comments
 (0)