ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 苹果内购手册 > https://developer.apple.com/documentation/appstorereceipts/requestbody ## laravel苹果内购扩展包 > https://github.com/imdhemy/appstore-iap > https://imdhemy.com/laravel-iap-docs/docs/appstore-iap/intro // 相关文档介绍 ## 如何使用? 安装扩展 `composer require imdhemy/appstore-iap` 代码示例 ~~~ // 苹果支付相关 use Imdhemy\AppStore\ClientFactory; use Imdhemy\AppStore\Receipts\Verifier; use Imdhemy\AppStore\ValueObjects\ReceiptInfo; // 失败后的队列处理 use App\Jobs\Store\ApplePayFailJob; /** * 新版苹果内购支付 * @param $receipt 内购后返回的base64的加密字符串 前端拿到给后端即可 * @param $user */ public function applePurchase($receipt, $user) { $client = ClientFactory::create(); $verifier = new Verifier($client, $receipt, config('apple.pay.password')); // 需要共享密码 详看文档 try { $receiptResponse = $verifier->verify(); }catch (\Exception $e){ Log::error('苹果支付验证凭证异常',getExceptionData($e)); return false; } // 获取验证的状态 $status = $receiptResponse->getStatus(); // 验证通过 开始处理 if ($status->isValid()) { // 获取环境 $environment = $receiptResponse->getEnvironment(); $receiptInfoArr = $receiptResponse->getReceipt()->getInApp(); // 存在即可进行处理 if (count($receiptInfoArr)) { foreach ($receiptInfoArr as $receiptObj) { $receiptInfo = self::getAppleOrderInfo($receiptObj, $environment); try { DB::beginTransaction(); // 购买成功之后做逻辑 // 需要检测苹果返回的订单号加上用户数据存在不 不存在 进行相关处理 DB::commit(); } catch (\Exception $ex) { // 回滚 DB::rollBack(); // 记录异常 Log::Error(); // 加入队列重试处理 ApplePayFailJob::dispatch(); continue; } } return true; } } return false; } /** * 返回苹果内购的信息 * @param ReceiptInfo $receiptInfo * @param string $environment 环境 * @return array */ protected static function getAppleOrderInfo(ReceiptInfo $receiptInfo, $environment = 'Sandbox'): array { return [ 'environment' => $environment, // 环境 'product_id' => $receiptInfo->getProductId(), // 产品ID 'transaction_id' => $receiptInfo->getTransactionId(), // 苹果订单号 'purchase_date' => $receiptInfo->getPurchaseDate() ? $receiptInfo->getPurchaseDate()->getCarbon()->format('Y-m-d H:i:s') : null, // 购买时间 'expires_date' => $receiptInfo->getExpiresDate() ? $receiptInfo->getExpiresDate()->getCarbon()->format('Y-m-d H:i:s') : null, // 过期时间 ]; } ~~~