scalapay / scalapay-php-sdk
轻松集成 scalapay.com API
Requires
- php: >=5.6
- ext-curl: *
- ext-json: *
This package is auto-updated.
Last update: 2024-09-03 16:46:20 UTC
README
用于集成 Scalapay API 的 PHP 库:以下链接可找到 Scalapay 完整的 API 文档。
使用 Scalapay PHP SDK,您可以管理 Scalapay 订单的流程,从创建到捕获和退款。
安装
您可以通过 Composer 安装此 SDK。
composer require scalapay/scalapay-php-sdk
在您的 PHP 文件中包含 Composer 的自动加载器
require 'vendor/autoload.php';
配置 SDK 客户端
要创建 Scalapay PHP SDK 的实例,请使用以下代码片段并添加您的 API 密钥。
对于测试环境(SANDBOX_URI),请从 Scalapay 文档 中找到 API 密钥。
对于生产环境(PRODUCTION_URI),API 密钥将由 Scalapay 团队共享,并将可在您的 Scalapay 商户门户中找到。
在此文档中,您可以找到您可以通过此 SDK 执行的所有方法。
// instance
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
// example
$apiResponse = $scalapayApi->createOrder($orderDetails);
方法
创建订单
您可以使用 createOrder() API 方法创建订单。
响应将包含 Scalapay 订单令牌和重定向 URL(将客户重定向到 Scalapay 门户以授权支付)。
// Create Order API
// set Customer object
$consumer = new \Scalapay\Sdk\Model\Customer\Consumer();
$consumer->setEmail('test@scalapay.com')
->setGivenNames('John')
->setSurname('Doe')
->setPhoneNumber('0214255312');
// set Billing Details Contact object
$billing = new \Scalapay\Sdk\Model\Customer\Contact();
$billing->setName('John billing')
->setLine1('123 Test Street')
->setSuburb('Test')
->setPostcode('00000')
->setCountryCode('IT')
->setPhoneNumber('0522789651');
// set Shipping Details Contact object
$shipping = new \Scalapay\Sdk\Model\Customer\Contact();
$shipping->setName('John shipping')
->setLine1('123 Test Street')
->setSuburb('Test')
->setPostcode('00000')
->setCountryCode('IT')
->setPhoneNumber('0522789651');
// set Item object list
$itemList = [];
$item = new \Scalapay\Sdk\Model\Order\OrderDetails\Item();
$item->setName('test product')
->setSku('test123')
->setQuantity(1);
$itemPrice = new \Scalapay\Sdk\Model\Order\OrderDetails\Money();
$itemPrice->setAmount(100);
$item->setPrice($itemPrice);
$itemList[] = $item;
// set Merchant Options object
// Replace Confirm and Failure URLS with your own redirect urls
$merchantOptions = new \Scalapay\Sdk\Model\Merchant\MerchantOptions();
$merchantOptions->setRedirectConfirmUrl('https://portal.integration.scalapay.com/success')
->setRedirectCancelUrl('https://portal.integration.scalapay.com/failure');
// set Order total amount object
$totalAmount = new \Scalapay\Sdk\Model\Order\OrderDetails\Money();
$totalAmount->setAmount(104);
// set Tax total amount object
$taxAmount = new \Scalapay\Sdk\Model\Order\OrderDetails\Money();
$taxAmount->setAmount(2);
// set Shipping total amount object
$shippingAmount = new \Scalapay\Sdk\Model\Order\OrderDetails\Money();
$shippingAmount->setAmount(5);
// set Discount total object
$discountAmount = new \Scalapay\Sdk\Model\Order\OrderDetails\Money();
$discountAmount->setAmount(3);
$discount = new \Scalapay\Sdk\Model\Order\OrderDetails\Discount();
$discount->setDisplayName('scalapay')
->setAmount($discountAmount);
$discountList = array();
$discountList[] = $discount;
// set Frequency object
$frequency = new \Scalapay\Sdk\Model\Order\OrderDetails\Frequency();
$frequency->setFrequencyType('monthly')
->setNumber(1);
// set Risk object (optional)
$risk = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Risk();
$risk->setReturning(true) // valorize as true if the customer has placed an order with Scalapay before else false
->setOrderCountLifetime(10)
->setOrderCountL30d(3)
->setOrderCountL180d(9)
->setOrderCountLifetimeRefunded(1)
->setOrderCountL30dRefunded(1)
->setOrderAmountLifetimeEur(340.42)
->setOrderAmountL30dEur(78.32)
->setOrderAmountLifetimeRefundedEur(12.22)
->setOrderAmountL30dRefundedEur(12.22)
->setLastOrderTimestamp('2024-10-01 00:00:00')
->setLastRefundTimestamp('2024-01-01 00:00:00')
->setAccountCreationTimestamp('2022-01-01 00:00:00')
->setPaymentMethodCount(4)
->setShippingAddressCount(2)
->setShippingAddressTimestamp('2023-01-01 00:00:00')
->setShippingAddressUseCount(3)
->setPhoneVerifiedTimestamp('2023-01-01 00:00:00');
// set Transportation Reservation Details object (optional)
$transportationReservationDetails = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Industry\Travel\TransportationReservationDetails();
$transportationReservationDetails->setType('air')
->setDeparture('BCN')
->setArrival('MXP')
->setTicketClass('economy')
->setTicketType('one_way')
->setLoyaltyProgramme(true);
// set Hotel Reservation Details object (optional)
$hotelReservationDetails = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Industry\Travel\HotelReservationDetails();
$hotelReservationDetails->setNights(4)
->setHotelName('Best Western')
->setHotelCountry('IT')
->setHotelStars(4)
->setInsurance(true)
->setLoyaltyProgramme(true);
// set Events object (optional)
$events = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Industry\Travel\Events();
$events->setEventName('Muse Concert in Rome')
->setEventCountry('IT')
->setCategory('concert')
->setTicketType('singleuse');
// set Travel object (optional)
$travel = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Industry\Travel();
$travel->setPersonsCount(1)
->setStartDate('2023-06-01')
->setEndDate('2023-06-10')
->setRefundable(true)
->setTransportationReservationDetails([$transportationReservationDetails])
->setHotelReservationDetails($hotelReservationDetails)
->setEvents($events);
// set Industry object (optional)
$industry = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Industry();
$industry->setCheckoutAttemptDeclineCount(2)
->setShippingLat(0.0)
->setShippingLng(0.0)
->setTravel($travel);
// set Notification object (optional)
$notification = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Type\Link\Notification();
$notification->setPreferredLanguage('italiano')
->setEmailAddress('scalapay-customer-example@gmail.com')
->setPhoneCountryCode('+39')
->setPhoneNumber('3353535335')
->setChannels(['sms']);
// set Link object (optional)
$link = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Type\Link();
$link->setNotification($notification);
// set Type object (optional)
$type = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\Type();
$type->setLink($link);
// set Plugin Details object
$pluginDetails = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions\PluginDetails();
$pluginDetails->setCheckout('woocommerce')
->setPlatform('WordPress')
->setCustomized('0')
->setPluginVersion('1.2.1')
->setCheckoutVersion('5.7')
->setPlatformVersion('6.2');
// set Extensions object
$extensions = new \Scalapay\Sdk\Model\Order\OrderDetails\Extensions();
$extensions->setRisk($risk)
->setIndustry($industry)
->setType($type)
->setPluginDetails($pluginDetails);
// set Order Details object
$orderDetails = new \Scalapay\Sdk\Model\Order\OrderDetails();
$orderDetails->setConsumer($consumer)
->setBilling($billing)
->setShipping($shipping)
->setMerchant($merchantOptions)
->setItems($itemList)
->setTotalAmount($totalAmount)
->setShippingAmount($shippingAmount)
->setTaxAmount($taxAmount)
->setDiscounts($discountList)
->setMerchantReference('123') // merchant reference is the order id in your platform
->setType('online')
->setProduct('pay-in-3')
->setFrequency($frequency)
->setExtensions($extensions);
// Create Order API call
// You will get the order token and the redirect url where to redirect the customer (customer will be redirected to Scalapay portal to authorize the payment).
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->createOrder($orderDetails);
var_dump($apiResponse);
更新商户参考
您可以使用 updateMerchantReference() API 方法在订单创建后添加或更新商户参考。
// Update Merchant Reference API call
// You obtain the token when creating the order on the Scalapay side
// The value of $merchantReference variable corresponds to the order id in your platform
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->updateMerchantReference($token, $merchantReference);
var_dump($apiResponse);
延迟捕获订单
您可以使用 delayCapture() API 方法延迟订单的捕获,通过延长支付授权过期时间。调用此 API 后,支付授权将延长至 5 天。要修改或延长支付授权,您需要联系 Scalapay 团队。在此期间,您需要使用 capture() API 方法捕获支付,否则支付授权将被取消。
// Delay Capture API call
// Delay Capture API work exclusively upon successful payment authorization
// You obtain the token when creating the order on the Scalapay side
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->delayCapture($token);
var_dump($apiResponse);
捕获订单
您可以使用 capture() API 方法捕获订单的支付授权。资金将从用户账户扣除并转移到商户账户。
// Capture API call
// Capture API work exclusively with a successful payment authorization
// You obtain the token when creating the order on the Scalapay side
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->capture($token);
var_dump($apiResponse);
取消订单
您可以使用 void() API 方法取消订单的支付授权。客户将被通知取消,并释放任何保留的资金。
// Void API call
// Void API work exclusively with a successful payment authorization
// You obtain the token when creating the order on the Scalapay side
// The value of $merchantReference variable corresponds to the order id in your platform
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->void($token, $merchantReference);
var_dump($apiResponse);
退款
您可以使用 refund() API 方法退款。
// Build order refund request object
// The value of $merchantReference variable corresponds to the order id in your platform
$orderRefund = new \Scalapay\Sdk\Model\Refund\OrderRefund();
$orderRefund->setRefundAmount($refundAmount);
$orderRefund->setMerchantReference($merchantReference);
// Refund API call
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->refund($token, $orderRefund);
var_dump($apiResponse);
获取订单详情
您可以使用 getOrderDetails() API 方法检查订单支付的状态。
// Order Details API call
// You obtain the token when creating the order on the Scalapay side
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->getOrderDetails($token);
var_dump($apiResponse);
获取支付信息
可以使用getPaymentInfo() API方法检索关于Scalapay支付的信息。
// Payment Info API call
// You obtain the token when creating the order on the Scalapay side
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->getPaymentInfo($token);
var_dump($apiResponse);
获取商户配置
可以使用getMerchantConfiguration() API方法检索商户Scalapay账户的配置。
// Merchant Configuration API call
$scalapayApi = \Scalapay\Sdk\Api::configure('YOUR_API_KEY', \Scalapay\Sdk\Model\Api\Client::SANDBOX_URI);
$apiResponse = $scalapayApi->getMerchantConfiguration();
var_dump($apiResponse);