1

我已经使用 PayPal MPL 库成功地将 PayPal 集成到我的 iOS 应用程序中。但问题是如何从我从 PayPal 的回调方法获得的 paykey 获取交易 ID。

我已经尝试过这种方式,但收到无效请求的错误。

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

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://svcs.paypal.com/AdaptivePayments/PaymentDetails"]];

//NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://svcs.sandbox.paypal.com/AdaptivePayments/PaymentDetails"]];

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];

    NSString *parameterString = [NSString stringWithFormat:@"payKey=%@&requestEnvelope.errorLanguage=%@",payKey,@"en_US"];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [parameterString length]];

    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    //do post request for parameter passing
    [theRequest setHTTPMethod:@"POST"];

    [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    //passing key as a http header request
    [theRequest addValue:api_username forHTTPHeaderField:@"X-PAYPAL-SECURITY-USERID"];

    //passing key as a http header request
    [theRequest addValue:api_password forHTTPHeaderField:@"X-PAYPAL-SECURITY-PASSWORD"];

    [theRequest addValue:api_signature forHTTPHeaderField:@"X-PAYPAL-SECURITY-SIGNATURE"];

    [theRequest addValue:@"JSON" forHTTPHeaderField:@"X-PAYPAL-REQUEST-DATA-FORMAT"];

    [theRequest addValue:@"JSON" forHTTPHeaderField:@"X-PAYPAL-RESPONSE-DATA-FORMAT"];

    [theRequest addValue:app_id forHTTPHeaderField:@"X-PAYPAL-APPLICATION-ID"];

    [theRequest setHTTPBody: [parameterString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&err];

    NSString* s = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSDictionary *result = [s JSONValue] ;

}

我需要将事务 ID 保存在我的应用程序后端以供将来参考。需要知道如何尽快得到它。任何帮助,将不胜感激。

感谢和问候 Pankaj

4

3 回答 3

0

首先,确保您不发布带有 API 密码和签名的应用程序,这一点至关重要。否则,恶意用户可能会反汇编您的应用程序、恢复凭据并对您的帐户执行任意操作。阅读有关验证付款的更多信息。

但是,假设您在服务器上实现了类似的代码,它应该可以工作。你得到的确切错误是什么?

于 2013-09-04T20:59:34.757 回答
0

我不了解iOS,但对于andorid,我们在类似的类中实现了PayPalResultDelegate、Serializable 接口。我正在分享我的代码:

class ResultDelegate implements PayPalResultDelegate, Serializable 
{
    private static final long serialVersionUID = 10001L;

    PaypalDetailImplementation ob = new PaypalDetailImplementation() ;

    public void onPaymentSucceeded(String payKey, String paymentStatus) 
    {
        ob.resultTitle = "SUCCESS";
        ob.resultInfo = "You have successfully completed your transaction.";
        ob.resultExtra = "Key: " + payKey;

        this.displayInformation("onPaymentSucceeded", ob.resultTitle, ob.resultInfo, ob.resultExtra) ;
    }

    public void onPaymentFailed(String paymentStatus, String correlationID,String payKey, String errorID, String errorMessage) 
    {
        ob.resultTitle = "FAILURE";
        ob.resultInfo = errorMessage;
        ob.resultExtra = "Error ID: " + errorID + "\nCorrelation ID: "+ correlationID + "\nPay Key: " + payKey;

        this.displayInformation("onPaymentFailed", ob.resultTitle, ob.resultInfo, ob.resultExtra) ;
    }

    public void onPaymentCanceled(String paymentStatus) 
    {
        ob.resultTitle = "CANCELED";
        ob.resultInfo = "The transaction has been cancelled.";
        ob.resultExtra = "";

        this.displayInformation("onPaymentCanceled", ob.resultTitle, ob.resultInfo, ob.resultExtra) ;
    }

    public void displayInformation ( String msg , String resultTitle , String resultInfo , String resultExtra )
    {
        Log.i("Result as message : ",msg);
        Log.i(resultTitle,"tushar:onPaymentSucceeded");
        Log.i(resultInfo,"tushar:onPaymentSucceeded");
        Log.i(resultExtra,"tushar:onPaymentSucceeded");

    }
于 2013-09-03T13:00:00.893 回答
0

我得到了这个问题解决了我自己。

我正在制作 JSON 类型的 X-PAYPAL-REQUEST-DATA-FORMAT,但是当我将其更改为 NV 时,我开始调整交易 ID。

感谢大家的回答。

于 2013-09-05T10:45:06.403 回答