如何在iphone中获得paypal集成中的paykey后获得Paypal交易ID?

时间:2012-04-13 12:37:34

标签: iphone transactions paypal response paykey

我将在我的iphone原生应用程序中实现paypal。但我认为这是一个问题。

我想获得有关PayKey的交易ID。有一种直接的方法可以在PayPal库中获取付费密钥以获得PayKey,但不能获得 transactionId 的方法。

这是方法:

-(void)paymentSuccessWithKey:(NSString *)payKey andStatus:(PayPalPaymentStatus)paymentStatus;

我认为IPN(即时付款通知)可以帮助解决此问题,但我不知道如何在我的应用中实现该问题?

请帮我解决此问题。

谢谢, 钱德拉普拉卡什

3 个答案:

答案 0 :(得分:1)

Fianlly我已经解决了我的问题。

我使用IPN获得了交易ID。

当我从PayPal帐户中选择 ENABLE IPN 选项时,我收到的所有详细信息都是通过Paypal收到的,我从那里收到了所有必填字段。

以下是一些可以帮助您的链接

IPN概述: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_html_instantpaymentnotif

IPN文档: https://cms.paypal.com/cms_content/US/en_US/files/developer/IPNGuide.pdf

IPN变量: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_IPNandPDTVariables

IPN脚本示例: https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_admin_IPNImplementation

IPN脚本生成器: https://www.paypaltech.com/SG2/

谢谢, CP

答案 1 :(得分:1)

我有类似的问题,但无法依赖IPN,所以我想我会分享另一种方法。

您不需要使用IPN,您可以使用payKey获取交易ID,这对于验证移动支付非常有用。这是一个php块,它将从payKey获取所有交易信息。您需要通过移动应用程序调用它。对于iOS,您可以使用本教程: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

?>

我会附上一张图片,显示你可以使用$ kArray获得的所有字段,但我没有声誉。你想要:$ kArray [“paymentInfoList.paymentInfo(0).transactionId”]

注意:您应该用于沙箱的API凭据可以在developer.paypal.com的“沙盒帐户”下找到。扩展xxxfacilitator @ xxx的配置文件以获取它们

答案 2 :(得分:1)

我们可以获得交易详情,如 -

首先使用clientId和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);

获取访问令牌后,我使用以下代码获取交易详情

 $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);

有了这个,我们就可以验证交易。

在您将其用于实时时,从网址中删除沙箱工作。