How to get Paypal transaction Id after getting paykey in paypal integration in iphone?
-
31-05-2021 - |
문제
I am going to implement paypal in my iphone native app. But I am facig an issue in that.
I want to get my transaction Id regarding my PayKey. There is a direct method to get pay key in PayPal library to get PayKey but not a method to get transactionId.
Here is that method:
-(void)paymentSuccessWithKey:(NSString *)payKey andStatus:(PayPalPaymentStatus)paymentStatus;
As I am thinking IPN(Instant Payment Notification ) can help to resolve this issue but I don't know how to implement that one in my app?
Please help me to resolve this issue.
Thanks, Chandra Prakash
해결책
Fianlly I have got solution of my question.
I have got transaction Id using IPN.
When I selected ENABLE IPN Option from my PayPal account then all details send by paypal I have received on URL which I given from there I have got all required fields.
Here are a few links to help you
IPN Overview: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_html_instantpaymentnotif
IPN Documentation: https://cms.paypal.com/cms_content/US/en_US/files/developer/IPNGuide.pdf
IPN Variables: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_IPNandPDTVariables
IPN Script Example: https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_admin_IPNImplementation
IPN Script Generator: https://www.paypaltech.com/SG2/
Thanks, Cp
다른 팁
I had a similar issue but was unable to rely on IPN so I thought I would share another method.
you don't need to use IPN, you can get the transaction ID with the payKey, which is useful for verifying mobile payments. Here is a php block that will get all of the transaction information from a payKey. You'll need to call it from your mobile app. For iOS you could use this tutorial: http://www.raywenderlich.com/13511/how-to-create-an-app-like-instagram-with-a-web-service-backend-part-12
<?
header("Content-Type: application/json");
//DEBUG TO CHECK PAYKEY
//turn php errors on
ini_set("track_errors", true);
//Example payKey (got from iOS app)
$payKey = "<snip>";
//set PayPal Endpoint to sandbox
$url = trim("https://svcs.sandbox.paypal.com/AdaptivePayments/PaymentDetails?payKey=".$payKey."&requestEnvelope.errorLanguage=en_US");
//PayPal API Credentials
//to do: change these for production credentials in the production app
$API_UserName = "<snip>";
$API_Password = "<snip>";
$API_Signature = "<snip>";
//Default App ID for Sandbox
$API_AppID = "APP-80W284485P519543T";
$API_RequestFormat = "NV";
$API_ResponseFormat = "NV";
try
{
//Create payload for this query
$bodyparams = array ("requestEnvelope.errorLanguage" => "en_US");
// convert payload array into url encoded query string
$body_data = http_build_query($bodyparams, "", chr(38)); // Generates body data
//create request and add headers
$params = array("http" => array(
"method" => "POST",
"content" => $body_data,
"header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
"X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
"X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
"X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
"X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
"X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat
));
//create stream context
$ctx = stream_context_create($params);
//open the stream and send request
$fp = @fopen($url, "r", false, $ctx);
//get response
$response = stream_get_contents($fp);
//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
//close the stream
fclose($fp);
//parse the ap key from the response
$keyArray = explode("&", $response);
foreach ($keyArray as $rVal){
list($qKey, $qVal) = explode ("=", $rVal);
$kArray[$qKey] = $qVal;
}
//print the response to screen for testing purposes
If ( $kArray["responseEnvelope.ack"] == "Success")
{
foreach ($kArray as $key =>$value){
echo $key . ": " .$value . "<br/>";
}
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
}
catch(Exception $e)
{
echo "Message: ||" .$e->getMessage()."||";
}
//End check paykey
?>
I would have attached an image showing all of the fields that you can get at with $kArray but I don't have the reputation. you would want: $kArray["paymentInfoList.paymentInfo(0).transactionId"]
NOTE: the API credentials you should use for sandbox can be found under your "sandbox accounts" at developer.paypal.com. Expand the profile of xxxfacilitator@xxx to get them
We can get transaction details like -
First get access token using your clientId and ClientSecret
$ch = curl_init();
$clientId = PAYPAL_CLIENT_ID; //client Id
$secret = PAYPAL_CLIENT_SECRET; client secrete key
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
$accessToken = null;
if (empty($result))
die('invalid access token');
else {
$json = json_decode($result);
$accessToken = $json->access_token;
}
curl_close($ch);
After Getting access token, I get transaction detail using following code
$curl = curl_init("https://api.sandbox.paypal.com/v1/payments/payment/<paykey>");
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
$result = json_decode($response);
With this we can validate transaction.
remove sandbox work from url when you are using it for live.