Stripe Refund on connected account - php

i would like to refund a payment on a connected account. Therefore I'm using the following code:
$acc = \Stripe\Account::retrieve($conn_account);
$ch_id = "ch_XXXXXXXXXXXXX";
/*$ch = $stripe->charges->capture(
$ch_id,
[],
['stripe_account' => $conn_account ]
);
echo $ch;
*/
$stripe->refunds->create([
'charge' => $ch_id,
]);
Running the pgp returns
Fatal error: Uncaught (Status 404) (Request req_XXXXXXXX) No such charge: 'ch_XXXXXXXXXXXXXXXXXXX'
thrown in C:\xampp\htdocs\user\vendor\stripe\stripe-php\lib\Exception\ApiErrorException.php on line 38<br /
How can I solve this?
Thanks!

Did you created the charge by setting Stripe-Account header to your connected account ID? If so, you need to also set the Stripe-Account header when creating your refund. Your code should look like this:
$stripe->refunds->create(
['charge' => $ch_id ],
['stripe_account' => $conn_account ]
);
You can take a look at the documentation for more information (like refunding application fees): https://stripe.com/docs/connect/direct-charges#issuing-refunds

Related

Stripe API Update DRAFT invoice for subscription (PHP)

I am using PHP and have a webhook generated with invoice.created. When the invoice is created, Stripe lets the draft invoice sit there for about an hour which gives us time to edit the invoice or adjust it. I use this time to check the database (via webhook) to see how many referrals this person has in the system to the adjust the coupons accordingly. I can't seem to adjust the invoice properly as I get errors in all the ways I have tried, which are:
$inv = {the invoice id and I get no error from the invoice ID being incorrect
define('STRIPE_API_KEY', 'sk_test_******************');
$stripe = new \Stripe\StripeClient(
STRIPE_API_KEY
);
$stripe->invoices->update(
$inv,
['discounts.coupon' => '20OFF']
);
Error is unknown object discounts.coupon
and
$stripe->invoices->update(
$inv,
['discount' => '20OFF']
);
Error = Fatal error: Uncaught (Status 400) (Request req_hsX1QTOxzWMiQn) Received unknown parameter: discount. Did you mean discounts? thrown in /home/searchoscn/public_html/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php on line 38
and
$stripe->invoices->update(
$inv,
['coupon' => '20OFF']
);
ERROR = Fatal error: Uncaught (Status 400) (Request req_yEwwOLoukR6j6Z) Received unknown parameter: coupon thrown in /home/searchoscn/public_html/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php on line 38
$stripe->invoices->update(
$inv,
['discounts' => ['coupon' => '20OFF']]
);
Fatal error: Uncaught (Status 400) (Request req_IDRP1Rjv1YoBZR) Invalid array thrown in /home/searchoscn/public_html/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php on line 38
I'm pretty sure I need to use 'discounts' but don't know how to structure the array to pass the coupon properly.
Trying to predict some questions or comments:
Yes, I do have an existing coupon with id of 20OFF. This is for 20% off
$inv does fetch the correct ID of the invoice at the time the webhook is sent.
Here is the link of the API documentation that I've been reading, but it isn't very helpful.
Instead of:
$stripe->invoices->update(
$inv,
['discounts.coupon' => '20OFF']
);
try:
$stripe->invoices->update(
$inv,
['discounts' => ['coupon' => $couponId]]
);
It looks like it must be the id of the coupon and not the customer facing code, e.g. 20OFF.
You would be able to get the id of the coupon from the dashboard (https://dashboard.stripe.com/coupons/) or by making another api request.
It's
$stripe->invoices->update(
$inv,
[
'discounts' => [
['coupon' => '20OFF']
]
]
);
As in, it's an array that then contains objects (in PHP those are both square brackets) and the objects refer to the coupon.

Stripe PHP API : You cannot confirm this PaymentIntent because it's missing a payment method

I am trying to implement the Stripe PHP API, but I am stuck since last week.
At first I thought I did not mention the payment method correctly in the way of a correct notation.
However this seems not to be the case.
I have created a wrapper class that loads all stripe API files and following handles all calls required for me.
Hence that's why the code layout.
When I execute my script I receive the following error :
Fatal error: Uncaught (Status 400) (Request req_AQbkjKFB4mo31Z) You
cannot confirm this PaymentIntent because it's missing a payment
method. You can either update the PaymentIntent with a payment method
and then confirm it again, or confirm it again directly with a payment
method. thrown in
httpdocs/stripe/stripe/lib/Exception/ApiErrorException.php
on line 38
I have contacted the Stripe helpdesk already however they keep referring me to their manual but that doesn't get me any further either.
The iDeal payment method has been enabled in their dashboard. So that's not the reason for failure.
Where it COULD be is the manner I setup my payment. But for as far as I am concerned I do not have set the payment method correctly. But for as far as I can see that has been done accordingly their manual.
Also, I cannot find any confirmatory documentation about using the retrieve call. Should I do this? Or is this simply double and unneeded.
public function create_payment($amount, $order_id, $method = 'ideal', $return_url = NULL, $currency = 'eur'){
///######## CHECK IF CURRENCY IS ALLOWED
if(!$this->currency_supported($currency)) exit('<strong>error</strong>, stripe currency not supported : '.$currency);
///######## SETUP PAYMENT
$result = $this->obj->paymentIntents->create(
array(
'amount' => $amount,
'currency' => $currency,
'payment_method_types' => array($method)
)
);
///######## IF ERROR
if(!is_object($result) || !isset($result->id)) exit('<strong>error</strong>, something went wrong during stripe payment intend creation');
///######## SETUP PAYMENT ID
$payment_id = $result->id;
///######## RETRIEVE PAYMENT INTEND DETAILS
$result = $this->obj->paymentIntents->retrieve($payment_id, []);
///######## SET AN ORDER ID
$result = $this->obj->paymentIntents->update($payment_id, array(
'metadata' => array(
'order_id' => $order_id,
),
'payment_method_types' => array($method),
));
///######## SETUP PARAMETRES
$params = array('payment_method_types' => array($method));
///######## IF THE RETURN URL HAS BEEN SET
if($return_url) $params['return_url'] = $return_url;
///######## CONFIRM A PAYMENT INTEND
$result = $this->obj->paymentIntents->confirm($payment_id, $params);
exit(print_r($result));
}
I hope that one could point me at my error. Since I am absolutely stuck here.

Stripe Connect - Error Refunding Application Fee?

In my app I am creating a charge, like so:
$appFee = 100;
$depositCharge = 2000;
\Stripe\Stripe::setApiKey('sk_test_xxxxxx');
$charge = \Stripe\Charge::create(array(
"amount" => (int) $depositCharge,
"currency" => "usd",
"capture" => "true",
"destination" => $providerID,
"application_fee" => (int) $appFee,
"customer" => $customerID
));
Later on, I am trying to process a refund with the API, and also refund the application fee (so my connected account doesn't eat the fee)
$re = \Stripe\Refund::create(array(
"charge" => $chargeID,
"refund_application_fee" => "true"
));
But when I do this, I get the following error:
{
"error": {
"type": "invalid_request_error",
"message": "Attempting to refund_application_fee on charge ch_xxxxxx, but it has no application fee. To refund the application fee on the associated transfer, set reverse_transfer=true."
}
}
If I leave out the 'refund_application_fee' parameter, it works fine. Why is this parameter causing an error?
Basically you need to provide the Stripe Account Id of the Connected account which is I think $providerID in your case while creating the Refund.
I have not tested your case, but I was also getting same issue that when I set refund_application_fee the Refund gives error and leaving it out works fine.
After that I Provided Stripe Account Id of the Connected account against which Charge was being created and Refund was successful with refund_application_fee set to true.

Omnipay token billing not working with stripe

I am using ignited/laravel-omnipay package for omnipay in laravel.
I am trying to implement token billing using stripe as shown here https://github.com/thephpleague/omnipay#token-billing.
Customer are getting created successfully on stripe but i am not able to make payment with the returned customer id.
Here's my code
$token = Input::get('stripetoken');
$gateway = Omnipay::create('Stripe');
$gateway->setApiKey('My Key');
$gateway->setTestMode(true);
$cardresponse = $gateway->createCard(array('token' =>$token))->send();
if ($cardresponse->isSuccessful()) {
$card_id = $cardresponse->getCardReference();
$data = $cardresponse->getData();
$customerid = $data['id'];
$cardid = $data['default_source'];
}
$paymentresponse = $gateway->purchase(array('amount' => '10.00','currency' => 'USD', 'cardReference' => $card_id))->send();
echo $paymentresponse->getMessage();
I am getting following response.
No such token: cus_8FwPaLNKdWcfRW
And when i check my stripe dashboard then customer with this id exists and has a card assigned.
Thanks for helping.
Since you're creating a customer object, you need to update your charge creation request to pass the customer ID in the customer parameter rather than in the source parameter (which causes the error you're seeing).
I'm not familiar with Omnipay but I think this should work:
$paymentresponse = $gateway->purchase(array('amount' => '10.00','currency' => 'USD', 'customerReference' => $card_id))->send();

PayPal Express Checkout with Omnipay not showing order in Sandbox account

I have used the Omnipay PayPal_Express checkout script on my site and everything works fine when I pay for an order except the order doesn't show in the PayPal Sandbox account.
It does show when I use the same script for PayPal_Pro.
My code is as follows:
use Omnipay\Omnipay;
// PayPal Express:
if(isset($_POST['paypalexpress'])) {
$gateway = GatewayFactory::create('PayPal_Express');
$gateway->setUsername('{myusername}');
$gateway->setPassword('{mypassword}');
$gateway->setSignature('{mysignauture}');
$gateway->setTestMode(true);
$response = $gateway->purchase(
array(
'cancelUrl'=>'http://www.mysite.com/?cancelled',
'returnUrl'=>'http://www.mysite.com/?success',
'amount' => "12.99",
'currency' => 'GBP',
'Description' => 'Test Purchase for 12.99'
)
)->send();
$response->redirect();
}
I have created two test accounts in my Sandbox, one is for the above API and one I use to pay with. I have tried paying with the test card details and the login but the order detail doesn't show in the account.
Can anyone help?
It looks like you're missing the completePurchase() part when Paypal returns to your returnUrl. My code assumes that you have the order details in a variable $order, but it may look something like this:
if(isset($_GET['success'])) {
$response = $gateway->completePurchase(array(
'transactionId' => $order->transaction,
'transactionReference' => $order->reference,
'amount' => $order->total,
'currency' => $order->currency,
))->send();
if ( ! $response->isSuccessful())
{
throw new Exception($response->getMessage());
}
}
Let me know if you need any help retrieving the order details on return. It can be stored in a session before you redirect, or in a database. If you haven't done already, take a look at the example code: https://github.com/omnipay/example/blob/master/index.php

Categories