我有一个信用卡详细信息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。
答案 0 :(得分:0)
如果仍然无法使用,则可以使用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": "10.00",
"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
进行评论祝你好运