iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 24
1
Software Development

PHP新手30天實戰金流系列 第 24

[Day24]平台串金流--PAYPAL 買家付款模式(order)

  • 分享至 

  • xImage
  •  
PHP新手30天實戰金流, Laravel6

  • 商家看到的交易狀態:
    1. 商家還沒取款(capture)前, status 是 pending
    2. 商家取款(capture)之後, status 是 unclaimed
    3. 買家退款之後, status 是 denied

商家畫面(商家取款交易)

商家畫面(買家退款之後)


商家畫面(買家退款的交易)

商家畫面(買家退款之後)

買家畫面(買家退款之後)

參考 Ray 大文章
建立訂單 ( order )

public function createOrder($toBeSavedInfo, Recipient $recipient, $debug = false)
{
    // 引用SDK
    $request = new OrdersCreateRequest();
    $request->headers["prefer"] = "return=representation";
    
    // 這邊的RequestBody等等會貼在下面
    $request->body = self::buildRequestBody($toBeSavedInfo, $recipient);

    // 這邊引用剛剛設定好的 PayPalClient
    $client = PayPalClient::client();
    $response = $client->execute($request);
    if ($debug)
    {
        print "Status Code: {$response->statusCode}\n";
        print "Status: {$response->result->status}\n";
        print "Order ID: {$response->result->id}\n";
        print "Intent: {$response->result->intent}\n";
        print "Links:\n";
        foreach ($response->result->links as $link)
        {
            print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
        }
        // To toggle printing the whole response body comment/uncomment below line
        echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
    }
    
    // 建立建立完成後,我只取讓使用者用來確認的連結,預設 PayPal 提供了很多的連結,但是其他的我們都可以靠 API 來達成。
    foreach (($response->result->links) as $link)
    {
        if ($link->rel === 'approve')
        {
            $linkForApproval = $link->href;
            break;
        }
    }

    // 這邊取得建立訂單之後的一些會用到的資訊,然後 return
    $toBeSavedInfo['payment_id'] = $response->result->id;
    $toBeSavedInfo['statusCode'] = $response->statusCode;
    $toBeSavedInfo['custom_id'] = $response->result->purchase_units[0]->custom_id;
    $toBeSavedInfo['PayPal_total_amount'] = $response->result->purchase_units[0]->amount->value;
    $toBeSavedInfo['orderStatus'] = $response->result->status;
    $toBeSavedInfo['linkForApproval'] = $linkForApproval;

    return $toBeSavedInfo;
}

下面是建立訂單功能會用到的 RequestBody

public static function buildRequestBody($toBeSavedInfo, Recipient $recipient)
{
    // 這邊的設定,使得我們可以在 PayPal 的付款頁面,看到多個商品的明細
    $item = [];
    $i = 1;
    foreach ($toBeSavedInfo['orders'] as $order)
    {
        $item[] = [
            'name'        => $order->item_name,
            'description' => $order->item_description,
            'sku'         => $i,
            'unit_amount' => [
                'currency_code' => $toBeSavedInfo['mc_currency'],
                'value'         => $order->unit_price,
            ],
            'quantity'    => $order->quantity,
        ];
        $i ++;
    }

    // 這邊我們指定 intent ,我設在環境變數,
    return [
        'intent'              => env('PAYPAL_SANDBOX_INTENT_OF_CREATED_ORDERS'),
        'application_context' =>
            [
                'return_url'           => env('PAYPAL_SANDBOX_RETURN_URL'),
                'cancel_url'           => env('PAYPAL_SANDBOX_CANCEL_URL'),
                'brand_name'           => env('APP_NAME'),
                'locale'               => env('PAYPAL_SANDBOX_LOCALE'),
                'landing_page'         => env('PAYPAL_SANDBOX_LANDING_PAGE'),
                'shipping_preferences' => env('PAYPAL_SANDBOX_SHIPPING_PREFERENCES'),
                'user_action'          => env('PAYPAL_SANDBOX_USER_ACTION'),
            ],
            // 這邊可以設定 purchase_unit ,一個 purchase_unit 裡面可以設定税、運費、等等,這邊省略
        'purchase_units'      =>
            [
                [
                    'custom_id' => $toBeSavedInfo['merchant_trade_no'],
                    'amount'    =>
                        [
                            'currency_code' => $toBeSavedInfo['mc_currency'],
                            'value'         => $toBeSavedInfo['total_amount'],
                            'breakdown'     =>
                                [
                                    'item_total' =>
                                        [
                                            'currency_code' => $toBeSavedInfo['mc_currency'],
                                            'value'         => $toBeSavedInfo['total_amount'],
                                        ],
                                ],
                        ],

                    'items'    => $item,
                    // 這邊可以指定收件人
                    'shipping' =>
                        array(
                            'name'    =>
                                array(
                                    'full_name' => $recipient->name,
                                ),
                            'address' =>
                                array(
                                    'address_line_1' => $recipient->others,
                                    'admin_area_2'   => $recipient->district,
                                    'admin_area_1'   => $recipient->city,
                                    'postal_code'    => $recipient->postcode,
                                    'country_code'   => $recipient->country_code,
                                ),
                        ),
                ],
            ],
    ];
}

上一篇
[Day23]平台串金流--PAYPAL 退款(capture)
下一篇
[Day25]平台串金流--PAYPAL migrating V1 to V2
系列文
PHP新手30天實戰金流34
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言