Refunding a captured authorization - php

am using the php api sdk found here: https://github.com/paypal/PayPal-PHP-SDK but am having trouble issuing a refund.
This is the code I am using:
// CAPTURE THE ORIGINAL AUTHORIZATION
try
{
$amount = new Amount();
$amount->setCurrency("USD");
$amount->setTotal($amount_authorized);
$capture = new Capture();
$capture->setId($authorization_id);
$capture->setAmount($amount);
$authorization = Authorization::get($transaction_id, $apiContext);
$capt = $authorization->capture($capture, $apiContext);
if ($capt->state == 'completed')
{
$capture_id = $capt->id;
}
}
catch (PayPal\Exception\PayPalConnectionException $e)
{
$response = json_decode($e->getData());
die('Error capturing funds: '.$response->message);
}
catch (Exception $e)
{
die('Error capturing funds: '.$e->getMessage());
}
// REFUND THE CAPTURE
try
{
$amount = new Amount();
$amount->setCurrency("USD");
$amount->setTotal($refund_amount);
$refund = new Refund();
$refund->setId($capture_id);
$refund->setAmount($amount);
$capture = Capture::get($capture_id, $apiContext);
$captureRefund = $capture->refund($refund, $apiContext);
if ($captureRefund->state == 'completed')
{
die('REFUNDED');
}
}
catch (PayPal\Exception\PayPalConnectionException $e)
{
$response = json_decode($e->getData());
die('There was an error refunding the funds: '.$response->message);
}
catch (Exception $e)
{
die('There was an error refunding the funds: '.$e->getMessage());
}
I get this error everytime: The requested resource ID was not found
I believe I am following the sample code on the developer site but I'll pulling my hair out. It's probably something pretty easy I hope.
Any ideas?
Thanks

Related

Docusign remote signing integration in laravel

Can anyone provide reference of docusign remote signing in laravel. I know that there is code already available in plain php on github but it did not work for me.I searched for code related to laravel but I did not find it anywhere on the Internet.
I am expecting the reference in laravel not in plain php.Can anyone help me in this?
https://www.docusign.com/blog/developers/send-document-laravel-jwt-grant-authentication has all the details about doing this with Laravel
Here is the PHP code:
$apiClient = new ApiClient();
$apiClient->getOAuth()->setOAuthBasePath(env('DS_AUTH_SERVER'));
try{
$accessToken = $this->getToken($apiClient);
} catch (\Throwable $th) {
return back()->withError($th->getMessage())->withInput();
}
$userInfo = $apiClient->getUserInfo($accessToken);
$accountInfo = $userInfo[0]->getAccounts();
$envelopeDefenition = $this->buildEnvelope($request);
try {
$envelopeApi = new EnvelopesApi($apiClient);
$result = $envelopeApi->createEnvelope($accountInfo[0]->getAccountId(), $envelopeDefenition);
} catch (\Throwable $th) {
return back()->withError($th->getMessage())->withInput();
}
private function getToken(ApiClient $apiClient) : string{
try {
$privateKey = file_get_contents(storage_path(env('DS_KEY_PATH')),true);
$response = $apiClient->requestJWTUserToken(
$ikey = env('DS_CLIENT_ID'),
$userId = env('DS_IMPERSONATED_USER_ID'),
$key = $privateKey,
$scope = env('DS_JWT_SCOPE')
);
$token = $response[0];
$accessToken = $token->getAccessToken();
} catch (\Throwable $th) {
throw $th;
}
return $accessToken;
}

PayPal Payout failed with 'Got Http response code 403'

I am working on PayPal with PHP.
I create Payout from this - http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payouts/CreateSinglePayout.html
Exactly code is like this:
$payouts = new Payout();
$senderBatchHeader = new PayoutSenderBatchHeader();
$senderBatchHeader->setSenderBatchId(uniqid())
->setEmailSubject("You have a Payout!");
$senderItem = new PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver('test#example.com')
->setSenderItemId("2014031400023")
->setAmount(new Currency('{
"value":"1.0",
"currency":"USD"
}'));
$payouts->setSenderBatchHeader($senderBatchHeader)
->addItem($senderItem);
$request = clone $payouts;
try {
$output = $payouts->createSynchronous(PaypalController::$_api_context);
} catch (Exception $ex) {
return 'Error: '. $ex->getMessage();
}
return 'Success Payout!';
But when I run this, it returns error - 'Got Http response code 403 when accessing https://api.sandbox.paypal.com/v1/payments/payouts?sync_mode=true.'
Why it happens, How can I solve this?
Thanks much to anyone help.

stripe webhook test mode response "none"

I am trying to update database.
I have set up the webhook in stripe in test mode and send a "invoice.payment_succeeded" test webhook to file.but it shows response "none" in stripe output.
What have i done wrong, here is the webhook file, please someone help me, i am very stuck at this. any help will be appreciate...
<?php
include '../admin/include/functions.php';
require_once('Stripe/lib/Stripe.php');
require_once 'stripe_secret.php';
// Retrieve the request's body and parse it as JSON
$input = #file_get_contents("php://input");
$event_json = json_decode($input);
$event_id = $event_json->id;
if(isset($event_json->id)) {
try {
Stripe::setApiKey($stripe['secretkey']);
$event = Stripe_Event::retrieve($event_id);
var_dump($event);
$invoice = $event->data->object;
if($event->type == 'invoice.payment_succeeded') {
$customer = Stripe_Customer::retrieve($invoice->customer);
$email = $customer->email;
$customerid = $customer->id;
/*$amount = $invoice->amount / 100;*/
$expiry = $invoice->period->end;
$expiredate = date('Y-d-m', $expiry);
$userup = $obj->run_query("UPDATE users SET Expiry_Date = '$expiredate' WHERE user_stripe_id = '$customerid' ");
if ($userup) {
echo "User Date extended";
}
// send a invoice notice email here
}
if($event->type == 'invoice.payment_failed') {
$obj->run_query("UPDATE users SET Status = '0' WHERE user_stripe_id = '$customerid' ");
echo "User membership expired";
}
}
catch(Stripe_CardError $e) {
}
catch (Stripe_InvalidRequestError $e) {
// Invalid parameters were supplied to Stripe's API
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
} catch (Stripe_Error $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}
}
http_response_code(200);
?>
The test webhooks from the test webhook button sends a webhook with the right format but all the values are null / zero / etc. Thus your line that does $obj->run_query("UPDATE users SET Expiry_Date = '$expiredate' WHERE user_stripe_id = '$customerid' "); will return a falsey result. This means you don't echo anything and just send back an empty 200 response.

Google Cloud Messaging (GCM) Notification receiving with logs detail

I have implement the gcm to send notification to my application and its really working perfect.
I am getting notification on device but issue is that I am getting logs detail may be in notification message instead of only human readable message that I am pushing.
Here is my code that I am using to send notification:
$gcmApiKey = GOOGLE_API_KEY;
$pushApi = new PushAPI();
$sender = new \PHP_GCM\Sender($gcmApiKey);
$message = new \PHP_GCM\Message("1", $messsageVal);
try {
$multicastResult = $sender->sendMulti($message, $deviceRegistrationId, 2);
$results = $multicastResult->getResults();
for ($i = 0; $i < count($deviceRegistrationId); $i++) {
$regId = $deviceRegistrationId[$i];
$result = $results[$i];
$messageId = $result->getMessageId();
if ($messageId != null) {
$canonicalRegId = $result->getCanonicalRegistrationId();
if ($canonicalRegId != null) {
// same device has more than on registration id: update it
}
} else {
$error = $result->getErrorCodeName();
if ($error == \PHP_GCM\Constants::$ERROR_NOT_REGISTERED) {
$pushApi->clearDeviceByDeviceId($regId);
}
}
}
} catch (\InvalidArgumentException $e) {
// $deviceRegistrationId was null
} catch (PHP_GCM\InvalidRequestException $e) {
// server returned HTTP code other than 200 or 503
} catch (\Exception $e) {
// message could not be sent
}
Here is the link of whole API that I am using. PHP_GCM
I try to google it but I didn't get anything to rectify this.
Any help will be appreciated.
You have to parse your response same as you parse a webservice response and show only the msg.
Try getting the message from the received intent using:
String msg=intent.getExtras().getString("msg");
Then show the message as :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.mipmap.app_icon)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle(context.getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);

Insert a comment on a Google+ page

How to post a comment or reply to a comment by using the google+ API?
I tried this:
$service = new Google_Service_PlusDomains($client);
try {
$activity = new Google_Service_PlusDomains_Activity();
$activity->setObject($text);
$activity->setActor($this->id);
$activity->setTitle($text);
$reply = $service->activities->insert('me',$activity);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
But it is not working, no comment or post is sent.

Categories