diff --git a/samples/rest/checkout_orders/RefundCapturedPayment.php b/samples/rest/checkout_orders/RefundCapturedPayment.php index 05f763f0..7776c734 100644 --- a/samples/rest/checkout_orders/RefundCapturedPayment.php +++ b/samples/rest/checkout_orders/RefundCapturedPayment.php @@ -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. @@ -31,7 +31,7 @@ 'amount' => $amount, 'note_to_payer' => $note_to_payer, - 'invoice_id' => $invoice_id, + ); $response = $PayPal->RefundCapturedPayment($capture_id,$requestArray); diff --git a/src/angelleye/PayPal/CheckoutOrdersClass.php b/src/angelleye/PayPal/CheckoutOrdersClass.php index 52208a1e..be86b568 100644 --- a/src/angelleye/PayPal/CheckoutOrdersClass.php +++ b/src/angelleye/PayPal/CheckoutOrdersClass.php @@ -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; } /** @@ -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; + } /** @@ -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; } /** @@ -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; } /** diff --git a/src/angelleye/PayPal/PayFlow.php b/src/angelleye/PayPal/PayFlow.php index 28a2e09a..4900a242 100755 --- a/src/angelleye/PayPal/PayFlow.php +++ b/src/angelleye/PayPal/PayFlow.php @@ -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)); diff --git a/src/angelleye/PayPal/PayPal.php b/src/angelleye/PayPal/PayPal.php index a1a729ef..3d41eb94 100755 --- a/src/angelleye/PayPal/PayPal.php +++ b/src/angelleye/PayPal/PayPal.php @@ -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) { @@ -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; } @@ -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) { + + } + } } \ No newline at end of file diff --git a/src/angelleye/PayPal/rest/payments/PaymentAPI.php b/src/angelleye/PayPal/rest/payments/PaymentAPI.php index 44bc6a61..a535e504 100755 --- a/src/angelleye/PayPal/rest/payments/PaymentAPI.php +++ b/src/angelleye/PayPal/rest/payments/PaymentAPI.php @@ -1,5 +1,6 @@ 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); } @@ -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); } @@ -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); } @@ -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); @@ -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); } @@ -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); @@ -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); @@ -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); @@ -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);