Stripe issue when creating subscription: "Could not determine which URL to request" - php

I've setup stripe multiple times and never had this issue before. I'm processing the "customer created" web hook and using the payload to attach stripe id to my user and setup a new subscription.
I'm getting the following error:
Stripe\Exception\UnexpectedValueException: Could not determine which URL to request: Stripe\Subscription instance has invalid ID: in file /Users/mick/repos/askit/vendor/stripe/stripe-php/lib/ApiResource.php on line 107
This only happens when I try to create the new subscription. Here is my full controller:
public function handleCustomerCreated(Request $request)
{
$payload = json_decode($request->getContent(), true);
$user_id = $payload->data->object->subscriptions->data[0]->metadata->user_id;
$subscription_id = $payload->data->object->subscriptions->data[0]->id;
$stripe_customer_id = $payload->data->object->id;
$current_period_start = $payload->data->object->subscriptions->data[0]->current_period_start;
$current_period_end = $payload->data->object->subscriptions->data[0]->current_period_end;
$stripe_plan = $payload->data->object->subscriptions->data[0]->items->data[0]->plan->id;
$name = $payload->data->object->subscriptions->data[0]->items->data[0]->plan->nickname;
$stripe_status = $payload->data->object->subscriptions->data[0]->status;
$interval = $payload->data->object->subscriptions->data[0]->items->data[0]->plan->interval_count;
$user = User::query()->find($user_id);
$user->stripe_id = $stripe_customer_id;
$user->save();
$subscription = new Subscription();
$subscription->user_id = $user_id;
$subscription->name = $name;
$subscription->stripe_id = $stripe_customer_id;
$subscription->stripe_status = $stripe_status;
$subscription->stripe_plan = $stripe_plan;
$subscription->interval = $interval;
$subscription->current_period_start = $current_period_start;
$subscription->current_period_end = $current_period_end;
$subscription->subscription_id = $subscription_id;
$subscription->save();
}
If I remove the new Subscription stuff it works fine, the stripe is getting attached to my user model just fine. The error only happens on this part:
$subscription = new Subscription();
$subscription->user_id = $user_id;
$subscription->name = $name;
$subscription->stripe_id = $stripe_customer_id;
$subscription->stripe_status = $stripe_status;
$subscription->stripe_plan = $stripe_plan;
$subscription->interval = $interval;
$subscription->current_period_start = $current_period_start;
$subscription->current_period_end = $current_period_end;
$subscription->subscription_id = $subscription_id;
$subscription->save();
Any ideas? The table was created automagically by Laravel Cashier.

Try using \Stripe\Subscription::create() as shown in the Stripe API Docs, instead of manually creating and modifying a new Subscription(). You're current code is trying to update a Subscription that doesn't yet exist in the API, so it can't find its id.

Related

Paytm php sdk integration PaymentDetailBuilder Class not found

Fatal error: Uncaught Error: Class 'PaymentDetailBuilder' not found
initial code is like this:
require_once('vendor/autoload.php');
use paytmpg\merchant\models\PaymentDetail\PaymentDetailBuilder;
use paytmpg\merchant\models\PaymentStatusDetail\PaymentStatusDetailBuilder;
use paytmpg\merchant\models\RefundDetail\RefundDetailBuilder;
use paytmpg\merchant\models\RefundStatusDetail\RefundStatusDetailBuilder;
// For Staging
$environment = LibraryConstants::STAGING_ENVIRONMENT;
// For Production
// $environment = LibraryConstants::PRODUCTION_ENVIRONMENT;
// Find your mid, key, website in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys
$mid = MERCHANT_MID;
$key = MERCHANT_KEY;
$website = MERCHANT_WEBSITE;
$client_id = MERCHANT_CLIENT_ID;
$callbackUrl = MERCHANT_CALLBACK_URL;
MerchantProperties::setCallbackUrl($callbackUrl);
MerchantProperties::initialize($environment, $mid, $key, $client_id, $website);
// If you want to add log file to your project, use below code
// Config::$monologName = '[PAYTM]';
// Config::$monologLevel = MonologLogger::INFO;
// Config::$monologLogfile = 'file.log';
$channelId = EChannelId::WEB;
$orderId = "ORDER_ID".rand(0,99); // post order id
$txnAmount = Money::constructWithCurrencyAndValue(EnumCurrency::INR, "11.00"); // post amount to be paid
$userInfo = new UserInfo("CUSTID_002");
$userInfo->setAddress("CUSTOMER_ADDRESS");
$userInfo->setEmail("CUSTOMER_EMAIL_ID");
$userInfo->setFirstName("CUSTOMER_FIRST_NAME");
$userInfo->setLastName("CUSTOMER_LAST_NAME");
$userInfo->setMobile("CUSTOMER_MOBILE_NO");
$userInfo->setPincode("CUSTOMER_PINCODE");
$paymentDetailBuilder = new PaymentDetailBuilder($channelId, $orderId, $txnAmount, $userInfo);
$paymentDetail = $paymentDetailBuilder->build();
$response = Payment::createTxnToken($paymentDetail);
print_r($response);
I have copied from => https://developer.paytm.com/docs/server-sdk/php/?ref=serverSdk#SDKCode;

How to fetch payment details using Razorpay PHP API?

I am trying to integrate the code for fetching payment details.
billno: This value will be taken from the URL.
My current code:
include 'razorpay/Razorpay.php';
use Razorpay\Api\Api;
$api = new Api('Secret ID', 'Secret Key');
$payment = $api->payment->fetch($_REQUEST['billno']);
$text = json_encode($payment->toArray());
$obj = json_decode($text);
$shopping_id = $obj->{'notes'}->{'shopping_id'};
$rzp_amount = $obj->{'amount'};//xheck
$real_amount = $rzp_amount/100;
$rzp_key = $obj->{'id'};
$rzp_status = $obj->{'status'}; //Authorised cgecj
$rzp_descp = $obj->{'description'};
$rzp_mail = $obj->{'email'};
$rzp_phone = $obj->{'contact'};
$rzp_address = $obj->{'notes'}->{'address'};
$rzp_timestamp = $obj->{'created_at'};
$rzp_method = $obj->{'method'};
Extracting the billno using the code would give us the above mentioned $rzp variables.
The payment->fetch call returns a Payment Entity, and has all the data members available for direct access. You don't have to decode JSON yourselves, that is taken care of by the SDK:
include 'razorpay/Razorpay.php';
use Razorpay\Api\Api;
$api = new Api('Secret ID', 'Secret Key');
$payment = $api->payment->fetch($_REQUEST['billno']);
echo $payment->amount;
print_r($payment->notes);
Disclaimer: I work for Razorpay

Quickbooks Online sdk add invoice exception

I am integrating my application with with Quickbook Online Sandbox account using php sdk version 3. I am able to connect and fetch/add data like customers without a problem. But I am unable to create an invoice using the sdk. Here is my code:
$invoiceObj = new IPPInvoice();
$Line = new IPPline();
$Line->Amount = 15;
$Line->DetailType = 'SalesItemLineDetail';
$saleItemLineDetail = new IPPSalesItemLineDetail();
$saleItemLineDetail->ItemRef = 1;
$saleItemLineDetail->UnitPrice = 15;
$saleItemLineDetail->Qty = 2;
$Line->SalesItemLineDetail = $saleItemLineDetail;
$invoiceObj->Line = $Line;
$invoiceObj->DocNumber = '23713';
$invoiceObj->TxnDate = 2015-10-11;
$invoiceObj->CustomerRef = 67;
try{
$resultingInvoiceObj = $connect->Add($invoiceObj);
} catch (Exception $e){
echo $e->getMessage();
}
I am writing this in a function which takes the connection object as parameter. I am able to add customer using this connection object in the same function.
The response i'm getting is
2015-04-22 06:46:15 - E:\wamp\www\test\application\libraries\QuickBooksOnline\DataService\DataService.php - 340 - CheckNullResponseAndThrowException - Response Null or Empty
I got stuck here. Please point out where i am doing wrong. Any help in this regard is highly appreciated.
Try this once to pass customerRef
$customerRef2 = new IPPReferenceType();
$customerRef2->value = "67";
$invoiceObj->CustomerRef = $customerRef2;

Netsuite PHP API: how to update custom field on item record

I have looked at some answers on here and tried putting them into action in my script; but it's not working, and I'm not sure why.
I am trying to update a custom field on an already-existing inventory item. Here is what I have so far
<?php
require_once '../PHPToolkit/NetSuiteService.php';
$service = new NetSuiteService();
$item = new InventoryItem();
$item->internalId = 72309;
$customFieldList = new CustomFieldList();
$customField = new StringCustomFieldRef();
$customField->value = utf8_encode("12345");
$customField->internalId = 'custitem_testinput';
$customFieldList->customField[] = $customField;
$item->customFieldList = $customFieldList;
$request = new UpdateRequest();
$request->record = $item;
$response = $service->update($request);
?>
I'm trying to pull the item record up by its internalID, and then update just 1 of its many custom fields.
This code doesn't error out, but it doesn't seem to do anything either. Looking at the var_dump of the objects, I can see an item object with just the parameters I've set populated, and everything else null.
What am I doing wrong?
This is a basic example showing how to update custom field in an existing record using PHP toolkit.
<?php
require_once 'PHPToolkit/NetSuiteService.php';
$service = new NetSuiteService();
$ItemId = '72309';
$ItemRecord= new InventoryItem();
//StringCustomFieldRef
$itemField= new StringCustomFieldRef();
$itemField->scriptId = 'custitem_part_lookup';
$itemField->value = 'test';
$customFieldList = new customFieldList();
$customFieldList->customField = array($itemField);
$ItemRecord->internalId = $ItemId ;
$ItemRecord->customFieldList = $customFieldList;
$updateRequest = new UpdateRequest();
$updateRequest->record = $ItemRecord;
$updateResponse = $service->update($updateRequest);
?>
For additional information you can visit Net Suite User Group

How to re-initialize the Docusign PHP API with different credentials

We have a case where we need to check envelope status in two separate Docusign accounts. If we don't get status in the first, we want to check the second.
I'm having trouble getting the API to re-initialize with the credentials of our second account. I'm calling this snippet with the new variables:
require_once('docusign/SignatureApi.php');
$IntegratorsKey = "abcd";
$UserID = "dave#account.com";
$Password = "xxxxx";
$_apiEndpoint = $Endpoint;
$_apiWsdl = "docusign/api/APIService.wsdl";
$api_options = array('location'=>$_apiEndpoint,'trace'=>true,'features' => SOAP_SINGLE_ELEMENT_ARRAYS);
$api = new APIService($_apiWsdl, $api_options);
$api->setCredentials("[" . $IntegratorsKey . "]" . $UserID, $Password);
$res = RequestEnvelopStatuses($envelopes);
$envelopeStatuses = $res->RequestStatusesResult;
if(!count($envelopeStatuses->EnvelopeStatuses->EnvelopeStatus)){
// If we did not find envelopes, check other account
$IntegratorsKey = "wxyz";
$UserID = "fred#altaccount.com";
$Password = "xxxxx";
$api->setCredentials("[" . $IntegratorsKey . "]" . $UserID, $Password);
// retry request
$res = RequestEnvelopStatuses($envelopes);
$envelopeStatuses = $res->RequestStatusesResult;
}
It doesn't return an error, but won't return envelope status either. It seems to still use the first credentials (guessing). The second attempt always seems to return whatever the first attempt did.
Is there a better / preferred way to do this?
That does not look like the proper way to get the envelope status. Maybe that's why you are not finding them and trying to look again?
// Create a filter using account ID and today as a start time
$envStatusFilter = new EnvelopeStatusFilter();
$envStatusFilter->AccountId = $AccountID;
$beginDateTime = new EnvelopeStatusFilterBeginDateTime();
$beginDateTime->_ = todayXsdDate(); // note that this helper function
// is in CodeSnippets/include/utils.php
// in the PHP SDK
$envStatusFilter->BeginDateTime = $beginDateTime;
// Send
$requestStatusesparams = new RequestStatuses();
$requestStatusesparams->EnvelopeStatusFilter = $envStatusFilter;
$response = $api->RequestStatuses($requestStatusesparams);

Categories