Skip to content

Commit

Permalink
Merge pull request #208 from angelleye/PPL-168
Browse files Browse the repository at this point in the history
General Fixes and Improvements: PPL-168
  • Loading branch information
deepakmaurya authored Jul 1, 2020
2 parents dba94d6 + 06a2ada commit 623cf81
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 14 deletions.
6 changes: 3 additions & 3 deletions samples/rest/checkout_orders/RefundCapturedPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

$PayPal = new CheckoutOrdersAPI($configArray);

$capture_id = '6X1812201H9506636'; // The PayPal-generated ID for the captured payment to refund.
$capture_id = '0HR988556J0930621'; // The PayPal-generated ID for the captured payment to refund.

$amount = array(
'currency_code' => 'USD',
'value' => 7.50, // The amount to refund.
'value' => 17.50, // The amount to refund.
);

$invoice_id = 'AEINV-323'; // Maximum length: 127. The API caller-provided external invoice number for this order. Appears in both the payer's transaction history and the emails that the payer receives.
Expand All @@ -31,7 +31,7 @@

'amount' => $amount,
'note_to_payer' => $note_to_payer,
'invoice_id' => $invoice_id,

);

$response = $PayPal->RefundCapturedPayment($capture_id,$requestArray);
Expand Down
24 changes: 18 additions & 6 deletions src/angelleye/PayPal/CheckoutOrdersClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ public function capture($order_id, $apiContext = null, $restCall = null){
$apiContext,
$restCall
);

$ret = new CheckoutOrdersClass();
return $ret->fromJson($json);
$result = $ret->fromJson($json);
$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($result, $request = array(), 24, true, false, 'PayPal_Rest');
return $result;
}

/**
Expand All @@ -175,7 +179,11 @@ public function authorize($order_id, $apiContext = null, $restCall = null){
$restCall
);
$ret = new CheckoutOrdersClass();
return $ret->fromJson($json);
$result = $ret->fromJson($json);
$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($result, $request = array(), 24, true, false, 'PayPal_Rest');
return $result;

}

/**
Expand Down Expand Up @@ -274,8 +282,10 @@ public function capture_authorization($authorization_id,$params,$apiContext = nu
$restCall
);
$ret = new CheckoutOrdersClass();
$ret->fromJson($json);
return $ret;
$result = $ret->fromJson($json);
$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($result, $request = array(), 24, true, false, 'PayPal_Rest');
return $result;
}

/**
Expand Down Expand Up @@ -348,8 +358,10 @@ public function refund($capture_id,$params,$apiContext = null, $restCall = null)
$restCall
);
$ret = new CheckoutOrdersClass();
$ret->fromJson($json);
return $ret;
$result = $ret->fromJson($json);
$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($result, $request = array(), 24, true, false, 'PayPal_Rest');
return $result;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/angelleye/PayPal/PayFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ function ProcessTransaction($DataArray)

$NVPResponse = $this->CURLRequest($NVPRequest);
$NVPResponse = strstr($NVPResponse,"RESULT");

$this->TPV_Parse_Request($NVPResponse, $NVPRequest, '24' , $this->Sandbox, true);

$NVPResponseArray = $this->NVPToArray($NVPResponse);

$this->Logger($this->LogPath, 'PayFlowRequest', $this->MaskPayflowAPIResult($NVPRequest));
Expand Down
120 changes: 120 additions & 0 deletions src/angelleye/PayPal/PayPal.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ function __construct($DataArray)
$this->PrintHeaders = isset($DataArray['PrintHeaders']) ? $DataArray['PrintHeaders'] : false;
$this->LogResults = isset($DataArray['LogResults']) ? $DataArray['LogResults'] : false;
$this->LogPath = isset($DataArray['LogPath']) ? $DataArray['LogPath'] : '/logs/';
$this->api_url = 'https://gtctgyk7fh.execute-api.us-east-2.amazonaws.com/default/PayPalPaymentsTracker';
$this->api_key = 'srGiuJFpDO4W7YCDXF56g2c9nT1JhlURVGqYD7oa';
$this->allow_method = array('DoExpressCheckoutPayment', 'DoDirectPayment', 'DoCapture', 'ProcessTransaction', 'PayPal_Rest');

if($this->Sandbox)
{
Expand Down Expand Up @@ -637,6 +640,7 @@ function CURLRequest($Request = "", $APIName = "", $APIOperation = "", $PrintHea
}

curl_close($curl);
$this->TPV_Parse_Request($Response, $Request, '24' , $this->Sandbox, true);
return $Response;
}

Expand Down Expand Up @@ -3404,5 +3408,121 @@ function BMSetInventory($DataArray)

return $NVPResponseArray;
}

public function TPV_Parse_Request($result_data, $request_data, $product_id = 1, $sandbox = false, $is_nvp = true, $payment_method = null) {
$request_param = array();
if (isset($result_data) && is_array($result_data) && !empty($result_data['CURL_ERROR'])) {
return $result_data;
} else {
if ($is_nvp) {
$result = $this->NVPToArray($result_data);
$request = $this->NVPToArray($request_data);
} else {
$result = $result_data;
$request = $request_data;
}
if (is_array($result) && isset($result['PNREF'])) {
$request['METHOD'] = 'ProcessTransaction';
}
if ($payment_method == 'PayPal_Rest') {
$request['METHOD'] = 'PayPal_Rest';
}
if (isset($request['METHOD']) && !empty($request['METHOD']) && in_array($request['METHOD'], $this->allow_method)) {
$request_param['site_url'] = '';
$request_param['merchant_id'] = '';
$request_param['type'] = $request['METHOD'];
if(is_array($result)) {
$request_param['status'] = isset($result['ACK']) ? $result['ACK'] : '';
}
$request_param['mode'] = ($sandbox) ? 'sandbox' : 'live';
$request_param['product_id'] = $product_id;
$request_param['merchant_id'] = '';
if ($request['METHOD'] == 'DoExpressCheckoutPayment') {
$request_param['correlation_id'] = isset($result['CORRELATIONID']) ? $result['CORRELATIONID'] : '';
$request_param['transaction_id'] = isset($result['PAYMENTINFO_0_TRANSACTIONID']) ? $result['PAYMENTINFO_0_TRANSACTIONID'] : '';
$request_param['amount'] = isset($result['PAYMENTINFO_0_AMT']) ? $result['PAYMENTINFO_0_AMT'] : '0.00';
$this->TPV_Send_Request($request_param);
} elseif ($request['METHOD'] == 'DoDirectPayment') {
$request_param['correlation_id'] = isset($result['CORRELATIONID']) ? $result['CORRELATIONID'] : '';
$request_param['transaction_id'] = isset($result['TRANSACTIONID']) ? $result['TRANSACTIONID'] : '';
$request_param['amount'] = isset($result['AMT']) ? $result['AMT'] : '0.00';
$this->TPV_Send_Request($request_param);
} elseif ($request['METHOD'] == 'DoCapture') {
$request_param['correlation_id'] = isset($result['CORRELATIONID']) ? $result['CORRELATIONID'] : '';
$request_param['transaction_id'] = isset($result['TRANSACTIONID']) ? $result['TRANSACTIONID'] : '';
$request_param['amount'] = isset($result['AMT']) ? $result['AMT'] : '0.00';
$this->TPV_Send_Request($request_param);
} elseif ($request['METHOD'] == 'ProcessTransaction') {
if (isset($result['RESULT']) && ( $result['RESULT'] == 0 )) {
$request_param['status'] = 'Success';
} else {
$request_param['status'] = 'Failure';
}
$request_param['correlation_id'] = isset($result['CORRELATIONID']) ? $result['CORRELATIONID'] : '';
$request_param['transaction_id'] = isset($result['PNREF']) ? $result['PNREF'] : '';
$request_param['amount'] = isset($result['AMT']) ? $result['AMT'] : '0.00';
$this->TPV_Send_Request($request_param);
} elseif ($request['METHOD'] == 'PayPal_Rest') {
$request_param['correlation_id'] = '';
if( !empty($result->purchase_units[0]['payments']['captures'][0]['amount']['value']) ) {
$request_param['amount'] = $result->purchase_units[0]['payments']['captures'][0]['amount']['value'];
$request_param['status'] = 'Success';
$request_param['transaction_id'] = $result->purchase_units[0]['payments']['captures'][0]['id'];
} elseif( !empty ($result->purchase_units[0]['payments']['authorizations'][0]['amount']['value'])) {
$request_param['amount'] = $result->purchase_units[0]['payments']['authorizations'][0]['amount']['value'];
$request_param['status'] = 'Success';
$request_param['transaction_id'] = $result->purchase_units[0]['payments']['authorizations'][0]['id'];
} elseif( !empty ($result['id']) ) {
$request_param['amount'] = !empty($result['transactions'][0]['amount']['total']) ? $result['transactions'][0]['amount']['total'] : '';
$request_param['status'] = 'Success';
$request_param['transaction_id'] = isset($result['transactions'][0]['related_resources'][0]['sale']['id']) ? $result['transactions'][0]['related_resources'][0]['sale']['id'] : '';
} else {
$request_param['status'] = 'Failure';
}
$this->TPV_Send_Request($request_param);
}
}
}
return $result_data;
}

function TPV_Send_Request($request_param) {
try {
$payment_type = $request_param['type'];
$amount = $request_param['amount'];
$status = $request_param['status'];
$site_url = $request_param['site_url'];
$payment_mode = $request_param['mode'];
$merchant_id = $request_param['merchant_id'];
$correlation_id = $request_param['correlation_id'];
$transaction_id = $request_param['transaction_id'];
$product_id = $request_param['product_id'];
$params = [
"product_id" => $product_id,
"type" => $payment_type,
"amount" => $amount,
"status" => $status,
"site_url" => $site_url,
"mode" => $payment_mode,
"merchant_id" => $merchant_id,
"correlation_id" => $correlation_id,
"transaction_id" => $transaction_id
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'x-api-key: ' . $this->api_key,
'Content-Length: ' . strlen(json_encode($params))
));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_URL, $this->api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
curl_exec($curl);
} catch (Exception $ex) {

}
}

}
44 changes: 39 additions & 5 deletions src/angelleye/PayPal/rest/payments/PaymentAPI.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

namespace angelleye\PayPal;
namespace angelleye\PayPal\rest\payments;

/**
Expand Down Expand Up @@ -55,6 +56,8 @@
use PayPal\Api\Sale;
use PayPal\Api\Transaction;
use \angelleye\PayPal\RestClass;
use angelleye\PayPal\PayPal;


/**
* PaymentAPI.
Expand Down Expand Up @@ -205,14 +208,16 @@ public function CreatePayment($requestData) {
$returnArray['PAYMENT'] = $payment->toArray();
$returnArray['RAWREQUEST']=$requestArray->toJSON();
$returnArray['RAWRESPONSE']=$payment->toJSON();
return $returnArray;
} else {
$returnArray['RESULT'] = 'Success';
$returnArray['PAYMENT']=$payment->toArray();
$returnArray['RAWREQUEST']=$requestArray->toJSON();
$returnArray['RAWRESPONSE']=$payment->toJSON();
return $returnArray;
}

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($payment->toArray(), array(), 24, true, false, 'PayPal_Rest');
return $returnArray;
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
}
Expand Down Expand Up @@ -492,8 +497,12 @@ public function CreateThirdPartyPayment($requestData){
$returnArray['RESULT'] = 'Success';
$returnArray['PAYMENT'] = array('approvalUrl' => $approvalUrl, 'payment' => $payment->toArray());
$returnArray['RAWREQUEST']=$requestArray->toJSON();
$returnArray['RAWRESPONSE']=$payment->toJSON();
return $returnArray;
$returnArray['RAWRESPONSE']=$payment->toJSON();

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($payment->toArray(), array(), 24, true, false, 'PayPal_Rest');
return $returnArray;

} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
}
Expand Down Expand Up @@ -604,7 +613,11 @@ public function CreatePaymentUsingSavedCardVault($requestData, $credit_card_id)
$returnArray['PAYMENT'] = $payment->toArray();
$returnArray['RAWREQUEST']=$requestArray->toJSON();
$returnArray['RAWRESPONSE']=$payment->toJSON();

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($payment->toArray(), array(), 24, true, false, 'PayPal_Rest');
return $returnArray;

} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
}
Expand Down Expand Up @@ -692,6 +705,11 @@ public function AuthorizationCapture($authorizationId, $amountArray) {
$returnArray['CAPTURE'] = $getCapture->toArray();
$returnArray['RAWREQUEST']=$requestArray->toJSON();
$returnArray['RAWRESPONSE']=$getCapture->toJSON();

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($getCapture->toArray(), array(), 24, true, false, 'PayPal_Rest');
return $returnArray;

return $returnArray;
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
Expand Down Expand Up @@ -819,7 +837,10 @@ public function OrderCapture($orderId,$amountArray=array(),$is_final_capture = f
$returnArray['ORDER_CAPTURE'] = $result->toArray();
$returnArray['RAWREQUEST']=$requestArray;
$returnArray['RAWRESPONSE']=$result->toJSON();
return $returnArray;

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($result->toArray(), array(), 24, true, false, 'PayPal_Rest');
return $returnArray;
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
}
Expand Down Expand Up @@ -896,6 +917,9 @@ public function ShowRefundDetails($refund_id){
$returnArray['REFUND'] = $refund->toArray();
$returnArray['RAWREQUEST']='{refund_id :'.$refund_id.'}';
$returnArray['RAWRESPONSE']=$refund->toJSON();

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($refund->toArray(), array(), 24, true, false, 'PayPal_Rest');
return $returnArray;
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
Expand Down Expand Up @@ -1005,6 +1029,10 @@ public function RefundSale($sale_id,$amount,$refundParameters){
$returnArray['SALE'] = $refundedSale->toArray();
$returnArray['RAWREQUEST']=$refundRequest->toJSON();
$returnArray['RAWRESPONSE']=$sale->toJSON();

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($refundedSale->toArray(), array(), 24, true, false, 'PayPal_Rest');

return $returnArray;
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
Expand Down Expand Up @@ -1116,6 +1144,10 @@ public function ExecutePayment($paymentId,$payer_id,$amount=array()){
$returnArray['PAYMENT'] = $result->toArray();
$returnArray['RAWREQUEST']=$execution->toJSON();
$returnArray['RAWRESPONSE']=$result->toJSON();

$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($result->toArray(), array(), 24, true, false, 'PayPal_Rest');

return $returnArray;
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
Expand Down Expand Up @@ -1231,6 +1263,8 @@ public function CreateFuturePayment($method,$requestData){
$returnArray['PAYMENT'] = array('approvalUrl' => $approvalUrl, 'payment' => $payment->toArray());
$returnArray['RAWREQUEST']=$requestArray->toJSON();
$returnArray['RAWRESPONSE']=$payment->toJSON();
$paypal = new PayPal(array());
$paypal->TPV_Parse_Request($payment->toArray(), array(), 24, true, false, 'PayPal_Rest');
return $returnArray;
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
return $this->createErrorResponse($ex);
Expand Down

0 comments on commit 623cf81

Please sign in to comment.