fiizy / fiizy-api-sdk
Fiizy API PHP SDK
v1.0.6
2022-11-02 12:24 UTC
Requires
- php: >= 5.6.0
- ext-curl: *
- ext-json: *
- psr/http-message: 1.*
- psr/simple-cache: 1.*
Requires (Dev)
README
Fiizy 融资解决方案。
安装
您可以使用 Composer
composer require fiizy/fiizy-api-sdk
使用方法
创建一个新订单并引导用户完成流程。一旦用户流程完成,用户将被重定向到 returnUrl
,并且 webhookUrl
端点将被调用,传递流程的结果。如果用户取消流程,将被重定向到 cancelUrl
。
$item = new LineItem(); $item->type = LineItemType::Product; $item->subType = LineItemSubType::PhysicalProduct; $item->reference = 'product-101'; $item->status = LineItemStatus::InStock; $item->name = 'Product name'; $item->description = 'Product description'; $item->url = 'http://product-url'; $item->imageUrl = 'http://product-image-url'; $item->quantity = 1; $item->price = new Decimal(97.45); $item->taxRate = new Decimal(21.00); $item->totalAmount = new Decimal(117.91); $item->metadata = array('id' => 101); $shipping = new LineItem(); $shipping->type = LineItemType::Fee; $shipping->subType = LineItemSubType::ShippingFee; $shipping->reference = 'shipping-1'; $shipping->name = 'Flat rate'; $shipping->quantity = 1; $shipping->price = new Decimal(11.85); $shipping->taxRate = new Decimal(21.00); $shipping->totalAmount = new Decimal(14.34); $shipping->metadata = array('id' => 1); $discount = new LineItem(); $discount->type = LineItemType::Discount; $discount->subType = LineItemSubType::DiscountDiscount; $discount->reference = 'discount-1'; $discount->name = 'Discount'; $discount->quantity = 1; $discount->price = new Decimal(5.00); $discount->totalAmount = new Decimal(5.00); $discount->metadata = array(); $request = new CheckoutRequest(); $request->order = new Order(); $request->order->reference = 'ref-1'; $request->order->number = '#001'; $request->order->status = OrderStatus::NewOrder; $request->order->currency = 'EUR'; $request->order->taxAmount = new Decimal(22.95); $request->order->totalAmount = new Decimal(127.25); $request->order->metadata = array('id' => 1); $request->order->lineItems = array($item, $shipping, $discount); $request->customer = new Customer(); $request->customer->gender = 'male'; $request->customer->firstName = 'Johnny'; $request->customer->lastName = 'Appleseed'; $request->customer->dateOfBirth = '1987-12-23'; $request->customer->email = 'johnny.appleseed@fiizy.com'; $request->customer->phoneNumber = '+34111111111'; $request->customer->nationalIdentificationNumber = '123'; $request->shippingAddress = new Address(); $request->shippingAddress->firstName = 'Johnny'; $request->shippingAddress->lastName = 'Appleseed'; $request->shippingAddress->phone = '+34111111111'; $request->shippingAddress->countryIso = 'ES'; $request->shippingAddress->postalCode = '28013'; $request->shippingAddress->state = 'Madrid'; $request->shippingAddress->city = 'Madrid'; $request->shippingAddress->addressLine1 = 'Prta del Sol'; $request->shippingAddress->addressLine2 = '1'; $request->billingAddress = $request->shippingAddress; $request->endpoints = new Endpoints(); $request->endpoints->returnUrl = 'http://return-url'; $request->endpoints->cancelUrl = 'http://cancel-url'; $request->endpoints->webhookUrl = 'http://webhook-url'; $request->client = new Client(); $request->client->language = 'en'; $request->client->ip = '127.0.0.1'; $request->client->userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36'; $normalizer = new ObjectNormalizer(); $client = new Client(); $client->setHttpClient(new CurlHttpClient('Fiizy-PHP-SDK/1.0.0')); $client->setSerializer(new SimpleSerializer(array($normalizer))); $client->setDenormalizer($normalizer); $client->setAuthorizationKeys('public-key', 'private-key'); $checkout = new Checkout($client); $response = $checkout->start($request); header('Location: ' . $response->redirectUrl);
为了处理 webhook 回调,请扩展 Fiizy\Api\Webhook\Receiver
并实现所需的方法。如果接收器能够接收并处理事件,应返回 HTTP 状态码 2XX。Webhook 回调支持回调失败的重试机制(如果 HTTP 状态码不是 2XX)。
// WebhookReceiver extends Fiizy\Api\Webhook\Receiver and implements required methods.
// $receiver = new WebhookReceiver();
$secret = 'private-key';
try {
$headers = getallheaders();
if (!isset($headers[\Fiizy\Api\Util\Signature::HEADER_KEY])) {
throw new \Exception('missing signature', 401);
}
$payload = @file_get_contents( 'php://input' );
$processed = $receiver->process( $payload, $headers[ \Fiizy\Api\Util\Signature::HEADER_KEY ], $secret );
if (true === $processed) {
// store data has been modified.
$statusCode = 204;
} else {
// store data has been left as is.
$statusCode = 202;
}
http_response_code( $statusCode );
header( 'Content-Type: application/json' );
} catch ( \Exception $e ) {
http_response_code( $e->getCode() );
}