I have an id of credit card details which is saved in paypal vault CARD-xxxxxxxxxxxxxxxxxx
. I need to make a payment with this card using php-paypal-SDK. For this purpose I have written bellow code.
<?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);
}
But it throws error as follows
MALFORMED_REQUEST - Incoming JSON request does not map to API request
what was wrong with this code?This is the first time i am dealing with this SDK.
If still not working then you can use curl
or any HTTP client. For the demonstration, I used 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);
Follow this link to read more details. If you need further help you can comment here or linkedin , github or instagram
Good luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.