【发布时间】:2015-07-18 18:40:24
【问题描述】:
我在使用 PayPal REST API 时出错,但我无法在任何地方找到解决方案。我正在尝试将用户发送到 PayPal 以支付会话数组中的指定项目。我已尝试更改付款方式、意图和 URL。
$this->setupCurl();
$this->setupPayPal();
$this->payer = new Payer();
$this->details = array();
$this->amount = array();
$this->transaction = array();
$this->redirectUrls = new RedirectUrls();
$this->payment = new Payment();
$this->payer->setPaymentMethod('paypal'); // TODO paypal . credit_card
foreach ($cart as $id => $item) {
$this->details[$id] = new Details();
$this->amount[$id] = new Amount();
$this->transaction[$id] = new Transaction();
$this->details[$id]->setShipping('0.00')
->setTax('0.00')
->setSubtotal(formatMoney($this->products[$id]['price'] * $item['quantity'], '', 2)); // TODO
$this->amount[$id]->setCurrency('AUD')
->setTotal(formatMoney($this->products[$id]['price'] * $item['quantity'], '', 2))
->setDetails($this->details[$id]);
$this->transaction[$id]->setAmount($this->amount[$id])
->setDescription($this->products[$id]['name']); // TODO
}
$this->redirectUrls->setReturnUrl('https://warsentech.com/pay?approved=true')
->setCancelUrl('https://warsentech.com/pay?approved=false');
$this->payment->setIntent('authorize') // TODO sale . buy
->setPayer($this->payer)
->setRedirectUrls($this->redirectUrls)
->setTransactions($this->transaction); // TODO - add transactions into array from the cart items
try {
$this->payment->create($this->paypal);
$hash = md5($this->payment->getId());
$_SESSION['paypal_hash'] = $hash;
if ($this->databaseConnection()) {
$result = $this->db_connection->prepare('INSERT INTO transactions_paypal (user_id, payment_id, hash, complete) VALUES (:user_id, :payment_id, :hash, 0)');
$result->execute([
'user_id' => $_SESSION['user_id'],
'payment_id' => $this->payment->getId(),
'hash' => $hash
]);
}
} catch (Exception $e) {
die(paypalError($e));
}
foreach ($this->payment->getLinks() as $link) {
if ($link->getRel() == 'approval_url') {
$redirectUrl = $link->getHref();
}
}
完整的错误信息:
异常 'PayPal\Exception\PayPalConnectionException' 并带有消息“访问 https://api.sandbox.paypal.com/v1/payments/payment 时获得 Http 响应代码 400。”在 /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:177 堆栈跟踪:#0 /var/www/html/vendor/paypal/rest-api -sdk-php/lib/PayPal/Transport/PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('{"intent":"auth...') #1 /var/www/html/vendor /paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php(103): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST' , '{"intent":"auth...', NULL) #2 /var/www/html/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php(424): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"auth...', NULL, Object(PayPal\Rest\ApiContext), NULL ) #3 /var/www/html/classes/PayPal.php(168): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 /var/www/html/classes/PayPal. php(50): PayPal->pay(Array) #5 /var/www/html/checkout.php(39): PayPal->__construct() #6 {main}
【问题讨论】: