2017-04-17 59 views
0

我想在交易中包含一個標識符,所以我知道從Paypal返回時哪個交易。我試圖使用reference_id,因爲它就像更好的選項一樣(因爲是我分配給事務的臨時標識符)。

的問題是,當我把它設置成交易,貝寶拒絕接受JSON,它返回:

{ 
    "name":"MALFORMED_REQUEST", 
    "message":"Incoming JSON request does not map to API request", 
    "information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST", 
    "debug_id":"6835f984b6735" 
} 

或多或少我使用的示例代碼:

// Create new payer and method 
$payer = new Payer(); 
$payer->setPaymentMethod("paypal"); 

// Set redirect urls 
$redirectUrls = new RedirectUrls(); 
$redirectUrls->setReturnUrl($urlRetorno) 
    ->setCancelUrl($urlCancelar); 

// Set payment amount 
$amount = new Amount(); 
$amount->setCurrency($moneda) 
    ->setTotal($total); 

// Set transaction object 
$transaction = new Transaction(); 
$transaction->setAmount($amount) 
    ->setDescription($descripcion); 

// If I comment this line it runs ok. 
$transaction->setReferenceId($referenceId); 

// Create the full payment object 
$payment = new Payment(); 
$payment->setIntent('sale') 
    ->setPayer($payer) 
    ->setRedirectUrls($redirectUrls) 
    ->setTransactions(array($transaction)); 

echo $payment->toJSON(); 

// Create payment with valid API context 
try { 

    $payment->create($apiContext); 

    // Get PayPal redirect URL and redirect user 
    $approvalUrl = $payment->getApprovalLink(); 

    // Finalmente le redirigimos a PayPal para que apruebe el pago 
    //redirige($approvalUrl, 'Intentando redirigir a PayPal.'); 

} catch (PayPal\Exception\PayPalConnectionException $ex) { 
    echo $ex->getCode(); 
    echo $ex->getData(); 
    die($ex); 
} catch (Exception $ex) { 
    die($ex); 
} 

而且這是(調用的toJSON())結果:

{ 
    "intent":"sale", 
    "payer":{ 
     "payment_method":"paypal" 
    }, 
    "redirect_urls":{ 
     "return_url":"https://example.com/return.php", 
     "cancel_url":"https://example.com/cancel.php" 
    }, 
    "transactions":[ 
     { 
     "amount":{ 
      "currency":"MXN", 
      "total":"1515" 
     }, 
     "description":"Suscripci\u00f3n c\u00f3digo PP-19119", 
     "reference_id":"304171757041" 
     } 
    ] 
} 

我已經找到了reference_id in the API documentation所以我想我可以使用它。

回答

相關問題