我有一个信用卡详细信息的ID,该ID已保存在Paypal库CARD-xxxxxxxxxxxxxxxxxx
中。我需要使用php-paypal-SDK用这张卡付款。为此,我编写了波纹管代码。
<?php
require 'PayPal-PHP-SDK-master/vendor/autoload.php';
$clientId = 'xxxxxxxxxxxxxxxxxx';
$clientSecret = 'xxxxxxxxxxxxxxxxxx';
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Api\Payment;
use PayPal\Api\Payer;
use PayPal\Api\FundingInstrument;
use PayPal\Api\CreditCard;
use PayPal\Api\CreditCardToken;
use PayPal\Api\Transaction;
$sdkConfig = array(
"mode" => "sandbox"
);
$apiContext = $apiContext = new ApiContext(new OAuthTokenCredential(
$clientId,
$clientSecret
));
$credit_card_token = new CreditCardToken();
$credit_card_token->setCreditCardId('CARD-xxxxxxxxxxxxxxxxxx');
$fundinginstrument = new FundingInstrument();
$fundinginstrument->setCreditCardToken($credit_card_token);
$payer = new Payer();
$payer->setPaymentMethod('credit_card');
$payer->setFundingInstruments(array($fundinginstrument));
$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$transaction = new Transaction();
$transaction->setAmount('10.00');
$transaction->setDescription('Test payment');
$payment->setTransactions(array($transaction));
$request = clone $payment;
try {
$output = $payment->create($apiContext);
print_r($output);
} catch (PayPal\Exception\PayPalConnectionException $pce) {
echo '<pre>';print_r(json_decode($pce->getData()));exit;
exit(1);
}
但是它会引发如下错误
MALFORMED_REQUEST-传入的JSON请求未映射到API请求
此代码出了什么问题?这是我第一次使用此SDK。
您的代码看起来不错,我认为这是与货币相关的问题。添加\PayPal\Api\Amount
,然后设置交易金额。按照下面的代码
$amount = new \PayPal\Api\Amount();
$amount->setCurrency('USD');
$amount->setTotal('10.00');
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
这里是完整代码
<?php
require 'vendor/autoload.php';
$clientId = 'CLIENT_ID';
$clientSecret = 'CLIENT_SECRET';
use PayPal\Api\Amount;
use PayPal\Api\CreditCardToken;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Rest\ApiContext;
$sdkConfig = array(
"mode" => "sandbox",
);
$apiContext = $apiContext = new ApiContext(new OAuthTokenCredential(
$clientId,
$clientSecret
));
$creditCardToken = new CreditCardToken();
$creditCardToken->setCreditCardId("CREDIT_CARD_ID");
$fi = new FundingInstrument();
$fi->setCreditCardToken($creditCardToken);
$payer = new Payer();
$payer->setPaymentMethod("credit_card");
$payer->setFundingInstruments(array($fi));
$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal('1.00');
$transaction = new Transaction();
$transaction->setAmount($amount);
$payment->setTransactions(array($transaction));
// $request = clone $payment;
try {
$output = $payment->create($apiContext);
echo "<pre>";
print_r($output);
} catch (PayPal\Exception\PayPalConnectionException $pce) {
echo '<pre>';
print_r(json_decode($pce->getData()));exit;
exit(1);
}
如果仍然无法使用,则可以使用curl
或任何HTTP客户端。为了演示,我使用了GuzzleHttp
$uri = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$clientId = 'CLIENT_ID';
$secret = 'CLIENT_SECRET';
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'headers' =>
[
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => 'grant_type=client_credentials',
'auth' => [$clientId, $secret, 'basic'],
]
);
$data = json_decode($response->getBody(), true);
$access_token = $data['access_token'];
$payment_uri = 'https://api.sandbox.paypal.com/v1/payments/payment';
$data = '{
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": [
{
"credit_card_token": {
"credit_card_id": "CREDIT_CARD_ID"
}
}]
},
"transactions": [
{
"amount": {
"total": "6.70",
"currency": "USD"
},
"description": "Payment by vaulted credit card."
}]
}';
$payment = $client->request('POST', $payment_uri, [
'headers' =>
[
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $access_token,
],
'body' => $data,
]
);
$transaction = json_decode($payment->getBody(), true);
echo "<pre>";
print_r($transaction);
跟随this 链接以阅读更多详细信息。如果您需要进一步的帮助,可以在此处评论,或单击linkedin,github或instagram
祝你好运